【发布时间】:2019-11-26 10:31:30
【问题描述】:
我正在尝试获取用户输入并在 CreateProcessW() 函数中使用它。简单地说,用户输入应用程序的路径,程序就会打开它。但它正在崩溃。任何帮助。一切编译正常。
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <processthreadsapi.h>
#include <errno.h>
void delay(unsigned int mseconds)
{
clock_t goal = mseconds + clock();
while (goal > clock());
}
int main(int argc,char *argv[])
{
LPCWSTR drive[2];
printf("\nEnter the drive, do not include '\\' (Ex. C:) : ");
wscanf(L"%s", drive);
LPCWSTR path = L"\\Windows\\notepad.exe";
STARTUPINFOW siStartupInfo;
PROCESS_INFORMATION piProcessInfo;
memset(&siStartupInfo, 0, sizeof(siStartupInfo));
memset(&piProcessInfo, 0, sizeof(piProcessInfo));
siStartupInfo.cb = sizeof(siStartupInfo);
LPCWSTR pPath;
wprintf(L"%ls%ls\n", drive, path);
printf("\nPlease enter the path exact as shown above: ");
wscanf(L"%s", &pPath);
printf("\nNow opening notepad . . . . \n\n");
delay(3000);
if (CreateProcessW(pPath,
NULL,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&siStartupInfo,
&piProcessInfo))
{
printf("Notepad opened. . .\n\n");
}
else
{
printf("Error = %ld\n", GetLastError());
}
return 0;
}
顺便说一句,我在网上和这里找到的大部分代码都被 sn-ps 泄露了。
【问题讨论】:
标签: c user-input createprocess