在我的C / C ++库中,我有一个这样的原型函数:
long myFunction( const wchar_t*& pszOutput)Dll为pszOutput分配内存并释放它。
如何从C#调用此函数?
我已经尝试了以下几行的很多变化(使用MarshalAs,......),但是在退出myFunction时我总是遇到崩溃:
[DllImport("mydll.dll", EntryPoint = "myFunction", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)] private static extern long myFunction(ref string szOutput);有谁知道,怎么做?
In my C/C++ Library, I've a function with such a prototype:
long myFunction( const wchar_t*& pszOutput)The Dll allocates the memory for pszOutput and deallocates it too.
How do I call this function from C# ?
I've tried a lot of variation of the following line (with out, with MarshalAs, ...), but I always get a crash when exiting myFunction:
[DllImport("mydll.dll", EntryPoint = "myFunction", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)] private static extern long myFunction(ref string szOutput);Does anyone knows, how to do that?
最满意答案
如果你在Windows上,C ++ long不是C# long ,通常是C# int 。 用C ++检查sizeof(long) 。 如果它是4那么它是int 。
尝试传递out IntPtr szOutput ,然后将Marshal.PtrToStringUni()与返回的IntPtr 。
有关类似问题,请参阅例如PInvoke和char ** 。
If you are on Windows, C++ long isn't C# long, it is normally C# int. Check the sizeof(long) in C++. If it is 4 then it is int.
Try passing a out IntPtr szOutput, then use the Marshal.PtrToStringUni() with the returned IntPtr.
See for example PInvoke and char** for a similar problem.
更多推荐
发布评论