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 Reactor\dotNET_Reactor.Console.exe" -file "$(TargetPath)" -targetfile "<AssemblyLocation>\<AssemblyFileName>"

posted on   空明流光  阅读(984)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示