gpt4 book ai didi

c++ - CreateprocessW 截断的命令行参数

转载 作者:太空宇宙 更新时间:2023-11-04 13:21:05 25 4
gpt4 key购买 nike

VS10、MBCS、Unicode 预处理器定义。浏览了 Google 博士的案例手册后,最好的帮助是 here ,但它并没有完全解决这个特定的问题。考虑这段代码,其中 thisexePath 是可执行文件的路径:

cmdLineArg = (wchar_t *)calloc(BIGGERTHANMAXPATH, sizeof(wchar_t));
wcscpy_s (cmdLineArg, maxPathFolder, L"\\\\??\\C:\\My directory\\My directory\\");
CreateProcessW (thisexePath, cmdLineArg, NULL, NULL, FALSE, NULL, NULL, NULL, &lpStartupInfo, &lpProcessInfo)

程序调用时

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
wchar_t hrtext[256];
swprintf_s(hrtext, sizeof(hrtext), L"%ls", lpCmdLine);
//deal with possible buffer overflow later
MessageBoxW(NULL, hrtext, L"Display", MB_ICONINFORMATION);
}

hrtext 只显示“My directory\My directory\” 这里明显遗漏了什么?

最佳答案

这是不正确的:

wchar_t hrtext[256];
swprintf_s(hrtext, sizeof(hrtext), L"%ls", lpCmdLine);

第二个参数应该表示字符数,而不是字节数。

MSDN link to swprintf_s

应该是这样的:

wchar_t hrtext[256];
swprintf_s(hrtext, sizeof(hrtext) / sizeof(wchar_t), L"%ls", lpCmdLine);

或者简单地说:

wchar_t hrtext[256];
swprintf_s(hrtext, 256, L"%ls", lpCmdLine);

关于c++ - CreateprocessW 截断的命令行参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35262539/

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