I have a win32 dll that I inject into a running process in order to extract and monitor/log data. This dll then hosts the CLR and uses ExecuteInDefaultAppDomain to load my c# dll that creates a WinForm.
Currently, when my c# dll is loaded and the init function is called, it creates a WinForm with a richtextbox to display information and it can import all my win32 dll's functions easily.
So now, how can I call a function to write to my richtextbox in my c# WinForm repeatedly(in a loop) from one of the functions in my win32 dll(both dll's are already loaded in the same process)?
edit: I have looked up quite a lot of topics relating to the various ways but none seemed to fit my situation on sites like here stackoverflow, msdn, codeguru, google.
Would using reverse p/invoke work for me in this situation? If so, can you please show me a small example?
Of course you'll use P/Invoke with a Delegate. Look at this answer here on SO: Howto implement callback interface from unmanaged DLL to .net app?
Related
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.
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
I need to dynamic loading some .NET control from DLL in runtime from C# winforms host project.
for example:
1.dll contrains control1
2.dll contrains control2
3.dll contrains control3
host app may load any of this dll-s, get controls from dll and put it on self form.
I tried:
__declspec(dllexport) TestCLRPlugin::Properties ^ GetControl()
{
return gcnew TestCLRPlugin::Properties();
}
but, clrcall methods can't be exported from dll.
You need to use the Assembly.LoadFrom methods and then you can use reflection to get the method/class/etc you want from the assembly. There are pretty good examples if you follow the MSDN link.
I'm not sure quite what you're looking for, but you may want to read up on reflection, which makes this pretty easy:
http://msdn.microsoft.com/en-us/library/ms173183(v=vs.80).aspx
Sorry if you're already familiar or that doesn't help.
In C#, Is it possible to dynamicaly load a .NET library at runtime with using something like System.Reflection.Assembly.LoadFile & disabling the loaded library from using [DllImport("someCPP.DLL", EntryPoint ="someFunction")] so that you can't call c++ stuff for security reasons??
The reason for this question is i'm thinking about making a generic openSource browser plugin that could run any .NET code & display things like OpenGL or Direct3D or whatever content you want in the browser using .NET. This would need the loaded .NET library to be secure though by disabling "DllImport" & maybe some other things. Is this possible?
You can use an application domain with restricted security permissions.
Here is a general introduction to application domains:
Application Domains (C# and Visual Basic)
And here is the specific permission related to executing unmanaged code:
SecurityPermissionFlag.UnmanagedCode
We had a bit of a problem where we have lost some source code for a very old ActiveX component we had. We've got a Delphi7 program that calls the Active X component. Thats a bit of a moster and can't be changed too much. However, some of the functionality in the Active X component needs to be updated. Since we've been moving our apps to C#, its been decided to redo the ActiveX as a C# class library.
I've got a basic C# class library working to prove the concept. I can hack the GUIDs and registry so it just looks liek the old one. If I look at the type lib for the old one and the new one, they are basically the same. Couple of slight differences but not anything i would have thought would be a problem.
Now as far as I can see there are three ways to use the new class library:
Late binding by looking up the ProgId. This works ok.
Import the type library. (Project| Import Type Library) This works ok.
Import as an ActiveX component. This generates a tlb file. This doesn't work. When I go to create the component I get 'No such interface exists'. The class is loading to a point but not completely.
Now unfortunately, the Delphi program is using option 3. So I have to try to get my class library working in this circumstance. Any ideas how I get past that error? I googled and found some references but nothing that says what the problem is.
Second question. I should know how to do this since I've done it once but I seemed to have fluked it. When I am in Delphi I do an Import ActiveX component to generate a TLB file. Now the first C# class library generated is showing up in the list of ActiveX controls and I can Create Unit for it. But I've created another simplier class library for testign purposes and damned if I can get the library to show in the list of ActiveX controls and so I can't import it.
The Create Unit for the Active X component generates a different format of TLB.pas to the Import Type Library format and its the ActiveX one that I require.
An ActiveX Control does way more then just implementing your interfaces. It is more like an TComponent or TWinControl. It requires you to implement a lot more ActiveX Control interfaces. Your C# object exposed through COM/ActiveX is not a Control, but a simple class.
It does not show up in Import ActiveX Control since it is not an ActiveX Control. I do not think it is feasible to make your C# class an Active Control.
Another possibility, although probably not the most elegant, would be to create a new ActiveX com object in Delphi which in turn calls your C# class by either method which you already have working.
Since you stated that this is for an existing Delphi 7 application, you might want to just use Com Interop on the C# side, expose the object as com visible, and the consume it directly from your Delphi 7 application.
The first option would probably allow you to get by with minimal changes to your existing application, but does add an additional layer that most likely is not necessary.