关于使用CreateProcess的问题(50分)

  • 主题发起人 主题发起人 exmacross
  • 开始时间 开始时间
E

exmacross

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样才能在Delphi中用CreateProcess建立新进程而不终止当前进程?<br>每次我在程序里用CreateProcess运行新程序,原来的程序就退出来了。<br>可是我连退出的代码都没有写。<br>谁帮我啊!我有急用啊!非常感谢!
 
那是你的代码出错了,程序退出。<br>把代码贴出来,大家帮你看。
 
winexec,ShellExecute和CreateProcess 这三个 为什么偏偏要用 CreateProcess呢?<br>
 
我也正在解决这个问题 找了一点资料:<br><br>winexec,ShellExecute和CreateProcess有本质的区别<br>(1)<br>The WinExec function runs the specified application. <br><br>This function is provided for compatibility with earlier versions of Windows. For Win32-based applications, use the CreateProcess function. <br><br>UINT WinExec(<br><br>&nbsp; &nbsp; LPCSTR lpCmdLine, // address of command line <br>&nbsp; &nbsp; UINT uCmdShow // window style for new application <br>&nbsp; &nbsp;); <br>&nbsp;<br><br>Parameters<br><br>lpCmdLine<br><br>Points to a null-terminated character string that contains the command line (filename plus optional parameters) for the application to be executed. If the name of the executable file in the lpCmdLine parameter does not contain a directory path, Windows searches for the executable file in this sequence: <br><br>1. The directory from which the application loaded. <br>2. The current directory. <br>3. The Windows system directory. The GetSystemDirectory function retrieves the path of this directory. <br>4. The Windows directory. The GetWindowsDirectory function retrieves the path of this directory. <br>5. The directories listed in the PATH environment variable. <br><br>&nbsp;<br><br>uCmdShow<br><br>Specifies how a Windows-based application window is to be shown and is used to supply the wShowWindow member of the STARTUPINFO parameter to the CreateProcess function. For a list of the acceptable values, see the description of the nCmdShow parameter of the ShowWindow function. For a non-Windows - based application, the PIF file, if any, for the application determines the window state. <br><br>&nbsp;<br><br>Return Values<br><br>If the function succeeds, the return value is greater than 31.<br>If the function fails, the return value is one of the following error values: <br><br>Value Meaning<br>0 The system is out of memory or resources.<br>ERROR_BAD_FORMAT The .EXE file is invalid (non-Win32 .EXE or error in .EXE image).<br>ERROR_FILE_NOT_FOUND The specified file was not found.<br>ERROR_PATH_NOT_FOUND The specified path was not found.<br>&nbsp;<br><br>Remarks<br><br>Win32-based applications should use the CreateProcess function rather than this function. The WinExec function exists in Win32 to provide compatibility with earlier versions of Windows. For more information about how the WinExec function is implemented, see the Remarks section of the LoadModule function.<br>In Win32, the WinExec function returns when the started process calls the GetMessage function or a time-out limit is reached. To avoid waiting for the time out delay, call the GetMessage function as soon as possible in any process started by a call to WinExec. <br>(2)<br>The ShellExecute function opens or prints a specified file. The file can be an executable file or a document file. See ShellExecuteEx also. <br><br>HINSTANCE ShellExecute(<br><br>&nbsp; &nbsp; HWND hwnd, // handle to parent window<br>&nbsp; &nbsp; LPCTSTR lpOperation, // pointer to string that specifies operation to perform<br>&nbsp; &nbsp; LPCTSTR lpFile, // pointer to filename or folder name string<br>&nbsp; &nbsp; LPCTSTR lpParameters, // pointer to string that specifies executable-file parameters <br>&nbsp; &nbsp; LPCTSTR lpDirectory, // pointer to string that specifies default directory<br>&nbsp; &nbsp; INT nShowCmd // whether file is shown when opened<br>&nbsp; &nbsp;); <br>&nbsp;<br><br>Parameters<br><br>hwnd<br><br>Specifies a parent window. This window receives any message boxes that an application produces. For example, an application may report an error by producing a message box.<br><br>lpOperation<br><br>Pointer to a null-terminated string that specifies the operation to perform. The following operation strings are valid:<br><br>String Meaning<br>"open" The function opens the file specified by lpFile. The file can be an executable file or a document file. The file can be a folder to open.<br>"print" The function prints the file specified by lpFile. The file should be a document file. If the file is an executable file, the function opens the file, as if "open" had been specified.<br>"explore" The function explores the folder specified by lpFile. <br>&nbsp;<br><br>The lpOperation parameter can be NULL. In that case, the function opens the file specified by lpFile. <br><br>lpFile<br><br>Pointer to a null-terminated string that specifies the file to open or print or the folder to open or explore. The function can open an executable file or a document file. The function can print a document file. <br><br>lpParameters<br><br>If lpFile specifies an executable file, lpParameters is a pointer to a null-terminated string that specifies parameters to be passed to the application.<br>If lpFile specifies a document file, lpParameters should be NULL. <br><br>lpDirectory<br><br>Pointer to a null-terminated string that specifies the default directory. <br><br>nShowCmd<br><br>If lpFile specifies an executable file, nShowCmd specifies how the application is to be shown when it is opened. &nbsp;This parameter can be one of the following values: <br><br>Value Meaning<br>SW_HIDE Hides the window and activates another window.<br>SW_MAXIMIZE Maximizes the specified window.<br>SW_MINIMIZE Minimizes the specified window and activates the next top-level window in the Z order.<br>SW_RESTORE Activates and displays the window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when restoring a minimized window.<br>SW_SHOW Activates the window and displays it in its current size and position. <br>SW_SHOWDEFAULT Sets the show state based on the SW_ flag specified in the STARTUPINFO structure passed to the CreateProcess function by the program that started the application. An application should call ShowWindow with this flag to set the initial show state of its main window.<br>SW_SHOWMAXIMIZED Activates the window and displays it as a maximized window.<br>SW_SHOWMINIMIZED Activates the window and displays it as a minimized window.<br>SW_SHOWMINNOACTIVE Displays the window as a minimized window. The active window remains active.<br>SW_SHOWNA Displays the window in its current state. The active window remains active.<br>SW_SHOWNOACTIVATE Displays a window in its most recent size and position. The active window remains active.<br>SW_SHOWNORMAL Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when displaying the window for the first time.<br>&nbsp;<br><br>If lpFile specifies a document file, nShowCmd should be zero. <br><br>&nbsp;<br><br>Return Values<br><br>If the function succeeds, the return value is the instance handle of the application that was run, or the handle of a dynamic data exchange (DDE) server application.<br>If the function fails, the return value is an error value that is less than or equal to 32. The following table lists these error values:<br><br>Value Meaning<br>0 The operating system is out of memory or resources.<br>ERROR_FILE_NOT_FOUND The specified file was not found.<br>ERROR_PATH_NOT_FOUND The specified path was not found.<br>ERROR_BAD_FORMAT The .EXE file is invalid (non-Win32 .EXE or error in .EXE image).<br>SE_ERR_ACCESSDENIED The operating system denied access to the specified file. <br>SE_ERR_ASSOCINCOMPLETE The filename association is incomplete or invalid.<br>SE_ERR_DDEBUSY The DDE transaction could not be completed because other DDE transactions were being processed.<br>SE_ERR_DDEFAIL The DDE transaction failed.<br>SE_ERR_DDETIMEOUT The DDE transaction could not be completed because the request timed out.<br>SE_ERR_DLLNOTFOUND The specified dynamic-link library was not found. <br>SE_ERR_FNF The specified file was not found. <br>SE_ERR_NOASSOC There is no application associated with the given filename extension.<br>SE_ERR_OOM There was not enough memory to complete the operation.<br>SE_ERR_PNF The specified path was not found.<br>SE_ERR_SHARE A sharing violation occurred.<br>&nbsp;<br><br>Remarks<br><br>The file specified by the lpFile parameter can be a document file or an executable file. If the file is a document file, the ShellExecute function opens or prints it, depending on the value of the lpOperation parameter. If the file is an executable file, the ShellExecute function opens it, even if lpOperation specifies printing. <br>You can use ShellExecute to open or explore a shell folder. To open a folder, use either of the following calls:<br><br>ShellExecute(handle, NULL, path_to_folder, NULL, NULL, SW_SHOWNORMAL);<br><br>or<br>&nbsp; <br><br>ShellExecute(handle, "open", path_to_folder, NULL, NULL, SW_SHOWNORMAL);<br>&nbsp; <br><br>To explore a folder, use the following call:<br><br>ShellExecute(handle, "explore", path_to_folder, NULL, NULL, SW_SHOWNORMAL);<br>&nbsp; <br><br>If lpOperation is NULL, the function opens the file specified by lpFile. If lpOperation is "open" or "explore", the function will force an open window or explorer.<br><br>See Also<br><br>FindExecutable, ShellExecuteEx <br>(3)<br>he CreateProcess function creates a new process and its primary thread. The new process executes the specified executable file. <br><br>BOOL CreateProcess(<br><br>&nbsp; &nbsp; LPCTSTR lpApplicationName, // pointer to name of executable module <br>&nbsp; &nbsp; LPTSTR lpCommandLine, // pointer to command line string<br>&nbsp; &nbsp; LPSECURITY_ATTRIBUTES lpProcessAttributes, // pointer to process security attributes <br>&nbsp; &nbsp; LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to thread security attributes <br>&nbsp; &nbsp; BOOL bInheritHandles, // handle inheritance flag <br>&nbsp; &nbsp; DWORD dwCreationFlags, // creation flags <br>&nbsp; &nbsp; LPVOID lpEnvironment, // pointer to new environment block <br>&nbsp; &nbsp; LPCTSTR lpCurrentDirectory, // pointer to current directory name <br>&nbsp; &nbsp; LPSTARTUPINFO lpStartupInfo, // pointer to STARTUPINFO <br>&nbsp; &nbsp; LPPROCESS_INFORMATION lpProcessInformation // pointer to PROCESS_INFORMATION &nbsp;<br>&nbsp; &nbsp;); <br>&nbsp;<br><br>Parameters<br><br>lpApplicationName<br><br>Pointer to a null-terminated string that specifies the module to execute.<br><br>The string can specify the full path and filename of the module to execute. <br>The string can specify a partial name. In that case, the function uses the current drive and current directory to complete the specification. <br>The lpApplicationName parameter can be NULL. In that case, the module name must be the first white space-delimited token in the lpCommandLine string. <br>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. <br><br>Windows NT: If the executable module is a 16-bit application, lpApplicationName should be NULL, and the string pointed to by lpCommandLine should specify the executable module. A 16-bit application is one that executes as a VDM or WOW process. &nbsp;<br><br>lpCommandLine<br><br>Pointer to a null-terminated string that specifies the command line to execute. <br><br>The lpCommandLine parameter can be NULL. In that case, the function uses the string pointed to by lpApplicationName as the command line. <br>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. <br><br>If lpApplicationName is NULL, the first white space-delimited token of the command line specifies the module name. 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, Windows searches for the executable file in the following sequence: <br><br>1. The directory from which the application loaded. <br>2. The current directory for the parent process. <br>3. Windows 95: The Windows system directory. Use the GetSystemDirectory function to get the path of this directory.<br><br>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.<br><br>4. 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.<br>5. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory. <br>6. The directories that are listed in the PATH environment variable. <br><br>&nbsp;<br><br>If the process to be created is an MS-DOS - based or 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. <br><br>lpProcessAttributes<br><br>Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpProcessAttributes is NULL, the handle cannot be inherited. <br><br>Windows NT: The lpSecurityDescriptor member of the structure specifies a security descriptor for the new process. If lpProcessAttributes is NULL, the process gets a default security descriptor. <br>Windows 95: The lpSecurityDescriptor member of the structure is ignored.<br><br>lpThreadAttributes<br><br>Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpThreadAttributes is NULL, the handle cannot be inherited. <br><br>Windows NT: The lpSecurityDescriptor member of the structure specifies a security descriptor for the main thread. If lpThreadAttributes is NULL, the thread gets a default security descriptor. <br>Windows 95: The lpSecurityDescriptor member of the structure is ignored.<br><br>bInheritHandles<br><br>Indicates whether the new process inherits handles from the calling process. If TRUE, each inheritable open handle in the calling process is inherited by the new process. Inherited handles have the same value and access privileges as the original handles. <br><br>dwCreationFlags<br><br>Specifies additional flags that control the priority class and the creation of the process. The following creation flags can be specified in any combination, except as noted: <br><br>Value Meaning<br>CREATE_DEFAULT_ERROR_MODE <br>The new process does not inherit the error mode of the calling process. Instead, CreateProcess gives the new process the current default error mode. An application sets the current default error mode by calling SetErrorMode.This flag is particularly useful for multi-threaded shell applications that run with hard errors disabled. The default behavior for CreateProcess is for the new process to inherit the error mode of the caller. Setting this flag changes that default behavior.<br>CREATE_NEW_CONSOLE <br>The new process has a new console, instead of inheriting the parent's console. This flag cannot be used with the DETACHED_PROCESS flag.<br>CREATE_NEW_PROCESS_GROUP <br>The new process is the root process of a new process group. The process group includes all processes that are descendants of this root process. The process identifier of the new process group is the same as the process identifier, which is returned in the lpProcessInformation parameter. Process groups are used by the GenerateConsoleCtrlEvent function to enable sending a CTRL+C or CTRL+BREAK signal to a group of console processes.<br>CREATE_SEPARATE_WOW_VDM <br>Windows NT only: This flag is valid only when starting a 16-bit Windows-based application. If set, the new process is run in a private Virtual DOS Machine (VDM). By default, all 16-bit Windows-based applications are run as threads in a single, shared VDM. The advantage of running separately is that a crash only kills the single VDM; any other programs running in distinct VDMs continue to function normally. Also, 16-bit Windows-based applications that are run in separate VDMs have separate input queues. That means that if one application hangs momentarily, applications in separate VDMs continue to receive input.<br>CREATE_SHARED_WOW_VDM <br>Windows NT only: The flag is valid only when starting a 16-bit Windows-based application. If the DefaultSeparateVDM switch in the Windows section of WIN.INI is TRUE, this flag causes the CreateProcess function to override the switch and run the new process in the shared Virtual DOS Machine.<br>CREATE_SUSPENDED <br>The primary thread of the new process is created in a suspended state, and does not run until the ResumeThread function is called.<br>CREATE_UNICODE_ENVIRONMENT <br>If set, the environment block pointed to by lpEnvironment uses Unicode characters. If clear, the environment block uses ANSI characters.<br>DEBUG_PROCESS <br>If this flag is set, the calling process is treated as a debugger, and the new process is a process being debugged. The system notifies the debugger of all debug events that occur in the process being debugged.If you create a process with this flag set, only the calling thread (the thread that called CreateProcess) can call the WaitForDebugEvent function.<br>DEBUG_ONLY_THIS_PROCESS <br>If not set and the calling process is being debugged, the new process becomes another process being debugged by the calling process's debugger. If the calling process is not a process being debugged, no debugging-related actions occur.<br>DETACHED_PROCESS <br>For console processes, the new process does not have access to the console of the parent process. The new process can call the AllocConsole function at a later time to create a new console. This flag cannot be used with the CREATE_NEW_CONSOLE flag.<br>&nbsp;<br><br>The dwCreationFlags parameter also controls the new process's priority class, which is used in determining the scheduling priorities of the process's threads. If none of the following priority class flags is specified, the priority class defaults to NORMAL_PRIORITY_CLASS unless the priority class of the creating process is IDLE_PRIORITY_CLASS. In this case the default priority class of the child process is IDLE_PRIORITY_CLASS. One of the following flags can be specified: <br><br>Priority Meaning<br>HIGH_PRIORITY_CLASS Indicates a process that performs time-critical tasks that must be executed immediately for it to run correctly. The threads of a high-priority class process preempt the threads of normal-priority or idle-priority class processes. An example is Windows Task List, which must respond quickly when called by the user, regardless of the load on the operating system. Use extreme care when using the high-priority class, because a high-priority class CPU-bound application can use nearly all available cycles.<br>IDLE_PRIORITY_CLASS Indicates a process whose threads run only when the system is idle and are preempted by the threads of any process running in a higher priority class. An example is a screen saver. The idle priority class is inherited by child processes.<br>NORMAL_PRIORITY_CLASS Indicates a normal process with no special scheduling needs.<br>REALTIME_PRIORITY_CLASS Indicates a process that has the highest possible priority. The threads of a real-time priority class process preempt the threads of all other processes, including operating system processes performing important tasks. For example, a real-time process that executes for more than a very brief interval can cause disk caches not to flush or cause the mouse to be unresponsive.<br>&nbsp;<br><br>lpEnvironment<br><br>Points to an environment block for the new process. If this parameter is NULL, the new process uses the environment of the calling process. <br><br>An environment block consists of a null-terminated block of null-terminated strings. Each string is in the form: <br><br>name=value &nbsp;<br>&nbsp;<br><br>Because the equal sign is used as a separator, it must not be used in the name of an environment variable. <br>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. <br>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.<br><br>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.<br><br>lpCurrentDirectory<br><br>Points 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. <br><br>lpStartupInfo<br><br>Points to a STARTUPINFO structure that specifies how the main window for the new process should appear. <br><br>lpProcessInformation<br><br>Points to a PROCESS_INFORMATION structure that receives identification information about the new process. <br><br>&nbsp;<br><br>Return Values<br><br>If the function succeeds, the return value is nonzero.<br>If the function fails, the return value is zero. To get extended error information, call GetLastError. <br><br>Remarks<br><br>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. <br>In addition to creating a process, CreateProcess also creates a thread object. The thread is created with an initial stack whose size is described in the image header of the specified program's executable file. The thread begins execution at the image's entry point. <br><br>The new process and the new thread handles are created with full access rights. For either handle, if a security descriptor is not provided, the handle can be used in any function that requires an object handle of that type. When a security descriptor is provided, an access check is performed on all subsequent uses of the handle before access is granted. If the access check denies access, the requesting process is not able to use the handle to gain access to the thread. <br><br>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. <br><br>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. <br>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. <br><br>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). <br><br>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: <br><br>?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. <br>?Only one thread in a process can be in a DLL initialization or detach routine at a time. <br>?The ExitProcess function does not return until no threads are in their DLL initialization or detach routines. <br><br>&nbsp;<br><br>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. <br>When the last thread in a process terminates, the following events occur: <br><br>?All objects opened by the process are implicitly closed. <br>?The process's termination status (which is returned by GetExitCodeProcess) changes from its initial value of STILL_ACTIVE to the termination status of the last thread to terminate. <br>?The thread object of the main thread is set to the signaled state, satisfying any threads that were waiting on the object. <br>?The process object is set to the signaled state, satisfying any threads that were waiting on the object. <br><br>&nbsp;<br><br>If the current directory on drive C is /MSVC/MFC, there is an environment variable called =C: whose value is C:/MSVC/MFC. As noted in the previous description of lpEnvironment, such current directory information for a system's drives does not automatically propagate to a new process when the CreateProcess function's lpEnvironment parameter is non-NULL. An application must manually pass the current directory information to the new process. To do so, the application must explicitly create the =X environment variable strings, get them into alphabetical order (because Windows NT and Windows 95 use a sorted environment), and then put them into the environment block specified by lpEnvironment. Typically, they will go at the front of the environment block, due to the previously mentioned environment block sorting. <br><br>One way to obtain the current directory variable for a drive X is to call GetFullPathName("X:",. .)<br>. That avoids an application having to scan the environment block. If the full path returned is X:/, there is no need to pass that value on as environment data, since the root directory is the default current directory for drive X of a new process. <br>The handle returned by the CreateProcess function has PROCESS_ALL_ACCESS access to the process object. <br><br>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.<br>Windows NT: When a process is created with CREATE_NEW_PROCESS_GROUP specified, an implicit call to SetConsoleCtrlHandler(NULL,TRUE) is made on behalf of the new process; this means that the new process has CTRL+C disabled. This lets good shells handle CTRL+C themselves, and selectively pass that signal on to sub-processes. CTRL+BREAK is not disabled, and may be used to interrupt the process/process group. <br><br><br><br>
 
我做了两个程序,都用的是CreateProcess,都可以运行指定的文件。但区别是,一个<br>是程序内置的待运行文件,另一个是从别的地方拖拽过来的。内置的那个CreateProcess<br>以后不会退出,使用拖拽的只要有文件拖进来,就运行那个文件,然后自己退出来了。<br><br>两个程序的CreateProcess代码都是一样的。
 
我用CreateProcess的原因是它可以运行非EXE扩展名的程序。比如说.bin。<br>当然如果别的函数也能有这个功能那也行……
 
shellexecute也可以运行非exe文件的(调用默认处理程序)
 
我把代码贴上来。<br>BinL.dpr:<br>procedure TfmMain.Launch(App:string);<br>var<br> si:TSTARTUPINFO;<br> pi:TPROCESSINFORMATION;<br>begin<br> si.cb:=Sizeof(si);<br> si.lpReserved:=nil;<br> si.lpDesktop:=nil;<br> si.lpTitle:=nil;<br> si.dwFlags:=STARTF_USESHOWWINDOW and STARTF_USESTDHANDLES;<br> si.cbReserved2:=0;<br> si.lpReserved2:=nil;<br> CreateProcess(PChar(App), nil, nil, nil, true, CREATE_DEFAULT_ERROR_MODE and NORMAL_PRIORITY_CLASS, nil, PChar(ExtractFilePath(App)), si,pi);<br>end;<br><br>procedure TfmMain.FormCreate(Sender: TObject);<br>begin<br> DragAcceptFiles(Handle,True);<br>end;<br><br>procedure TfmMain.DragDropFiles(var msg: TMessage);<br>var<br> char1:array[0..128]of char; // 缓冲区<br> char2:pchar;<br> Sum:Integer;<br>begin<br>// sum:=DragQueryFile(msg.WParam,$FFFFFFFF,nil,0);<br>// if sum&gt;0 then<br>// begin<br> DragQueryFile(msg.WParam,0,char2,128);<br> App:=char2;<br>// end;<br> DragFinish(msg.WParam);<br> Launch(App);<br>end ;<br>窗体上还有一个TImage组件,Align=alClient,拖放的目标就是那个图像Image。<br>问题就是一有程序拖进来BinL就先运行程序然后自己退出。<br><br>远帆你的意思我明白,但是我要运行的是*.bin,系统一般没有关联的。<br>有的也是关联到某些映像程序上了。
 
后退
顶部