gpt4 book ai didi

visual-c++ - 如何将字符串传递给 CreateProcess 函数?

转载 作者:行者123 更新时间:2023-12-04 08:26:49 32 4
gpt4 key购买 nike

我想将一个字符串传递给我的 CreateProcess 函数,以便我可以将这个函数用于我的所有操作。如何正确地做到这一点?

下面是我的代码:

CString ExecuteExternalProgram(CString pictureName)
{
CString parameterOne = _T(" -format \"%h\" C:\\");
CString filename = pictureName;
CString parameterLast = _T("\"");
CString parameterFull = parameterOne + filename + parameterLast;

CreateProcess(_T("C:\\identify.exe"), parameterFull,0,0,TRUE,
NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW,0,0,&sInfo,&pInfo);
CloseHandle(wPipe);

.......
}

错误:

错误 2 错误 C2664:“CreateProcessW”:无法将参数 2 从“ATL::CString”转换为“LPWSTR”c:\a.cpp

最佳答案

您需要执行以下操作:

CreateProcess(L"C:\\identify.exe",csExecute.GetBuffer(),0,0,TRUE,
NORMAL_PRIORITY_CLASS|CREATE_NO_WINDOW,0,0,&sInfo,&pInfo);
CreateProcess()出于某种原因需要命令行参数的可写缓冲区,因此 CString 的隐式转换到一个普通的旧指针不会发生(因为如果它是一个 const 指针,它只会执行隐式转换)。

如果这不是您遇到的问题,请发布有关您遇到的错误或意外行为的更多详细信息。

例如,下面运行一个小实用程序,它转储给定的命令行:
int main() {
CString csExecute = "some string data";

STARTUPINFO sInfo = {0};
sInfo.cb = sizeof(sInfo);
PROCESS_INFORMATION pInfo = {0};

CreateProcess(L"C:\\util\\echoargs.exe",csExecute.GetBuffer(),0,0,TRUE,
NORMAL_PRIORITY_CLASS,0,0,&sInfo,&pInfo);

return 0;
}

关于visual-c++ - 如何将字符串传递给 CreateProcess 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4589866/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com