将c#字符串传递给非托管c ++ DLL

Pra*_*bhu 5 .net clr unmanaged marshalling

我有一个简单的应用程序,加载一个非托管的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);
Run Code Online (Sandbox Code Playgroud)

而C++宣言就像

extern "C"    
__declspec(dllexport) int DumpToDBLogFile( int loggingLevel, int jobId, int threadId, string procName, string message )
{
    //access strings..
}
Run Code Online (Sandbox Code Playgroud)

请帮忙!!!

And*_*rey 7

string != LPStr
Run Code Online (Sandbox Code Playgroud)

尝试:

extern "C"
__declspec(dllexport) int DumpToDBLogFile( int loggingLevel, int jobId, int threadId, char* procName, char* message ) { //access strings..

}
Run Code Online (Sandbox Code Playgroud)