AccessViolation,从 C++/CLI 调用 C++-DLL 时
我为 C++-DLL 编写了一个 C++/CLI 包装器,以便在 C# 程序中使用此 DLL。
但是,当我调用一个需要 char* 的函数时,我得到了 AccessViolation
int Wrapper::Net_methodX(int a, String^ key, long v)
{
IntPtr ptr = Marshal::StringToHGlobalAnsi(key);
pin_ptr<char> cKey = static_cast<char*>(ptr.ToPointer());
int val = methodX(a,cKey, v); // AccessViolation here
Marshal::FreeHGlobal(ptr);
return val;
}
C++ 函数的签名是
int methodX(int a, char *Key, long v);
EDIT 1
只是像下面这样“固定”也不起作用:
int Wrapper::Net_methodX(int a, String^ key, long v)
{
IntPtr ptr = Marshal::StringToHGlobalAnsi(key);
char* cKey = static_cast<char*>(ptr.ToPointer());
pin_ptr<char> pinned = cKey;
int val = methodX(a,cKey, v);
Marshal::FreeHGlobal(ptr);
return val;
}
编辑1结束
编辑2
我也尝试了PtrToStringChars以下方式(谢谢马特,还找到了一些文档此处):
int Wrapper::Net_methodX(int a, String^ key, long v)
{
pin_ptr<const wchar_t> wkey = PtrToStringChars(key);
size_t convertedChars = 0;
size_t sizeInBytes = ((key->Length + 1) * 2);
errno_t err = 0;
char * ckey = (char * ) malloc(sizeInBytes);
err = wcstombs_s(&convertedChars, ckey, sizeInBytes, wkey, sizeInBytes);
int val = methodX(A_Symbol_Table,ckey, Value);
return val;
}
AccessViolation 仍然发生,可能是 methodX() 中的错误(这是第三方 DLL) 。
EDIT 2 END
我在这里阅读了一些相关问题,但尚未找到解决方案。
有什么提示吗? 谢谢。
I've written a C++/CLI wrapper for a C++-DLL to use this DLL in a C# programm.
However, when I call a function, which takes a char* I get a AccessViolation
int Wrapper::Net_methodX(int a, String^ key, long v)
{
IntPtr ptr = Marshal::StringToHGlobalAnsi(key);
pin_ptr<char> cKey = static_cast<char*>(ptr.ToPointer());
int val = methodX(a,cKey, v); // AccessViolation here
Marshal::FreeHGlobal(ptr);
return val;
}
The signature of the C++-function is
int methodX(int a, char *Key, long v);
EDIT 1
Just to "pin" like the following didn't work either:
int Wrapper::Net_methodX(int a, String^ key, long v)
{
IntPtr ptr = Marshal::StringToHGlobalAnsi(key);
char* cKey = static_cast<char*>(ptr.ToPointer());
pin_ptr<char> pinned = cKey;
int val = methodX(a,cKey, v);
Marshal::FreeHGlobal(ptr);
return val;
}
EDIT 1 END
EDIT 2
I tried also PtrToStringChars the following way (Thanks Matt, found also some doc here):
int Wrapper::Net_methodX(int a, String^ key, long v)
{
pin_ptr<const wchar_t> wkey = PtrToStringChars(key);
size_t convertedChars = 0;
size_t sizeInBytes = ((key->Length + 1) * 2);
errno_t err = 0;
char * ckey = (char * ) malloc(sizeInBytes);
err = wcstombs_s(&convertedChars, ckey, sizeInBytes, wkey, sizeInBytes);
int val = methodX(A_Symbol_Table,ckey, Value);
return val;
}
AccessViolation still occurs, maybe it's an error in methodX() (which is a Third-party-DLL).
EDIT 2 END
I have read some related questions here, but did not find a solution yet.
Any hints?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我知道这是一个老问题,但对于任何偶然发现这个问题并寻求答案的人来说,这里有一些更简单的解决方案。
sprintf
进行转换,如下所示:sprintf(cStr, "%s", clrString);
。有关完整示例,请参阅我对此问题的回答 。marshal_as
(知识库中的方法 #4)。它比该文档中的其他方法简单得多。I know this is an old question, but for anyone who stumble upon this question looking for an answer, here are some simpler solutions.
sprintf
to do the conversion like this:sprintf(cStr, "%s", clrString);
. See my answer to this question for a complete example.marshal_as<>
(Method #4 in the KB). It's much simpler than the other methods in that document.西蒙,
我尝试了您的示例,但没有遇到访问冲突。这是我的代码:
另外,我有一些评论:
Simon,
I tried out your example and I do not get an Access Violation. Here's my code:
Also, I have a few comments:
Simon
我认为以下代码有问题
您可能想阅读此http://social.msdn.microsoft.com/forums/en-US/vclanguage/thread/0bd049fe-844a-4cb6-b9f6-c8f5107bc957
请告诉我帮助了你。
苏杰
Simon
I think there is a problem with the following code
You might want to read this http://social.msdn.microsoft.com/forums/en-US/vclanguage/thread/0bd049fe-844a-4cb6-b9f6-c8f5107bc957
Let me know if it helped you.
Sujay