Running C++ Qt app from C# - c#

Introduction: I have qt-opencv-multithreaded - Qt/OpenCv application writen in C++ (running on Windows). But I also have another application written in C#.
C++ app does analysis of video and identifies robots (position, rotation). And stores it in variable inside ProcessingThread.
C# app receives these values and decides where should robots move.
I decided to use C# project as startup project. And I tried to create C++/cli wrapper for qt-opencv-multithreaded .
Questions: Now I have two questions:
How do I run the Qt app from another project? I tried to add include MainWindow.h to wrapper project and create instance from it. I added also directory of qt-opencv-multithreaded to "Additional Include Directories" but it wants also qt directories and I am worried that it is not a right way to do it. So how should I do it?
I know I need to use wrapper. But how do I sent data stored in list of robots in C++ project in processing thread to C# project? (where robot includes cv::Point2i position and cv::Point2i rotation and some other not important inner class) I tried to "pseudoserialize" values into string, send it to C#, and then "deserialize" it to prepared object. But it seems somehow wrong. Is there simple way to do what I need?
Thank you

If you want to run it as a stand-alone process, C# provides facilities for doing so.
If you want to call individual functions of your C++ code from C#, look into P/Invoke

Related

Is it possible/better to use PHP to execute C# code from the file or is it necessary to make it an executable?

I am making a PHP webpage that will retrieve data from a database and allow a user to send that data as an object to a database utilized by another application. Because of the API of the application, the object I need to send it as requires .NET.
The C# page takes in the necessary constructors, creates the object, and sends it. This part of my code functions perfectly when run within Visual Studio or when ran as a .exe from the command line. I can, presumably, run exec() on the executable and have it run that way within my PHP page.
However, I am hesitant to develop it this way. It seems like there should be a way to simply have a/some PHP file(s) within the same project and call the C# file directly.
I am very new to both PHP and C#. I have searched for both solutions, and I have only found that it is possible to run an executable that uses C# within PHP and seen examples of C# using PHP, but nothing that addresses using an executable over referencing the file directly, or even anything about just referencing a C# file within a Visual Studio project.
To clarify the question, I am wondering if it is possible to execute code from a C# file directly with PHP, and if so, is it a better practice than using exec() to run an executable version of the C# project.
EDIT: I want to clarify that I know PHP isn't naturally available within Visual Studio.
Yes, it's possible to execute C# code directly from PHP. The title of your question suggest you want to execute C# code that is compiled in .dll form not as an executable application .exe Excute C# Code From File
Using a DLL With PHP for Dummies
Call C-Sharp Using PHP
Make it executable than call it in php by
<?php
exec('file.exe');
?>
You can also read this article: Calling .Net Framework and .Net Assemblies from PHP

in C#, calling third party API (C++)

Was wondering if anyone would be so kind to point me in the right direction on how to call a third party's C++ DLL in C#. The API is closed source but the header files are available.
I've read up on using P/Invoke but after further diving, it's leading to marshalling very complex data structures across, and that's IF I can do the declarations for the third party classes/functions.
If there's any reading material / tutorials out there you can recommend or just point me in the right direction, I would appreciate it.
Thanks so much in advance
I found http://www.pinvoke.net/ to be very helpful.
Also the http://clrinterop.codeplex.com/releases/view/14120
Additionally, what I did that I found helpful was to do a test solution. Start off with making a C# console app that initially does nothing, then make another Project (File, New, Project, Visual C++, Win32 Console Application)
When the Wizard appears, choose DLL, Export symbols, (ATL and/or MFC if you need that).
When the Project appears, compile it as is and copy the DLL to you output folder of you C# Project
Use the P/Invoke Interop Assistant to generate the C# method based on the header (I'd start with the function that takes no params and returns an int.)
then call that from your C# console app.
Once I'd established how it worked with something really simple it was much easier to expand a bit at a time.
Try writing the C# calls yourself and then use the P/Invoke Interop Assistant to double check. Best wishes.

Reading a File While Being Written to by Another Program(Windows)

So basically, I'm trying to read a log file from a game while that game is running in order to have my program react to it in dynamic ways. I know it's possible, as other programs such as Notepad(++) are able to do it. However, after searching for an answer, most of what I can come up with is only Linux related.
I have no idea as to how to go about this, help in doing so with either C++ or C# would be great. Even though I would prefer native C++ over C#.
The "native" winapi solution is CreateFile, and in particular its dwShareMode parameter.

VS2010, C#, Python: Python script file execution on runtime

I wish to create a sample program in C# where it downloaded real time data (ie datalogger) via USB port. C# accept the data (hex or numerical) and then automatically download a python file (ie validatemethodA.ps) which is then executed. The python accept the data from the C# code and then do validation check on data or do complex calculation and return results back to C#, where the data is displayed on window form and appended to the file (by C#).
The Python is a script, where it does not requires complies in advance (during c# runtime) this mean the user is free to modify the python script to optimise pass/fails parameter or calculation results without getting back to VS2010 to complies the whole program.
This mean I can supply release C# product and only user can modify the python file.
How this is achieved?, is there sample/demo, can anyone link me to this solution so I can experiment this proof of concept?
Have you looked at IronPython?
There is a whole bunch of information on Stack Overflow including a number of particularly relevant questions such as IronPython - Load script from string in C# 4.0 application.
And plenty more on the web.
There are a couple of ways to do this...one would be to "shell out" to Python by invoking a system call from your C# program. There, you would be looking to use the .NET runtime Process.Start method to run the Python interpreter as a subprocess from your C# program.
Another option would be to use a tighter integration between C# and Python by using the IronPython interpreter. IronPython (ipy), is the python interpreter implemented in .NET
ipy can run Python code (essentially the same as normal "c" python, but with some differences), but can also import .NET assemblies and interact with the .NET runtime, directly.

C# - Possible to use Subinacl or something else (an api maybe?) from C# code?

I've created a program in C# which creates users and adds them to groups, everything is working fine. But I also want to create a "home folder", which is on another server, and the share will be like this: 81file01/users/username. And of course set the rights of the folder to the newly created AD-user. Now we're using a vb-script to do this, and this part is done with Subinacl, but is there a way to do this through my c# code?
I'm using .net 3.5 by the way :)
You can shell out using System.Diagnostics.Process and just call Subinacl directly as you do now.
Or you roll your sleeves up, and get your hands dirty calling what appear to be mainly a Win32 set of APIs. The friendliest article on that subject I could find from Microsoft talks about using the COM interface via COM interop.
There are some wrappers to these APIs floating around, but how good they are, I don't know.

Categories