码迷,mamicode.com
首页 > Windows程序 > 详细

VcCallC#_02

时间:2016-04-27 10:43:04      阅读:359      评论:0      收藏:0      [点我收藏+]

标签:

1、VC代码:(vs2013运行正常)

// ConsoleApplication_CallCS.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

/*
int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}
//*/

/*
#include <SDKDDKVer.h>

#include <stdio.h>
#include <tchar.h>
#include <windows.h>

#include <metahost.h>
#include <mscoree.h>
#pragma comment(lib, "mscoree.lib")

int _tmain(int argc, _TCHAR* argv[])
{
    ICLRMetaHost        *pMetaHost = nullptr;
    ICLRMetaHostPolicy  *pMetaHostPolicy = nullptr;
    ICLRRuntimeHost     *pRuntimeHost = nullptr;
    ICLRRuntimeInfo     *pRuntimeInfo = nullptr;

    HRESULT hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&pMetaHost);
    //hr = pMetaHost->GetRuntime(L"v4.0.30319", IID_PPV_ARGS(&pRuntimeInfo));
    hr = pMetaHost->GetRuntime(L"vv2.0.50727", IID_PPV_ARGS(&pRuntimeInfo));

    if (FAILED(hr))
    {
        goto cleanup;
    }

    hr = pRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_PPV_ARGS(&pRuntimeHost));

    hr = pRuntimeHost->Start();

    DWORD dwRet = 0;
    //hr = pRuntimeHost->ExecuteInDefaultAppDomain(L"SampleManagedApp.exe",
    //                                             L"SampleManagedApp.Program",
    //                                             L"Test",
    //                                             L"Hello World!",
    //                                             &dwRet);
    hr = pRuntimeHost->ExecuteInDefaultAppDomain(L"ClassLibrary4VC.dll",
        L"ClassLibrary4VC.ClassLibraryTest01",
        L"ClassLibraryFunc01",
        L"Hello World!",
        &dwRet);

    hr = pRuntimeHost->Stop();

    cleanup:
    if (pRuntimeInfo != nullptr)
    {
        pRuntimeInfo->Release();
        pRuntimeInfo = nullptr;
    }

    if (pRuntimeHost != nullptr)
    {
        pRuntimeHost->Release();
        pRuntimeHost = nullptr;
    }

    if (pMetaHost != nullptr)
    {
        pMetaHost->Release();
        pMetaHost = nullptr;
    }

    system("pause");
}
//*/


//CorBindToRuntimeEx
// https://msdn.microsoft.com/zh-cn/library/99sz37yh.aspx
// http://zhidao.baidu.com/link?url=L2NYvtj82SvXjqn6-Abatd6dyK3VdSta_DZAeqGqoRUCDcsvlXrxK-vnMUXq8RJQLSSSlHY1M6RTWLHTzJJSvcuipSwuhGwkFFCWcZBZpGG
// http://blog.csdn.net/hadstj/article/details/34823905

#include <SDKDDKVer.h>

#include <stdio.h>
#include <tchar.h>
#include <windows.h>

#include <metahost.h>
#include <mscoree.h>
#pragma comment(lib, "mscoree.lib")

int _tmain(int argc, _TCHAR* argv[])
{
    ICLRMetaHost        *pMetaHost = nullptr;
    ICLRMetaHostPolicy  *pMetaHostPolicy = nullptr;
    ICLRRuntimeHost     *pRuntimeHost = nullptr;
    ICLRRuntimeInfo     *pRuntimeInfo = nullptr;

    //HRESULT hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*)&pMetaHost);
    ////hr = pMetaHost->GetRuntime(L"v4.0.30319", IID_PPV_ARGS(&pRuntimeInfo));
    //hr = pMetaHost->GetRuntime(L"vv2.0.50727", IID_PPV_ARGS(&pRuntimeInfo));

    //if (FAILED(hr))
    //{
    //    goto cleanup;
    //}

    //hr = pRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_PPV_ARGS(&pRuntimeHost));

    // invoke the method that loads the CLR

    HRESULT hrCorBind = CorBindToRuntimeEx(
        NULL, // CLR version - NULL load the latest available // L"v4.0.30319",
        L"wks", // GCType ("wks" = workstation or "svr" = Server)
        0, // STARTUP_LOADER_SAFEMODE
        CLSID_CLRRuntimeHost,
        IID_ICLRRuntimeHost,
        (PVOID*)&pRuntimeHost);
    if (FAILED(hrCorBind))
    {
        printf("CorBindToRuntimeEx failed : %08X\n", hrCorBind);
        goto cleanup;
    }
    // ***

    HRESULT hr = pRuntimeHost->Start();
    if (FAILED(hr))
    {
        printf("pRuntimeHost->Start() failed : %08X\n", hr);
        goto cleanup;
    }

    DWORD dwRet = 0;
    //hr = pRuntimeHost->ExecuteInDefaultAppDomain(L"SampleManagedApp.exe",
    //                                             L"SampleManagedApp.Program",
    //                                             L"Test",
    //                                             L"Hello World!",
    //                                             &dwRet);
    hr = pRuntimeHost->ExecuteInDefaultAppDomain(L"ClassLibrary4VC.dll",
        L"ClassLibrary4VC.ClassLibraryTest01",
        L"ClassLibraryFunc01",
        L"Hello World!",
        &dwRet);
    if (FAILED(hr))
    {
        printf("pRuntimeHost->ExecuteInDefaultAppDomain failed : %08X\n", hr);
        goto cleanup;
    }

    WORD words[6] = { 0 };
    words[0] = 11;
    words[1] = 22;
    words[2] = 33;
    words[3] = 07;
    words[4] = 55;
    hr = pRuntimeHost->ExecuteInDefaultAppDomain(L"ClassLibrary4VC.dll",
        L"ClassLibrary4VC.ClassLibraryTest01",
        L"ClassLibraryFunc02",
        (WCHAR*)&words[0],
        &dwRet);

    hr = pRuntimeHost->Stop();
    if (FAILED(hr))
    {
        printf("pRuntimeHost->Stop() failed : %08X\n", hr);
        goto cleanup;
    }

cleanup:
    if (pRuntimeInfo != nullptr)
    {
        pRuntimeInfo->Release();
        pRuntimeInfo = nullptr;
    }

    if (pRuntimeHost != nullptr)
    {
        pRuntimeHost->Release();
        pRuntimeHost = nullptr;
    }

    if (pMetaHost != nullptr)
    {
        pMetaHost->Release();
        pMetaHost = nullptr;
    }

    system("pause");
}

 

2、

 

VcCallC#_02

标签:

原文地址:http://www.cnblogs.com/cppskill/p/5437847.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!