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:
我有一个简单的应用程序,它加载一个非托管的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);
and the C++ Declaration is like
c++声明是这样的
extern "C"
__declspec(dllexport) int DumpToDBLogFile( int loggingLevel, int jobId, int threadId, string procName, string message )
{
//access strings..
}
Help please!!!
请帮助! ! !
2 个解决方案
#1
7
string != LPStr
try:
试一试:
extern "C"
__declspec(dllexport) int DumpToDBLogFile( int loggingLevel, int jobId, int threadId, char* procName, char* message ) { //access strings..
}
#2
2
I would modify the pinvoke signature....
段我将修改pinvoke签名....
[DllImport("X.dll", CallingConvention = CallingConvention.Cdecl) ] public static extern int DumpToDBLogFile(int loggingLevel, int jobId, int threadId, StringBuilder procName, StringBuilder message);
And from the managed side use the StringBuilder class initialized....
从管理方面使用StringBuilder类初始化....
StringBuilder sbProcName = new StringBuilder(1024); StringBuilder sbMessage = new StringBuilder(1024);
Then pass in the sbProcName
and sbMessage
to the DumpToDBLogFile
...
然后将sbProcName和sbMessage传递给DumpToDBLogFile…
Hope this helps, Best regards, Tom.
祝你好运,汤姆。
#1
7
string != LPStr
try:
试一试:
extern "C"
__declspec(dllexport) int DumpToDBLogFile( int loggingLevel, int jobId, int threadId, char* procName, char* message ) { //access strings..
}
#2
2
I would modify the pinvoke signature....
段我将修改pinvoke签名....
[DllImport("X.dll", CallingConvention = CallingConvention.Cdecl) ] public static extern int DumpToDBLogFile(int loggingLevel, int jobId, int threadId, StringBuilder procName, StringBuilder message);
And from the managed side use the StringBuilder class initialized....
从管理方面使用StringBuilder类初始化....
StringBuilder sbProcName = new StringBuilder(1024); StringBuilder sbMessage = new StringBuilder(1024);
Then pass in the sbProcName
and sbMessage
to the DumpToDBLogFile
...
然后将sbProcName和sbMessage传递给DumpToDBLogFile…
Hope this helps, Best regards, Tom.
祝你好运,汤姆。