C # DLL for .il, for C ++ DLL: string problem

I am trying to export some functions from C #, so I can use it in my unmanaged C ++ application. In my test project, I first create a C # DLL with a simple function to write a string to a file. Then I use ildasm to turn this into an intermediate language (.il file). The function in .il looks like this:


  // =============== CLASS MEMBERS DECLARATION ===================

  .class public auto ansi beforefieldinit MyTest.CSharpExportClass extends 

  [mscorlib]System.Object
  {
  .method public hidebysig static void        modopt([mscorlib]System.Runtime.CompilerServices.CallConvStdcall)   ExportNameOfFunction2(string test) cil managed
    {
  .vtentry 1 : 1
  .export [1] as ExportNameOfFunction2
      // Code size       25 (0x19)
      .maxstack  2
      .locals init ([0] class [mscorlib]System.IO.TextWriter tw)
      IL_0000:  ldstr      "date.txt"
      IL_0005:  newobj     instance void [mscorlib]System.IO.StreamWriter::.ctor(string)
      IL_000a:  stloc.0
    IL_000b:  ldloc.0
    IL_000c:  ldarg.0
    IL_000d:  callvirt   instance void [mscorlib]System.IO.TextWriter::WriteLine(string)
    IL_0012:  ldloc.0
    IL_0013:  callvirt   instance void [mscorlib]System.IO.TextWriter::Close()
    IL_0018:  ret
  } // end of method CSharpExportClass::ExportNameOfFunction2

.method public hidebysig specialname rtspecialname instance void  .ctor() cil managed
  {
    // Code size       7 (0x7)
    .maxstack  8
    IL_0000:  ldarg.0
    IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
    IL_0006:  ret
  } // end of method CSharpExportClass::.ctor

} // end of class MyTest.CSharpExportClass

I see the keyword "ansi" there ... After it has turned into a C ++ DLL using ilasm.exe, I try to load this function into my C ++ application:


HMODULE hdll = LoadLibraryW(L"CpCsh.dll");
    if(!hdll)
    {       
        return(-1);
    }

    typedef void (__stdcall *_EXP_NAME_OF_FUNCT)(wchar_t*);
    _EXP_NAME_OF_FUNCT      ExportNameOfFunction;

    ExportNameOfFunction = (_EXP_NAME_OF_FUNCT)GetProcAddress(hdll, "ExportNameOfFunction2");

    if(ExportNameOfFunction == NULL)
    {
        return(-1);
    }

    ExportNameOfFunction(L"hello all");

But this will only write the first letter ("h") to the file. If I declare a function using char instead of wchar_t in C ++ and use "hello all" instead of L "hello all", the entire line is written to the file.

: ildasm:/nologo/quiet/out:MyTest.dll MyTest.il/unicode/DLL/resource=MyTest.res/optimize

ilasm:/nologo/out:C:\temp\CpCsh.dll "MyTest.il" /DLL

!

+3
2

, char [], wchar_t []: wchar_t ,

, dll char*, dumpbin /exports yourdll.dll.

ilasm, , .

+2

Sandeep DLL ++.

0

Source: https://habr.com/ru/post/1769406/


All Articles