2017-01-08 11 views
0

フォーム(Project1)とVisual Studioの別の一般プロジェクト(Project2)を含むCLRプロジェクトは、どちらもC++であり、どちらも完全に機能します。私はそれらをリンクしようとしているが、私はすぐにプログラムが起動すると、次のエラーを取得しておいてください。BrailleGUI.exeで0x77400A36(ntdll.dllの)で投げVisual StudioのフォームプロジェクトとC++プロジェクトをリンクする

例外:0xc0000005で:?>アクセス違反読み取り場所を0x029905BB。

この例外のハンドラがある場合、プログラムは安全に続行されます。私はProject1の溶液にProject2のを追加しようとした

、私はProject1のにのProject2の.hファイルと.cppのファイルを追加しようとした、私もちょうど、すでに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.c PP

#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

問題ではないかもしれませんリンクすると(機能的な実行可能ファイルを取得するため)、むしろプログラミングエラーです。デバッガをアタッチし、例外がどこから発生したのかを確認し、コードを再現するためにポストコードを確認します。 – stijn

+0

さて、それはコードですが、それはたくさんあります。 http://stackoverflow.com/help/mcveをお読みください。また、このコードではどこに例外がスローされますか? – stijn

+0

@stijn申し訳ありませんが、これは私がstackoverflowを求めているのは初めてです。 'char * PointAtCOM = _com_util :: ConvertBSTRToString(DeviceName.bstrVal);' – Armando

答えて

関連する問題