在Visual Studio中将C ++项目与表单项目链接起来

时间:2017-01-08 19:08:08

标签: c++ forms visual-studio exception

我有一个包含表单(Project1)的CLR项目,以及visual studio中的另一个General项目(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);
}

1 个答案:

答案 0 :(得分:1)

我通过删除Project1.cpp中的[StaThread]行来实现它,并将_com_util :: ConvertBSTRToString()函数替换为我在此处找到的函数:https://www.codeproject.com/Articles/1969/BUG-in-com-util-ConvertStringToBSTR-and-com-util