如何在 C++ 中创建进程 在 Windows 上?

发布于 2024-07-26 01:36:54 字数 121 浏览 6 评论 0原文

谁能告诉我如何在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜心小果奶 2024-08-02 01:36:54

regasm.exe(程序集注册工具)会对 Windows 注册表进行更改,因此如果您想将 regasm.exe 作为提升的进程启动,您可以使用以下代码:

#include "stdafx.h"
#include "windows.h"
#include "shellapi.h"

int _tmain(int argc, _TCHAR* argv[])
{
      SHELLEXECUTEINFO shExecInfo;

      shExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);

      shExecInfo.fMask = NULL;
      shExecInfo.hwnd = NULL;
      shExecInfo.lpVerb = L"runas";
      shExecInfo.lpFile = L"regasm.exe";
      shExecInfo.lpParameters = L"testdll /tlb:test.tlb /codebase";
      shExecInfo.lpDirectory = NULL;
      shExecInfo.nShow = SW_NORMAL;
      shExecInfo.hInstApp = NULL;

      ShellExecuteEx(&shExecInfo);

      return 0;
}

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 start regasm.exe as elevated process you could use the following code:

#include "stdafx.h"
#include "windows.h"
#include "shellapi.h"

int _tmain(int argc, _TCHAR* argv[])
{
      SHELLEXECUTEINFO shExecInfo;

      shExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);

      shExecInfo.fMask = NULL;
      shExecInfo.hwnd = NULL;
      shExecInfo.lpVerb = L"runas";
      shExecInfo.lpFile = L"regasm.exe";
      shExecInfo.lpParameters = L"testdll /tlb:test.tlb /codebase";
      shExecInfo.lpDirectory = NULL;
      shExecInfo.nShow = SW_NORMAL;
      shExecInfo.hInstApp = NULL;

      ShellExecuteEx(&shExecInfo);

      return 0;
}

shExecInfo.lpVerb = L"runas" means that process will be started with elevated privileges. If you don't want that just set shExecInfo.lpVerb to NULL. But under Vista or Windows 7 it's required administrator rights to change some parts of Windows Registry.

金橙橙 2024-08-02 01:36:54

您需要阅读 CreateProcess()在 msdn 中。 该页面上有示例代码。

You need to read up on CreateProcess() in msdn. There's sample code on that page.

德意的啸 2024-08-02 01:36:54

如果您只想执行同步命令(运行并等待),最好的选择是仅使用 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).

落墨 2024-08-02 01:36:54

使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文