如何解决AccessViolationException
您能否帮助解决下面提到的问题:
以下是代码片段:
托管代码 (VC++) - 包装器代码:
方法 1:
void displayString(std::string abc)
{
std::string xyz=abc;
std::cout<<xyz;
}
方法 2:
void sendData(System::String^ input)
{
char* inputData = (char*)Marshal::StringToHGlobalAnsi(input).ToPointer();
std::string argData = inputData;
displayString(argData); //Working fine
//passing string instance to unmanaged code
/* while execution the below line the system throws: _CrtIsValidHeapPointer(pUserData), _BLOCK_TYPE_IS_VALID(pHead->nBlockUse), HEAP CORRUPTION DETECTED, and finally AccessViolationException */
sendDisplayString(argData); //AccessViolationException
sendDisplayString("Hello"); //AccessViolationException
}
非托管代码 C++ dll:
void sendDisplayString(std::string input)
{
std::cout<<input;
}
注意:在所有提供的链接中搜索,但没有此问题的解决方案。所有链接都会路由到 MSDN 编组页面。
提前致谢。
Could you please help in resolving the below mentioned issue:
Following are the code snippet:
Managed code (VC++) - wrapper code:
Method-1:
void displayString(std::string abc)
{
std::string xyz=abc;
std::cout<<xyz;
}
Method-2:
void sendData(System::String^ input)
{
char* inputData = (char*)Marshal::StringToHGlobalAnsi(input).ToPointer();
std::string argData = inputData;
displayString(argData); //Working fine
//passing string instance to unmanaged code
/* while execution the below line the system throws: _CrtIsValidHeapPointer(pUserData), _BLOCK_TYPE_IS_VALID(pHead->nBlockUse), HEAP CORRUPTION DETECTED, and finally AccessViolationException */
sendDisplayString(argData); //AccessViolationException
sendDisplayString("Hello"); //AccessViolationException
}
unmanaged code C++ dll:
void sendDisplayString(std::string input)
{
std::cout<<input;
}
Note: Searched in all provided link but there is no resolution for this issue. All the links route to MSDN marshalling page.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想它回答了您的问题:http://support.microsoft.com/kb/172396
I guess it answers your question: http://support.microsoft.com/kb/172396