Hi DomadoM,
根据我的研究以及搜索,c#从内存直接执行c++exe程序似乎很难做到。
通常来说,c#执行C++的程序需要以cmd的方式运行,因为c#不能直接执行c++的exe.
因此,我推荐你使用间接的方法来实现从内存加载exe。
首先,你可以用以下代码来实现从c#加载c++程序。(注意这边Main是没有参数的)
static void Main()
{
var proc = new Process();
proc.StartInfo.FileName = @"D:\TestConsole\Debug\TestConsole.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.Start();
var sw = proc.StandardInput;
Console.ReadKey();
}
其次再建一个新的控制台项目,然后用下面的代码加载刚刚的exe文件。
class Program
{
static void Main(string[] args)
{
FileStream fs = new FileStream(@"D:\ConsoleApp1\bin\Debug\ConsoleApp1.exe", FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] bin = br.ReadBytes(Convert.ToInt32(fs.Length));
fs.Close();
br.Close();
Assembly a = Assembly.Load(bin);
MethodInfo method = a.EntryPoint;
if (method != null)
{
// create an instance of the Startup form Main method
object o = a.CreateInstance(method.Name);
// invoke the application starting point
method.Invoke(o, null);
}
}
}
最终,你会看到c++控制台中的内容。
希望这会帮到你。
Best Regards,
Jack
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to
MSDN Support, feel free to contact MSDNFSF@microsoft.com.