zoukankan      html  css  js  c++  java
  • Windows API 之 GetStartupInfo 、CreateProcess

    GetStartupInfo

    参考:https://msdn.microsoft.com/en-us/library/windows/desktop/ms683230%28v=vs.85%29.aspx

    Retrieves the contents of the STARTUPINFO structure that was specified when the calling process was created.

    VOID WINAPI GetStartupInfo(
      _Out_ LPSTARTUPINFO lpStartupInfo
    );

    Return value

    This function does not return a value.

    If an error occurs, the ANSI version of this function (GetStartupInfoA) can raise an exception. The Unicode version (GetStartupInfoW) does not fail.

    Remarks

    The STARTUPINFO structure was specified by the process that created the calling process. It can be used to specify properties associated with the main window of the calling process.

    StartUpInfo structure

    参考:https://msdn.microsoft.com/en-us/library/windows/desktop/ms686331%28v=vs.85%29.aspx

    Specifies the window station, desktop, standard handles, and appearance of the main window for a process at creation time.

    typedef struct _STARTUPINFO {
      DWORD  cb;
      LPTSTR lpReserved;
      LPTSTR lpDesktop;
      LPTSTR lpTitle;
      DWORD  dwX;
      DWORD  dwY;
      DWORD  dwXSize;
      DWORD  dwYSize;
      DWORD  dwXCountChars;
      DWORD  dwYCountChars;
      DWORD  dwFillAttribute;
      DWORD  dwFlags;
      WORD   wShowWindow;
      WORD   cbReserved2;
      LPBYTE lpReserved2;
      HANDLE hStdInput;
      HANDLE hStdOutput;
      HANDLE hStdError;
    } STARTUPINFO, *LPSTARTUPINFO;
    hStdOutput

    If dwFlags specifies STARTF_USESTDHANDLES, this member is the standard output handle for the process. Otherwise, this member is ignored and the default for standard output is the console window's buffer.

    hStdError

    If dwFlags specifies STARTF_USESTDHANDLES, this member is the standard error handle for the process. Otherwise, this member is ignored and the default for standard error is the console window's buffer.

    Remarks

    For graphical user interface (GUI) processes, this information affects the first window created by the CreateWindow function and shown by the ShowWindow function. For console processes, this information affects the console window if a new console is created for the process. A process can use the GetStartupInfo function to retrieve the STARTUPINFO structure specified when the process was created.

    CreateProcess

    参考:https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85%29.aspx

    Creates a new process and its primary thread. The new process runs in the security context of the calling process.

    The CreateProcess function creates a new process, which runs independently of the creating process. However, for simplicity, the relationship is referred to as a parent-child relationship.

    BOOL WINAPI CreateProcess(
      _In_opt_    LPCTSTR               lpApplicationName,
      _Inout_opt_ LPTSTR                lpCommandLine,
      _In_opt_    LPSECURITY_ATTRIBUTES lpProcessAttributes,
      _In_opt_    LPSECURITY_ATTRIBUTES lpThreadAttributes,
      _In_        BOOL                  bInheritHandles,
      _In_        DWORD                 dwCreationFlags,
      _In_opt_    LPVOID                lpEnvironment,
      _In_opt_    LPCTSTR               lpCurrentDirectory,
      _In_        LPSTARTUPINFO         lpStartupInfo,
      _Out_       LPPROCESS_INFORMATION lpProcessInformation
    );
    lpProcessAttributes [in, optional]

    A pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle to the new process object can be inherited by child processes. If lpProcessAttributes is NULL, the handle cannot be inherited.

    lpThreadAttributes [in, optional]

    A pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle to the new thread object can be inherited by child processes. If lpThreadAttributes is NULL, the handle cannot be inherited.

    example:

    #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]
    ", argv[0]);
            return;
        }
    
        // Start the child process. 
        if( !CreateProcess( NULL,   // No module name (use command line)
            argv[1],        // 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).
    ", GetLastError() );
            return;
        }
    
        // Wait until child process exits.
        WaitForSingleObject( pi.hProcess, INFINITE );
    
        // Close process and thread handles. 
        CloseHandle( pi.hProcess );
        CloseHandle( pi.hThread );
    }

    If CreateProcess succeeds, it returns a PROCESS_INFORMATION structure containing handles and identifiers for the new process and its primary thread. The thread and process handles are created with full access rights, although access can be restricted if you specify security descriptors. When you no longer need these handles, close them by using the CloseHandle function.

    Return value

    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.

    Security Remarks

    The first parameter, lpApplicationName, can be NULL, in which case the executable name must be in the white space–delimited string pointed to by lpCommandLine. If the executable or path name has a space in it, there is a risk that a different executable could be run because of the way the function parses spaces. The following example is dangerous because the function will attempt to run "Program.exe", if it exists, instead of "MyApp.exe".

     
    	LPTSTR szCmdline = _tcsdup(TEXT("C:\Program Files\MyApp -L -S"));
    	CreateProcess(NULL, szCmdline, /* ... */);
    

    If a malicious user were to create an application called "Program.exe" on a system, any program that incorrectly calls CreateProcess using the Program Files directory will run this application instead of the intended application.

    To avoid this problem, do not pass NULL for lpApplicationName. If you do pass NULL for lpApplicationName, use quotation marks around the executable path in lpCommandLine, as shown in the example below.

     
    	LPTSTR szCmdline[] = _tcsdup(TEXT(""C:\Program Files\MyApp" -L -S"));
    	CreateProcess(NULL, szCmdline, /*...*/);
    
  • 相关阅读:
    工作的本质是思考
    V8、JSCore、Hermes、QuickJS,hybrid开发JS引擎怎么选
    Aspects框架的源码解读及问题解析
    饿了么移动APP的架构演进
    关键字:客户端架构演进
    以小见大,见微知著——亿万级APP架构演进之路
    理解 Swift:ObjectiveC 的构建管道
    MMKV 组件现在开源了
    进阶:iOS 性能优化系列
    你如果无法度量它,就无法管理它
  • 原文地址:https://www.cnblogs.com/predator-wang/p/4807173.html
Copyright © 2011-2022 走看看