将 c# 字符串传递给非托管 c++ DLL
我有一个简单的应用程序,它加载一个非托管 dll 并从 C# 向它传递一些字符串值。但在 C++ dll 应用程序中,我收到异常 :: 试图访问读/写保护的内存。我的 DLL 导入看起来像这样:
[DllImport("X.dll", CallingConvention = CallingConvention.Cdecl) ]
public static extern int
DumpToDBLogFile([MarshalAs(UnmanagedType.I4)]int loggingLevel,
[MarshalAs(UnmanagedType.I4)]int jobId,
int threadId,
[MarshalAs(UnmanagedType.LPStr)]string procName,
[MarshalAs(UnmanagedType.LPStr)]string message);
C++ 声明就像
extern "C"
__declspec(dllexport) int DumpToDBLogFile( int loggingLevel, int jobId, int threadId, string procName, string message )
{
//access strings..
}
Help please!!!
I have a simple application that loads an unmanaged dll and passes a few string values to it from C#. But in the C++ dll application, I receive an exception :: Tried to access a read/write protected memory. My DLL Import looks like this:
[DllImport("X.dll", CallingConvention = CallingConvention.Cdecl) ]
public static extern int
DumpToDBLogFile([MarshalAs(UnmanagedType.I4)]int loggingLevel,
[MarshalAs(UnmanagedType.I4)]int jobId,
int threadId,
[MarshalAs(UnmanagedType.LPStr)]string procName,
[MarshalAs(UnmanagedType.LPStr)]string message);
and the C++ Declaration is like
extern "C"
__declspec(dllexport) int DumpToDBLogFile( int loggingLevel, int jobId, int threadId, string procName, string message )
{
//access strings..
}
Help please!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试:
try:
我将修改 pinvoke 签名......
并从托管端使用初始化的 StringBuilder 类......
然后将
sbProcName
和sbMessage
传递给DumpToDBLogFile
...希望这有帮助,
此致,
汤姆.
I would modify the pinvoke signature....
And from the managed side use the StringBuilder class initialized....
Then pass in the
sbProcName
andsbMessage
to theDumpToDBLogFile
...Hope this helps,
Best regards,
Tom.