传递C#字符串非托管C ++ DLL [英] Passing c# string to unmanaged c++ DLL
本文介绍了传递C#字符串非托管C ++ DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个简单的应用程序加载的非托管的DLL,并传递了一些字符串值,将它从C#。但在C ++ DLL的应用程序,我收到一个异常:试图访问一个读/写保护的内存。我的DLL导入是这样的:
[的DllImport(X.dll,CallingConvention = CallingConvention.Cdecl)
公共静态外部INT
DumpToDBLogFile([的MarshalAs(UnmanagedType.I4)INT loggingLevel,
[的MarshalAs(UnmanagedType.I4)INT JOBID,
INT threadId的,
[的MarshalAs(UnmanagedType.LPStr)字符串PROCNAME,
[的MarshalAs(UnmanagedType.LPStr)字符串消息);
和C ++的宣言就像
的externC
__declspec(dllexport)的INT DumpToDBLogFile(INT loggingLevel,诠释JOBID,诠释threadId的,串PROCNAME,字符串消息)
{
//访问字符串..
}
请帮助!
解决方案
字符串!= LPSTR
尝试:
的externC
__declspec(dllexport)的INT DumpToDBLogFile(INT loggingLevel,诠释JOBID,诠释threadId的,字符* PROCNAME,字符*消息){//访问字符串..
}
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!!!
解决方案
string != LPStr
try:
extern "C"
__declspec(dllexport) int DumpToDBLogFile( int loggingLevel, int jobId, int threadId, char* procName, char* message ) { //access strings..
}
这篇关于传递C#字符串非托管C ++ DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文