如何在 C++ 中创建进程 在 Windows 上?
谁能告诉我如何在VC++中创建一个进程? 我需要
regasm.exe testdll /tlb:test.tlb /codebase
在该过程中执行命令。
Can anyone tell me how to create a process in VC++? I need to execute
regasm.exe testdll /tlb:test.tlb /codebase
command in that process.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
regasm.exe
(程序集注册工具)会对 Windows 注册表进行更改,因此如果您想将regasm.exe
作为提升的进程启动,您可以使用以下代码:shExecInfo.lpVerb = L"runas"
表示该进程将以提升的权限启动。 如果您不想这样做,只需将 shExecInfo.lpVerb 设置为 NULL。 但在 Vista 或 Windows 7 下,需要管理员权限才能更改 Windows 注册表的某些部分。regasm.exe
(Assembly Registration Tool) makes changes to the Windows Registry, so if you want to startregasm.exe
as elevated process you could use the following code:shExecInfo.lpVerb = L"runas"
means that process will be started with elevated privileges. If you don't want that just setshExecInfo.lpVerb
to NULL. But under Vista or Windows 7 it's required administrator rights to change some parts of Windows Registry.您需要阅读 CreateProcess()在 msdn 中。 该页面上有示例代码。
You need to read up on CreateProcess() in msdn. There's sample code on that page.
如果您只想执行同步命令(运行并等待),最好的选择是仅使用
system()
调用(请参阅 此处)来运行它。 是的,我知道这是一个 Linux 页面,但 C 是一个标准,不是吗? :-)要更细粒度地控制运行内容、运行方式(同步/异步)以及更多选项,
CreateProcess()
(请参阅 此处) 及其兄弟可能更好,尽管您将与 Windows 绑定平台(这可能不是您直接关心的)。If you just want to execute a synchronous command (run and wait), your best bet is to just use the
system()
call (see here) to run it. Yes, I know it's a Linux page but C is a standard, no? :-)For more fine-grained control of what gets run, how it runs (sync/async) and lots more options,
CreateProcess()
(see here), and its brethren, are probably better, though you'll be tied to the Windows platform (which may not be of immediate concern to you).使用 CreateProcess() 生成进程,检查返回值以确保它正常启动,然后关闭进程和线程的句柄或使用 WaitForSingleObject() 等待它完成,然后关闭句柄。
Use CreateProcess() to spawn the process, check the return value to ensure that it started okay, then either close the handles to the process and the thread or use WaitForSingleObject() to wait until it finishes and then close handles.