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

发布于 2024-08-23 09:42:19 字数 730 浏览 4 评论 0原文

我有一个简单的应用程序,它加载一个非托管 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野鹿林 2024-08-30 09:42:19
string != LPStr

尝试:

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

}
string != LPStr

try:

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

}
叫思念不要吵 2024-08-30 09:42:19

我将修改 pinvoke 签名......

[DllImport("X.dll", CallingConvention = CallingConvention.Cdecl) ]
public static extern int
DumpToDBLogFile(int loggingLevel, int jobId, int threadId, StringBuilder procName, StringBuilder message);

并从托管端使用初始化的 StringBuilder 类......

StringBuilder sbProcName = new StringBuilder(1024);
StringBuilder sbMessage = new StringBuilder(1024);

然后将 sbProcNamesbMessage 传递给 DumpToDBLogFile...

希望这有帮助,
此致,
汤姆.

I would modify the pinvoke signature....

[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 sbProcName = new StringBuilder(1024);
StringBuilder sbMessage = new StringBuilder(1024);

Then pass in the sbProcName and sbMessage to the DumpToDBLogFile...

Hope this helps,
Best regards,
Tom.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文