user-0 提问: 关于 win32 api CreateProcess 的使用

回复:
user: user-1
content: char ExeName[] = {"C:\\MySQL\\MySQL Server 5.1\\bin"}, 可执行文件名呢？

user: user-2
content: CreateProcess The CreateProcess function creates a new process and its primary thread. The new process executes the specified executable file.   BOOL CreateProcess(   LPCTSTR lpApplicationName,                          // pointer to name of executable module   LPTSTR lpCommandLine,  // pointer to command line string   LPSECURITY_ATTRIBUTES lpProcessAttributes,  // process security attributes   LPSECURITY_ATTRIBUTES lpThreadAttributes,   // thread security attributes   BOOL bInheritHandles,  // handle inheritance flag   DWORD dwCreationFlags, // creation flags   LPVOID lpEnvironment,  // pointer to new environment block   LPCTSTR lpCurrentDirectory,   // pointer to current directory name   LPSTARTUPINFO lpStartupInfo,  // pointer to STARTUPINFO   LPPROCESS_INFORMATION lpProcessInformation  // pointer to PROCESS_INFORMATION );   Parameters lpApplicationName  Pointer to a null-terminated string that specifies the module to execute.  The string can specify the full path and filename of the module to execute or it can specify a partial name. In the case of a partial name, the function uses the current drive and current directory to complete the specification.   The lpApplicationName parameter can be NULL. In that case, the module name must be the first white space-delimited token in the lpCommandLine string. If you are using a long filename that contains a space, use quoted strings to indicate where the filename ends and the arguments begin, otherwise, the filename is ambiguous. For example, consider the string "c:\program files\sub dir\program name". This string can be interpreted in a number of ways. The system tries the possibilities in the following order:   c:\program.exe files\sub dir\program name c:\program files\sub.exe dir\program name c:\program files\sub dir\program.exe name c:\program files\sub dir\program name.exe   The specified module can be a Win32-based application. It can be some other type of module (for example, MS-DOS or OS/2) if the appropriate subsystem is available on the local computer.   lpCommandLine  Pointer to a null-terminated string that specifies the command line to execute. The system adds a null character to the command line, trimming the string if necessary, to indicate which file was actually used.  The lpCommandLine parameter can be NULL. In that case, the function uses the string pointed to by lpApplicationName as the command line.   If both lpApplicationName and lpCommandLine are non-NULL, *lpApplicationName specifies the module to execute, and *lpCommandLine specifies the command line. The new process can use GetCommandLine to retrieve the entire command line. C runtime processes can use the argc and argv arguments.   If lpApplicationName is NULL, the first white space-delimited token of the command line specifies the module name. If you are using a long filename that contains a space, use quoted strings to indicate where the filename ends and the arguments begin (see the explanation for the lpApplicationName parameter). If the filename does not contain an extension, .EXE is assumed. If the filename ends in a period (.) with no extension, or the filename contains a path, .EXE is not appended. If the filename does not contain a directory path, the system searches for the executable file in the following sequence:   The directory from which the application loaded.  The current directory for the parent process.  Windows 95 and Windows 98: The Windows system directory. Use theGetSystemDirectory function to get the path of this directory. Windows NT: The 32-bit Windows system directory. Use the GetSystemDirectory function to get the path of this directory. The name of this directory is SYSTEM32.   Windows NT: The 16-bit Windows system directory. There is no Win32 function that obtains the path of this directory, but it is searched. The name of this directory is SYSTEM.  The Windows directory. Use theGetWindowsDirectory function to get the path of this directory.  The directories that are listed in the PATH environment variable.  If the process to be created is an MS-DOS - based or 16-bit Windows-based application, lpCommandLine should be a full command line in which the first element is the application name. Because this also works well for Win32-based applications, it is the most robust way to set lpCommandLine.    lpEnvironment  Pointer to an environment block for the new process. If this parameter is NULL, the new process uses the environment of the calling process.  An environment block consists of a null-terminated block of null-terminated strings. Each string is in the form:   name=value    Because the equal sign is used as a separator, it must not be used in the name of an environment variable.   If an application provides an environment block, rather than passing NULL for this parameter, the current directory information of the system drives is not automatically propagated to the new process. For a discussion of this situation and how to handle it, see the following Remarks section.   An environment block can contain Unicode or ANSI characters. If the environment block pointed to by lpEnvironment contains Unicode characters, the dwCreationFlags field's CREATE_UNICODE_ENVIRONMENT flag will be set. If the block contains ANSI characters, that flag will be clear.   Note that an ANSI environment block is terminated by two zero bytes: one for the last string, one more to terminate the block. A Unicode environment block is terminated by four zero bytes: two for the last string, two more to terminate the block.   lpCurrentDirectory  Pointer to a null-terminated string that specifies the current drive and directory for the child process. The string must be a full path and filename that includes a drive letter. If this parameter is NULL, the new process is created with the same current drive and directory as the calling process. This option is provided primarily for shells that need to start an application and specify its initial drive and working directory.  lpStartupInfo  Pointer to a STARTUPINFO structure that specifies how the main window for the new process should appear.  lpProcessInformation  Pointer to a PROCESS_INFORMATION structure that receives identification information about the new process.  Return Values If the function succeeds, the return value is nonzero.  If the function fails, the return value is zero. To get extended error information, call GetLastError.   Remarks The CreateProcess function is used to run a new program. The WinExec and LoadModule functions are still available, but they are implemented as calls to CreateProcess.   The process is assigned a 32-bit process identifier. The identifier is valid until the process terminates. It can be used to identify the process, or specified in the OpenProcess function to open a handle to the process. The initial thread in the process is also assigned a 32-bit thread identifier. The identifier is valid until the thread terminates and can be used to uniquely identify the thread within the system. These identifiers are returned in the PROCESS_INFORMATION structure.   When specifying an application name in the lpApplicationName or lpCommandLine strings, it doesn't matter whether the application name includes the filename extension, with one exception: an MS-DOS – based or Windows-based application whose filename extension is .COM must include the .COM extension.   The calling thread can use the WaitForInputIdle function to wait until the new process has finished its initialization and is waiting for user input with no input pending. This can be useful for synchronization between parent and child processes, because CreateProcess returns without waiting for the new process to finish its initialization. For example, the creating process would use WaitForInputIdle before trying to find a window associated with the new process.   The preferred way to shut down a process is by using the ExitProcess function, because this function notifies all dynamic-link libraries (DLLs) attached to the process of the approaching termination. Other means of shutting down a process do not notify the attached DLLs. Note that when a thread calls ExitProcess, other threads of the process are terminated without an opportunity to execute any additional code (including the thread termination code of attached DLLs).   ExitProcess, ExitThread, CreateThread, CreateRemoteThread, and a process that is starting (as the result of a call by CreateProcess) are serialized between each other within a process. Only one of these events can happen in an address space at a time. This means the following restrictions hold:   During process startup and DLL initialization routines, new threads can be created, but they do not begin execution until DLL initialization is done for the process.  Only one thread in a process can be in a DLL initialization or detach routine at a time.  The ExitProcess function does not return until no threads are in their DLL initialization or detach routines.  The created process remains in the system until all threads within the process have terminated and all handles to the process and any of its threads have been closed through calls to CloseHandle. The handles for both the process and the main thread must be closed through calls to CloseHandle. If these handles are not needed, it is best to close them immediately after the process is created.   When the last thread in a process terminates, the following events occur:   The current directory specified by the lpcurrentDirectory parameter is the current directory for the child process. The current directory specified in item 2 under the lpCommandLine parameter is the current directory for the parent process.

user: user-0
content: 引用 3 楼 wang0635 的回复:char ExeName[] = {"C:\\MySQL\\MySQL Server 5.1\\bin"}, 可执行文件名呢？   那是后改过的，加上执行文件名一样不行

user: user-0
content: 引用 1 楼 zhao4zhong1 的回复:你注释掉的system行不行，如果行，将system的内容写到一个临时批处理文件比如tmp.bat中，然后 WinExec("cmd.exe /c tmp.bat",SW_HIDE); remove("tmp.bat"); 我注释掉的那段可以，但是我想用 CreateProcess ，因为 CreateProcess 灵活性高一些

user: user-2
content: 你注释掉的system行不行，如果行，将system的内容写到一个临时批处理文件比如tmp.bat中，然后 WinExec("cmd.exe /c tmp.bat",SW_HIDE); remove("tmp.bat");