简体   繁体   English

如何在 Windows ( C++ ) 中创建进程以运行另一段代码?

[英]How to create process in Windows ( C++ ) to run another section of code?

I am trying to create a process in a simple cpp program and want to run another section of code from a different source file in this process.我正在尝试在一个简单的 cpp 程序中创建一个进程,并希望在此进程中从不同的源文件运行另一段代码。 I found the CreateProcess API for Windows that allows us to create processes but it seems like it only takes exe files for execution.我发现 Windows 的CreateProcess API 允许我们创建进程,但它似乎只需要执行 exe 文件。 Is there a way to create a process in windows and provide a function etc. to be executed by it similar to threads?有没有办法在 windows 中创建一个进程并提供一个 function 等由它执行,类似于线程?

PS: I'm not looking for threads-based solutions since I want to test out a couple of things with processes and shared memory. PS:我不是在寻找基于线程的解决方案,因为我想用进程和共享 memory 测试一些东西。

Is there a way to create a process in windows and provide a function etc. to be executed by it similar to threads?有没有办法在 windows 中创建一个进程并提供一个 function 等由它执行,类似于线程?

No. But what you can do is have your program run another copy of itself, passing it command line parameters to tell it what to do as needed.不,但是你可以做的是让你的程序运行另一个自己的副本,传递命令行参数告诉它需要做什么。 You can use GetModuleFileName() to get the full path to the EXE file of the current process.您可以使用GetModuleFileName()获取当前进程的 EXE 文件的完整路径。 Then, in your main() / WinMain() , if any command line parameters are present, run the appropriate function and then exit, otherwise run your main logic normally.然后,在您的main() / WinMain()中,如果存在任何命令行参数,请运行相应的 function 然后退出,否则正常运行您的主逻辑。

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

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