• 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>"

  • 相关阅读:
    sql语句中的通配符
    Q&A 20090922
    Web大文件上传
    作别2010, 迎来2011的第一个工作日
    .Net 中 获取当前应用程序启动目录的几个方法
    asp.net身份验证和使用Silverlight的问题
    常用正则表达式
    最完美的xslt数值函数与字符串函数
    SQL2005的分页存储过程,支持多表多关联,亲测高效绝对可用
    处理大并发下的dropdownlist数据关联
  • 原文地址:https://www.cnblogs.com/nanfei/p/10838989.html
Copyright © 2020-2023  润新知