贴点代码,主要是GetLastError和FormatMessage函数:
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
STARTUPINFO sinfo;//如果在此去掉后面的 “= {sizeof(sinfo)}”,就能看到后面错误处理效果
int b;
DWORD errorcode;
PROCESS_INFORMATION pinfo;
HLOCAL hlocal = NULL;
DWORD systemlocal = MAKELANGID(LANG_NEUTRAL,SUBLANG_NEUTRAL);
sinfo.dwFlags = STARTF_USESHOWWINDOW;
sinfo.wShowWindow = true;
b = CreateProcessA("C:\\windows\\notepad.exe",NULL,NULL,NULL,FALSE,CREATE_NEW_CONSOLE,NULL,NULL,&sinfo,&pinfo);
if (b)
{
printf("success!\n");
printf("The process ID is: %d\n",pinfo.dwProcessId);
CloseHandle(pinfo.hProcess);
CloseHandle(pinfo.hThread);
}
else
{
errorcode = GetLastError();
printf("进程创建失败!\n");
printf("错误代码: %d\n",errorcode);
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS ,NULL,errorcode,systemlocal,(PTSTR)&hlocal,0,NULL);
printf("原因: %s\n",hlocal);
}
return 0;
}
已经有(0)位网友发表了评论,你也评一评吧!
原创文章如转载,请注明:转载自Eddy Blog
原文地址:http://www.rrgod.com/program/181.html 欢迎订阅Eddy Blog。