【发布时间】:2017-01-08 19:08:07
【问题描述】:
我有一个 CLR 项目,其中包括一个表单 (Project1) 和 Visual Studio 中的另一个通用项目 (Project2),它们都使用 C++,它们都可以完美地工作。我正在尝试链接它们,但程序启动后我不断收到以下错误:
在 BrailleGUI.exe 中的 0x77400A36 (ntdll.dll) 处引发异常:0xC0000005: ?>访问冲突读取位置 0x029905BB。
如果有这个异常的处理程序,程序可以安全地继续。
我尝试将 Project2 添加到 Project1 的解决方案中,我尝试将 Project2 的 .h 和 .cpp 文件添加到 Project1 中,我什至尝试将代码复制到 Project1 中已经存在的文件中。我仍然得到同样的错误。有什么想法吗?
提前感谢您,如果您需要更多信息,请告诉我。
代码:
Project2.h
#pragma once
#include <Windows.h>
#include <iostream>
#include <WbemCli.h>
#include <string>
#include <comutil.h>
#include <regex>
#pragma comment(lib, "comsuppw.lib")
#pragma comment (lib, "wbemuuid.lib")
int ReturnCOM();
int FindPort(char *com);
Project2.cpp
#include "ChipCommunication.h"
using namespace std;
int ReturnCOM(){
int COMNumber = 0;
HRESULT hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (FAILED(hRes)){
cout << "Unable to CoInitialize";
return -1;
}
if ((FAILED(hRes = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_CONNECT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0))))
{
cout << "Unable to initialize security: 0x" << std::hex << hRes << endl;
return -1;
}
IWbemLocator* pLocator = NULL;
if (FAILED(hRes = CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_ALL, IID_PPV_ARGS(&pLocator)))){
cout << "Unable to create a WbemLocator: " << hRes << endl;
return -1;
}
IWbemServices* pService = NULL;
if (FAILED(hRes = pLocator->ConnectServer(L"root\\CIMV2", NULL, NULL, NULL, WBEM_FLAG_CONNECT_USE_MAX_WAIT, NULL, NULL, &pService))){
pLocator->Release();
cout << "Unable to connect to \"CIMV2\": " << hRes << endl;
return -1;
}
IEnumWbemClassObject* pEnumerator = NULL;
if (FAILED(hRes = pService->ExecQuery(L"WQL", L"SELECT * FROM Win32_PnPEntity", WBEM_FLAG_FORWARD_ONLY, NULL, &pEnumerator))){
pLocator->Release();
pService->Release();
cout << "Unable to retrive com ports" << hRes << endl;
return -1;
}
IWbemClassObject* clsObj = NULL;
int numElems;
while ((hRes = pEnumerator->Next(WBEM_INFINITE, 1, &clsObj, (ULONG*)&numElems)) != WBEM_S_FALSE){
if (FAILED(hRes))
break;
VARIANT vRet;
VARIANT DeviceName;
VariantInit(&vRet);
VariantInit(&DeviceName);
if (SUCCEEDED(clsObj->Get(L"PNPDeviceID", 0, &vRet, NULL, NULL)) && vRet.vt == VT_BSTR){
if (wcsstr(vRet.bstrVal, L"VID_0525&PID_A4A7") != NULL){
if (SUCCEEDED(clsObj->Get(L"Name", 0, &DeviceName, NULL, NULL)) && DeviceName.vt == VT_BSTR){
char* PointAtCOM = _com_util::ConvertBSTRToString(DeviceName.bstrVal);
COMNumber = FindPort(PointAtCOM);
delete[] PointAtCOM;
}
}
VariantClear(&vRet);
}
clsObj->Release();
}
pEnumerator->Release();
pService->Release();
pLocator->Release();
return COMNumber;
}
int FindPort(char *com){
int ComNumber = 0;
regex FindCOM("\\(COM([0-9]+)");
smatch NumberinString;
string DeviceName (com);
if (regex_search(DeviceName, NumberinString, FindCOM)){
DeviceName = NumberinString[1].str();
ComNumber = stoi (DeviceName);
}
return ComNumber;
}
Project1.h
#pragma once
#include <windows.h>
#include <iostream>
#include <sstream>
#include <msclr\marshal_cppstd.h>
int comNum;
std::ostringstream cmdCHIPStream;
std::string cmdCHIPString;
namespace BrailleGUI {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
}
protected:
~MyForm()
{
if (components){
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::Label^ label2;
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
...
Project1.cpp
#include "ChipCommunication.h"
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThread]
void main(array<String^> ^ args) {
comNum = ReturnCOM();
std::cout << "ReturnCOM is " << comNum << std::endl;
if (comNum == -1) {
std::cout << "Translator not connected" << std::endl;
}
std::ostringstream comConStream;
std::string comConString;
comConStream << "mode com" << comNum << " BAUD=115200 PARITY=n DATA=8" << std::endl;
comConString = comConStream.str();
system(comConString.c_str());
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
BrailleGUI::MyForm form;
Application::Run(%form);
}
【问题讨论】:
-
问题可能不是因为链接(因为您从中获得了功能性可执行文件),而是一些编程错误。附加调试器并查看异常的来源,然后发布代码以重现它。
-
嗯,那是代码,但它很多。请阅读stackoverflow.com/help/mcve。另外:这段代码在哪里抛出异常?
-
@stijn 抱歉,这是我第一次在 stackoverflow 上提问。我将问题缩小到 Project2.cpp 中的这一行:
char* PointAtCOM = _com_util::ConvertBSTRToString(DeviceName.bstrVal); -
调试器中的最后一行:“Project1.exe”(CLR v4.0.30319:DefaultDomain):已加载“C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0。 0__b77a5c561934e089\System.dll'。跳过加载符号。模块已优化,调试器选项“仅我的代码”已启用。在 Project1.exe 中的 0x77400A36 (ntdll.dll) 处引发异常:0xC0000005:访问冲突读取位置 0x5E17496C。
-
对不起,我尝试了一段时间,但无法重现:我将 project2.h/cpp 粘贴到 CLR 项目中(控制台应用程序,我没有安装 WinForms),然后从 CLR 调用 ReturnCOM主功能。由于 main 上的 [StaThread] 属性,这会导致 CoInitialize 失败。如果我删除 STAThread 它可以正常工作并且 ConvertBSTRToString 可以正常工作。我也没有立即看到代码有问题,除了我认为您应该在 DeviceName 上调用 VariantClear,请参阅msdn.microsoft.com/en-us/library/aa391442(v=vs.85).aspx
标签: c++ forms visual-studio exception