读书人

帮小弟我看看小弟我的CreateProcess是

发布时间: 2012-03-02 14:40:29 作者: rapoo

帮我看看我的CreateProcess是不是有问题
void Pipdialog::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
SECURITY_ATTRIBUTES sa;
HANDLE hRead,hWrite;
sa.nLength =sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor =NULL;
sa.bInheritHandle=TRUE;
if (!CreatePipe(&hRead,&hWrite,&sa,0)){
MessageBox( "Error On CreatePipe() ");
return ;
}
STARTUPINFO si;
PROCESS_INFORMATION pi;
si.cb =sizeof(STARTUPINFO);
GetStartupInfo(&si);
si.hStdError=hWrite;
si.hStdOutput=hWrite;
si.wShowWindow=SW_HIDE;
si.dwFlags =STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
if(!CreateProcess(NULL, "D:\\my program\\decisiontable\\Debug\\decisiontable.exe/c dir/? " ,NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi))
{
MessageBox( "Error on CreateProcess() ");
return;
}
CloseHandle(hWrite);
char buffer[4096]={0};
DWORD bytesRead;
while(true){
if(ReadFile(hRead,buffer,4095,&bytesRead,NULL)==NULL)
break;
}
n_Edit1+=buffer;
UpdateData(false);
Sleep(200);
}

为什么执行完它老出现Error on CreateProcess()是不是我 "D:\\my program\\decisiontable\\Debug\\decisiontable.exe/c dir/? "写错了

[解决办法]
你自己应该知道这个参数是什么啊。

请参照:
pszApplicationName and pszCommandLine
The pszApplicationName and pszCommandLine parameters specify the name of the executable file the new process will use and the command-line string that will be passed to the new process, respectively. Let 's talk about the pszCommandLine parameter first.


NOTE
--------------------------------------------

Notice that the pszCommandLine parameter is prototyped as a PTSTR. This means that CreateProcess expects that you are passing the address of a non-constant string. Internally, CreateProcess actually does modify the command-line string that you pass to it. But before CreateProcess returns, it restores the string to its original form.

This is important because an access violation will occur if your command-line string is contained in a read-only portion of your file image. For example, the following code causes an access violation because Visual C++ 6.0 places the "NOTEPAD " string in read-only memory:

STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
CreateProcess(NULL, TEXT( "NOTEPAD "), NULL, NULL,
FALSE, 0, NULL, NULL, &si, &pi);




When CreateProcess attempts to modify the string, an access violation occurs. (Earlier versions of Visual C++ placed the string in read/write memory so calls to CreateProcess did not cause access violations.)

The best way to solve this problem is to copy the constant string to a temporary buffer before calling CreateProcess as follows:

STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;
TCHAR szCommandLine[] = TEXT( "NOTEPAD ");
CreateProcess(NULL, szCommandLine, NULL, NULL,
FALSE, 0, NULL, NULL, &si, &pi);




You might also look into using Visual C++ 's /Gf and /GF compiler switches, which control the elimination of duplicate strings and determine whether those strings are placed in a read-only section. (Also note that the /ZI switch, which allows the use of Visual Studio 's Edit & Continue debugging feature, implies the /GF switch.) The best thing you can do is to use the /GF compiler switch and a temporary buffer. The best thing Microsoft can do is fix CreateProcess so that it takes over the responsibility of making a temporary copy of the string so we don 't have to do it. Maybe this will happen in a future version of Windows.



By the way, if you are calling the ANSI version of CreateProcess on Windows 2000, you will not get an access violation because a temporary copy of the command-line string is made. (For more information about this, see Chapter 2.)


You use the pszCommandLine parameter to specify a complete command line that CreateProcess uses to create the new process. When CreateProcess parses the pszCommandLine string, it examines the first token in the string and assumes that this token is the name of the executable file you want to run. If the executable file 's name does not have an extension, an .exe extension is assumed. CreateProcess also searches for the executable in the following order:


The directory containing the .exe file of the calling process


The current directory of the calling process


The Windows system directory


The Windows directory


The directories listed in the PATH environment variable

Of course, if the filename includes a full path, the system looks for the executable using the full path and does not search the directories. If the system finds the executable file, it creates a new process and maps the executable 's code and data into the new process 's address space. The system then calls the C/C++ run-time startup routine. As noted earlier, the C/C++ run-time startup routine examines the process 's command line and passes the address to the first argument after the executable file 's name as (w)WinMain 's pszCmdLine parameter.

All of this happens as long as the pszApplicationName parameter is NULL (which should be the case more than 99 percent of the time). Instead of passing NULL, you can pass the address to a string containing the name of the executable file you want to run in the pszApplicationName parameter. Note that you must specify the file 's extension; the system will not automatically assume that the filename has an .exe extension. CreateProcess assumes that the file is in the current directory unless a path precedes the filename. If the file can 't be found in the current directory, CreateProcess doesn 't look for the file in any other directory—it simply fails.

Even if you specify a filename in the pszApplicationName parameter, however, CreateProcess passes the contents of the pszCommandLine parameter to the new process as its command line. For example, say that you call CreateProcess like this:

// Make sure that the path is in a read/write section of memory.
TCHAR szPath[] = TEXT( "WORDPAD README.TXT ");

// Spawn the new process.
CreateProcess(TEXT( "C:\\WINNT\\SYSTEM32\\NOTEPAD.EXE "),szPath,...);




The system invokes the Notepad application, but Notepad 's command line is WORDPAD README.TXT. This quirk is certainly a little strange, but that 's how CreateProcess works. This capability provided by the pszApplicationName parameter was actually added to CreateProcess to support Windows 2000 's POSIX subsystem.


[解决办法]
"/c dir/? "是参数,试试:
CreateProcess( "D:\\decisiontable.exe ", "/c dir/? " ,NULL,NULL,TRUE,NULL,NULL,NULL,&si,&pi))

读书人网 >VC/MFC

热点推荐