簡體   English   中英

無法使用/ clr選項從Visual C ++項目加載CLR

[英]Unable load CLR from Visual C++ project with /clr option

我正在嘗試創建具有以下配置的Visual C ++ Win32控制台應用程序:操作系統:Windows 7 Visuals Studio:VS 2012公共語言運行時支持:公共語言運行時支持(/ clr [項目屬性]。

我也創建了一個Visual C#WPF應用程序,並使用該dll從下面提到的c ++程序中使用它。

我按照此鏈接中的所有說明進行操作: https : //code.msdn.microsoft.com/CppHostCLR-e6581ee0

問題是我無法啟動CLR,即ICLRRunTimeHost:Start方法返回0x1而不是0x0。請幫助/指導我解決我的錯誤,如果我的代碼概念上錯誤,請同樣指導我。

請在下面找到我的代碼:

#include "stdafx.h"
#include <metahost.h>
#include <mscoree.h> 
//#include <Windows.h>

//#using <WpfWithoutService.dll>
//using namespace System;
//using namespace System::Threading;

//using namespace WpfWithoutService;

#pragma comment(lib, "mscoree.lib")
/*ref class MainWinClass
{
public:
    void MainForm()
    {
        MainWindow^ mainwin = gcnew MainWindow();
        //mainwin->Activate();
        //mainwin->InitializeComponent();
    }
};*/

int _tmain(int argc, _TCHAR* argv[])
{
    HRESULT hr; 
    //BOOL fLoadable;

    ICLRMetaHost *pMetaHost = NULL;
    LPCWSTR pwzVersion = (LPCWSTR)"v4.0.30319";


    //hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&pMetaHost); 
    hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_PPV_ARGS(&pMetaHost));
    if (FAILED(hr)) 
    { 
        wprintf(L"CLRCreateInstance failed w/hr 0x%08lx\n", hr); 
    }

    ICLRRuntimeInfo *pRuntimeInfo = NULL; 
// Get the ICLRRuntimeInfo corresponding to a particular CLR version.
    //hr = pMetaHost->GetRuntime(L"v4.0.30319", IID_ICLRRuntimeInfo, (LPVOID *)&pRuntimeInfo); 
    hr = pMetaHost->GetRuntime(L"v4.0.30319", IID_PPV_ARGS(&pRuntimeInfo)); 
    if (FAILED(hr)) 
    { 
        wprintf(L"ICLRMetaHost::GetRuntime failed w/hr 0x%08lx\n", hr); 
        //goto Cleanup; 
    } 


/*Check if the specified runtime can be loaded into the process. This  
// method will take into account other runtimes that may already be  
// loaded into the process and set pbLoadable to TRUE if this runtime can  
// be loaded in an in-process side-by-side fashion.  

    hr = pRuntimeInfo->IsLoadable(&fLoadable); 
    if (FAILED(hr)) 
    { 
        wprintf(L"ICLRRuntimeInfo::IsLoadable failed w/hr 0x%08lx\n", hr); 
        //goto Cleanup; 
    } 


    if (!fLoadable) 
    { 
        wprintf(L".NET runtime %s cannot be loaded\n", "4.0.30319.18063"); 
        //goto Cleanup; 
    }*/


// Load the CLR into the current process and return a runtime interface  pointer. 
    ICLRRuntimeHost *pClrRuntimeHost = NULL;
    hr = pRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_PPV_ARGS(&pClrRuntimeHost)); 
    if (FAILED(hr)) 
    { 
        wprintf(L"ICLRRuntimeInfo::GetInterface failed w/hr 0x%08lx\n", hr); 
    }


// Start the CLR.
    if (hr == S_OK)
    {
        hr = pClrRuntimeHost->Start(); 
        if (FAILED(hr)) 
        { 
            wprintf(L"CLR failed to start w/hr 0x%08lx\n", hr); 
            //goto Cleanup; 
        }
    }

//Load an assembly and call the required function
    if (hr == S_OK) // if CLR is started successfully
    {
        DWORD dwRet;
        hr = pClrRuntimeHost->ExecuteInDefaultAppDomain(L"WpfWithoutService.dll", L"WpfWithoutService.MainWindow", L"InitializeComponent", NULL, &dwRet); 
        if (FAILED(hr)) 
        { 
            wprintf(L"CLR failed to start w/hr 0x%08lx\n", hr); 
        } 
    }


    if (pMetaHost) 
    { 
        pMetaHost->Release(); 
        pMetaHost = NULL; 
    } 
    if (pRuntimeInfo) 
    { 
        pRuntimeInfo->Release(); 
        pRuntimeInfo = NULL; 
    } 
    if (pClrRuntimeHost) 
    { 
        // Please note that after a call to Stop, the CLR cannot be  
        // reinitialized into the same process. This step is usually not  
        // necessary. You can leave the .NET runtime loaded in your process. 
        //wprintf(L"Stop the .NET runtime\n"); 

            pClrRuntimeHost->Stop()
            pClrRuntimeHost->Release(); 
            pClrRuntimeHost = NULL; 

    } 


    return 0;
}

ICLRRunTimeHost:Start方法返回0x1而不是0x0

即S_FALSE,在CLR已經運行時返回。 您不能啟動兩次。

具有以下配置...公共語言運行時支持

這是不合適的,它迫使CLR 此代碼開始運行之前被加載和初始化。 您只能在使用/ clr構建的本地C ++程序中編寫此類CLR托管代碼。

換句話說,您根本不需要編寫此代碼。 並沒有明顯的理由為什么要摘錄。 從Assembly :: Load()開始,您可以通過使用Reflection實現完全相同的功能。 或更簡單的是,通過添加對該程序集的引用。 實際意圖很難逆向工程。 如果實際上有一個,則只需將/ clr選項設置回“ No”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM