zoukankan      html  css  js  c++  java
  • 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>"

  • 相关阅读:
    元旦晚会
    CF906D Power Tower
    基于51单片机的多功能秒表(校赛作品)
    集训队第二次排位赛
    《史记》——五帝本纪第一,黄帝部分
    原创,让你的 "Ajax"请求华丽转身,给 "body" 或是 "Div" 加上Loading遮罩!
    Excel导出通用操作方式
    图片(img标签)的onerror事件,你有用过嘛?
    @Url.ActionLink 和 @Url.Action
    原创,自己做的一个简单实用的提示小插件,兼容性很好,基本上都兼容!
  • 原文地址:https://www.cnblogs.com/nanfei/p/10838989.html
Copyright © 2011-2022 走看看