简体   繁体   English

winApi 使用 C++ 创建进程

[英]winApi create process using c++

I'm trying to open a picture using WinApi in c++, tried both createProcessW and createProcessA , my main problem was concatenating the strings that are used as cmdLine parameter.我正在尝试在 C++ 中使用 WinApi 打开图片,尝试了createProcessWcreateProcessA ,我的主要问题是连接用作 cmdLine 参数的字符串。 this is what iv'e got:这就是我得到的:

STARTUPINFOW process_startup_info{ 0 };
process_startup_info.cb = sizeof(process_startup_info); // setup size of strcture in bytes

PROCESS_INFORMATION process_info{ 0 };

wchar_t* s = L"\"C:\\Windows\\system32\\mspaint.exe\" ";
std::string s2 = pic.getPath();
// connecting strings here
if (CreateProcessW(NULL, /* string should be here */, NULL, NULL, TRUE, 0, NULL, NULL, &process_startup_info, &process_info))
{
    WaitForSingleObject(process_info.hProcess, INFINITE);
    CloseHandle(process_info.hProcess);
    CloseHandle(process_info.hThread);
}

Try to use the same types everywhere for the command and the params if you can.如果可以,尝试在命令和参数的任何地方使用相同的类型。 Here I am using a wstring , concatenating a parameter to the command then casting it to LPWSTR for the CreateProcess method.在这里,我使用wstring ,将参数连接到命令,然后将其转换为LPWSTR以用于 CreateProcess 方法。

STARTUPINFOW process_startup_info{ 0 };
process_startup_info.cb = sizeof(process_startup_info); // setup size of strcture in bytes

PROCESS_INFORMATION process_info{ 0 };

std::wstring params = L"\"C:\\Windows\\system32\\mspaint.exe\" ";
params.append(L"\"C:\\Vroom Owl.png\"");

// connecting strings here
if (CreateProcessW(NULL, (LPWSTR)params.data(), NULL, NULL, TRUE, 0, NULL, NULL, &process_startup_info, &process_info))
{
    WaitForSingleObject(process_info.hProcess, INFINITE);
    CloseHandle(process_info.hProcess);
    CloseHandle(process_info.hThread);
}

In include :在包括:

#include <sstream>

In code:在代码中:

std::wstringstream wstringCmd;

std::wstring wstrExec = L"\"C:\\Windows\\system32\\mspaint.exe\" "; //<- wstring 
std::string strPic = pic.getPath(); //<- if getPath() return char  
// std::wstring wstrPic = pic.getPath();//<- if getPath() return wchar  

// you can combine as you like ...
wstringCmd << wstrExec.c_str() << strPic.c_str(); // or << wstrPic.c_str();
std::wstring wstrCommande= wstringCmd.str();

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

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