내 목표는 내 프로그램에서 외부 실행 파일을 실행하는 것입니다. 첫째, 나는 system()
함수를 사용했지만, 콘솔을 사용자에게 보이고 싶지는 않습니다. 그래서, 조금 검색하고 CreateProcess()
기능을 찾았습니다. 그러나 매개 변수를 전달하려고 할 때 이유를 알지 못하고 실패합니다. MSDN에서이 코드를 가져 와서 조금 바꿨습니다.CreateProcess()가 액세스 위반으로 실패합니다.
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
void _tmain(int argc, TCHAR *argv[])
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
/*
if(argc != 2)
{
printf("Usage: %s [cmdline]\n", argv[0]);
return;
}
*/
// Start the child process.
if(!CreateProcess(NULL, // No module name (use command line)
L"c:\\users\\e\\desktop\\mspaint.exe", // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
)
{
printf("CreateProcess failed (%d).\n", GetLastError());
return;
}
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
그러나이 코드는 어떻게 든 액세스 위반을 작성했습니다. 사용자에게 콘솔을 보여주지 않고 mspaint를 실행할 수 있습니까?
대단히 감사합니다.
,'CreateProcess'는 (있는 경우)의 두 번째 매개 변수는 const가 아닌 문자열이 될 것을 요구한다. 이것이 실제로 문제가되는지는 확실치 않지만 완벽을 기하기 위해 언급하고 싶습니다. – reuben
... 그렇지 않으면 어디에서 AV가 발생합니까? 통화 스택이 있습니까? – reuben
@reuben Uhm ... 잘 모르겠지만 호출 스택의 출력이라고 생각합니다 :'kernel32.dll! 76da70ac() \t \t [아래 프레임은 올바르지 않을 수도 있고 없거나, 심볼이로드되지 않았을 수도 있습니다 KERNEL32.DLL] \t > \t msvcr100d.dll에! _nh_malloc_dbg (서명 INT n 크기, INT nhFlag, INT nBlockUse, const를 숯불 * szFileName, INT의 nLine) 라인 (302) + \t C 바이트 0x1d ++' –