c++调用c#代码

// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <iostream>
#include <metahost.h>
#include <atlbase.h>
#include <atlcom.h>

#pragma comment(lib, "mscoree.lib")

#define IfFailRet(expr)                { hr = (expr); if(FAILED(hr)) return (hr); }
#define IfNullFail(expr)                { if (!expr) return (E_FAIL); }

extern "C" int __declspec(dllexport) CALLBACK CallClrMethod(
    const WCHAR *miniRuntimeVersion,
    const WCHAR *assemblyName,
    const WCHAR *typeName,
    const WCHAR *methodName,
    const WCHAR *args,
    LPDWORD pdwResult
)
{
    int hr = S_OK;
    CComPtr<ICLRMetaHost> spHost;
    hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_PPV_ARGS(&spHost));
    CComPtr<ICLRRuntimeInfo> spRuntimeInfo;
    CComPtr<IEnumUnknown> pRunTimes;
    IfFailRet(spHost->EnumerateInstalledRuntimes(&pRunTimes));
    ICLRRuntimeInfo *pUnkRuntime = NULL;
    ULONG fetched = 0;
    wchar_t version[20];
    DWORD versionStringSize = 20;
    while (S_OK == pRunTimes->Next(1, (IUnknown **)&pUnkRuntime, &fetched))
    {
        if (fetched <= 0 || pUnkRuntime == nullptr)
            break;

        hr = pUnkRuntime->GetVersionString(version, &versionStringSize);

        if (versionStringSize >= 2 
            && (int)version[1] >= (int)miniRuntimeVersion[0]
            && (int)version[2] >= (int)miniRuntimeVersion[1]
            && (int)version[3] >= (int)miniRuntimeVersion[2]) {
            CComQIPtr<ICLRRuntimeInfo> pp(pUnkRuntime);
            spRuntimeInfo = pp;
            break;
        }
    }

    IfNullFail(spRuntimeInfo);

    BOOL bStarted;
    DWORD dwStartupFlags;
    hr = spRuntimeInfo->IsStarted(&bStarted, &dwStartupFlags);

    CComPtr<ICLRRuntimeHost> spRuntimeHost;
    IfFailRet(spRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_PPV_ARGS(&spRuntimeHost)));
    if (!bStarted)
        hr = spRuntimeHost->Start();
    hr = spRuntimeHost->ExecuteInDefaultAppDomain(
        assemblyName,
        typeName,
        methodName,
        args,
        pdwResult);

    return hr;
}


int main()
{
    DWORD dwResult;
    HRESULT hr = CallClrMethod(
        L"4.0",
        L"ClassLibrary1.dll",  // name of DLL (can be fullpath)
        L"ClassLibrary1.Class1",  // name of managed type
        L"ManagedMethodCalledFromExtension", // name of static method
        L"some args xx",
        &dwResult);

    return 0;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassLibrary1
{
    public class Class1
    {
        /// <summary>
        /// a managed static method that gets called from native code
        /// </summary>
        public static int ManagedMethodCalledFromExtension(string args)
        {
            // need to return an integer: the length of the args string
            return args.Length * 2;
        }

    }
}

此项目加入了Reactor加壳保护,在编译后事件中添加命令:

"C:Program Files (x86)Eziriz.NET ReactordotNET_Reactor.Console.exe" -file "$(TargetPath)" -targetfile "<AssemblyLocation><AssemblyFileName>"

原文地址:https://www.cnblogs.com/nanfei/p/10838989.html