Thursday, 15 August 2013

c# - How to call a .NET DLL from a Win32 process? -


What are the options to use a .NET DLL from the Win32 process? I basically need to use C # DLL from a Win32 process.

I have a possible solution, which requires that C # DLL needs to add GAC (using RegAsm.exe), then C # DLL calls the COM wrapped, though this solution is too heavy . This requires that the .NET DLL be added to all the machines that want to run this Win32 process.

Is it possible to call the RegAsm before being able to use C # DLL?

You can use register-free COM with .NET COM components - see.

Another option is to use C ++ / CLI as a bridge, people are familiar with using it to wrap unmanaged APIs to manage it, but it is actually in both ways Works - it is possible to compile it with / clr , and yet a .dll assemblies with plain unmanaged exports, which can be called unmanaged code as usual Here's a very simple example: System :: String :: Toopper displays in this way:

  // comp.exe / clr / LD compile with wrapper.cpp ole32. Lib #include & lt; Windows.h & gt; __declipsee (dilexport) wchar_t * ToUpper (const wchar_t * wcs) {system :: string ^ s = gcnew system :: string (wcs); Array & lt; Wchar_t & gt; ^ Character = s-> ToUpper () - & gt; ToCharArray (); Size_ty size = character- & gt; Length * 2; Wchar_t * dst = (wchar_t *) Kotesak Almallok (size + 2); Pin_ptr & lt; Wchar_t & gt; Src = more characters [0]; Memcpy (DST, src, size); DST [Character-> Length] = 0; Return DST; }  

This will generate wrapper.dll - hybrid managed / unmanaged assembly - and an export library wrapper.lib . The latter can be used in the net resident application:

  // cl.exe test.cpp ole32.lib wrapper.lib // Compile with Note, no / clr #include & Lt; Stdio.h & gt; #include & lt; Windows.h & gt; Wchar_t * ToUpper (Const. Wchar_t * wcs); Int main () {wchar_t * s = Toupper (L "Foo"); Wprintf (L "% s", S); CoTaskMemFree (s); }  

In practice this CLR will load runtime in the calling process (unless it has already been loaded there) and send the familyly managed code from the native code - all Magic C ++ / CLI is compiled by the compiler.


No comments:

Post a Comment