将托管字符串[]封送为非托管字符**
这是我的 c++ 结构(使用多字节字符集)
typedef struct hookCONFIG {
int threadId;
HWND destination;
const char** gameApps;
const char** profilePaths;
} HOOKCONFIG;
和 .Net 结构
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct HOOKCONFIG {
public int threadId;
public IntPtr destination;
// MarshalAs?
public string[] gameApps;
// MarshalAs?
public string[] profilePaths;
}
我遇到了一些问题,如何封送字符串数组? 当我在 C++ 中访问结构变量“profilePaths”时,出现如下错误:
App.exe 中发生类型为“System.AccessViolationException”的未处理异常
附加信息:尝试读取或写入受保护的内存。这通常是一个 表明其他内存已损坏。
MessageBox(0, cfg.profilePaths[0], "Title", MB_OK); // error ... Orz
This is my c++ struct (Use Multi-Byte Character Set)
typedef struct hookCONFIG {
int threadId;
HWND destination;
const char** gameApps;
const char** profilePaths;
} HOOKCONFIG;
And .Net struct
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct HOOKCONFIG {
public int threadId;
public IntPtr destination;
// MarshalAs?
public string[] gameApps;
// MarshalAs?
public string[] profilePaths;
}
I got some problem that how do I marshal the string array?
When I access the struct variable "profilePaths" in C++ I got an error like this:
An unhandled exception of type 'System.AccessViolationException' occurred in App.exe
Additional information: Attempted to read or write protected memory. This is often an
indication that other memory is corrupt.
MessageBox(0, cfg.profilePaths[0], "Title", MB_OK); // error ... Orz
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单的方法:将原型更改为 IntPtr[]:
现在,当您调用时,您需要大致以下伪代码:
Easy way: Change prototype to IntPtr[]:
Now when you call you need to roughly the following psudo-code: