Interop sending string from C# to C++

前端 未结 5 1180
醉酒成梦
醉酒成梦 2020-12-11 23:25

I want to send a string from C# to a function in a native C++ DLL.

Here is my code: The C# side:

[DllImport(@\"Native3DHandler.dll\", EntryPoint = \"         


        
5条回答
  •  星月不相逢
    2020-12-12 00:00

    Works fine for me with no extra marshalling instructions in VS2008:

    C# side:

    [DllImport("Test.dll")]
    public static extern void getString(StringBuilder theString, int bufferSize);
    
    func()
    {
      StringBuilder tstStr = new StringBuilder(BufSize);
      getString(tstStr, BufSize);
    }
    

    C++ side:

    extern "C" __declspec(dllexport) void getString(char* str, int bufferSize)
    {
      strcpy_s(str, bufferSize, "FOOBAR");
    }
    

提交回复
热议问题