简体   繁体   English

如何从C#应用程序运行C ++二进制代码

[英]How to run c++ binary code from c# application

I have a binary data from a c++ application that works. 我有一个有效的C ++应用程序提供的二进制数据。 I want to run that specific binary code directly from my C# application. 我想直接从C#应用程序运行该特定的二进制代码。

I have this function that I got from internet: 我具有从互联网获得的以下功能:

    static void MemExe(byte[] buffer)
    {
        Assembly asm = Assembly.Load(buffer);

        if (asm.EntryPoint == null)
            throw new ApplicationException("No entry point found!");

        MethodInfo ePoint = asm.EntryPoint;
        object ins = asm.CreateInstance(ePoint.Name);
        ePoint.Invoke(ins, null);
    }

It it works well with C# binary code, but if I try to open a C++ binary code it crashes. 它可以很好地与C#二进制代码配合使用,但是如果我尝试打开C ++二进制代码,则会崩溃。

The exception right here: 异常在这里:

Additional information: Could not load file or assembly '5154816 bytes loaded Test, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null' or one of its dependencies. 附加信息:无法加载文件或程序集'5154816字节已加载测试,版本= 1.0.0.0,区域性=中性,公共密钥令牌=空'或其依赖项之一。 It was made an attempt to load program with an incorrect format. 试图加载格式错误的程序。

I guess it is something related to the header? 我想这与标题有关吗? I am no expert... 我不是专家...

Is it possible to run c++ binary in the same way that the function MemEx runs C# binary? 是否可以以与MemEx函数运行C#二进制文件相同的方式运行c ++二进制文件?

Thanks. 谢谢。

Assembly.Load(...) will only load .NET binaries. Assembly.Load(...)将仅加载.NET二进制文件。

If you want to have access to the API of binary other than .NET, you would want to use P/Invoke signatures. 如果要访问.NET以外的二进制API,则需要使用P / Invoke签名。

If you want to run an executable, use System.Diagnostics.Process.Start(...) . 如果要运行可执行文件,请使用System.Diagnostics.Process.Start(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM