windows进程函数介绍【三】

来源:互联网 发布:西宁数据库工程师招聘 编辑:程序博客网 时间:2024/05/16 11:01

STARTUPINFO Structure

Visual Studio 2010

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

Syntax

C++
View ColorizedCopy to ClipboardPrint
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;
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;

Members

cb

The size of the structure, in bytes.

lpReserved

Reserved; must be NULL.

lpDesktop

The name of the desktop, or the name of both the desktop and window station for this process. A backslash in the string indicates that the string includes both the desktop and window station names. For more information, seeThread Connection to a Desktop.

lpTitle

For console processes, this is the title displayed in the title bar if a new console window is created. If NULL, the name of the executable file is used as the window title instead. This parameter must be NULL for GUI or console processes that do not create a new console window.

dwX

If dwFlags specifies STARTF_USEPOSITION, this member is the x offset of the upper left corner of a window if a new window is created, in pixels. Otherwise, this member is ignored.

The offset is from the upper left corner of the screen. For GUI processes, the specified position is used the first time the new process callsCreateWindow to create an overlapped window if the x parameter of CreateWindow is CW_USEDEFAULT.

dwY

If dwFlags specifies STARTF_USEPOSITION, this member is the y offset of the upper left corner of a window if a new window is created, in pixels. Otherwise, this member is ignored.

The offset is from the upper left corner of the screen. For GUI processes, the specified position is used the first time the new process calls CreateWindow to create an overlapped window if the y parameter of CreateWindow is CW_USEDEFAULT.

dwXSize

If dwFlags specifies STARTF_USESIZE, this member is the width of the window if a new window is created, in pixels. Otherwise, this member is ignored.

For GUI processes, this is used only the first time the new process calls CreateWindow to create an overlapped window if the nWidth parameter of CreateWindow is CW_USEDEFAULT.

dwYSize

If dwFlags specifies STARTF_USESIZE, this member is the height of the window if a new window is created, in pixels. Otherwise, this member is ignored.

For GUI processes, this is used only the first time the new process calls CreateWindow to create an overlapped window if the nHeight parameter of CreateWindow is CW_USEDEFAULT.

dwXCountChars

If dwFlags specifies STARTF_USECOUNTCHARS, if a new console window is created in a console process, this member specifies the screen buffer width, in character columns. Otherwise, this member is ignored.

dwYCountChars

If dwFlags specifies STARTF_USECOUNTCHARS, if a new console window is created in a console process, this member specifies the screen buffer height, in character rows. Otherwise, this member is ignored.

dwFillAttribute

If dwFlags specifies STARTF_USEFILLATTRIBUTE, this member is the initial text and background colors if a new console window is created in a console application. Otherwise, this member is ignored.

This value can be any combination of the following values: FOREGROUND_BLUE, FOREGROUND_GREEN, FOREGROUND_RED, FOREGROUND_INTENSITY, BACKGROUND_BLUE, BACKGROUND_GREEN, BACKGROUND_RED, and BACKGROUND_INTENSITY. For example, the following combination of values produces red text on a white background:

FOREGROUND_RED| BACKGROUND_RED| BACKGROUND_GREEN| BACKGROUND_BLUE

dwFlags

A bit field that determines whether certain STARTUPINFO members are used when the process creates a window. This member can be one or more of the following values.

ValueMeaning
STARTF_FORCEONFEEDBACK
0x00000040

Indicates that the cursor is in feedback mode for two seconds after CreateProcess is called. The Working in Background cursor is displayed (see the Pointers tab in the Mouse control panel utility).

If during those two seconds the process makes the first GUI call, the system gives five more seconds to the process. If during those five seconds the process shows a window, the system gives five more seconds to the process to finish drawing the window.

The system turns the feedback cursor off after the first call to GetMessage, regardless of whether the process is drawing.

STARTF_FORCEOFFFEEDBACK
0x00000080

Indicates that the feedback cursor is forced off while the process is starting. The Normal Select cursor is displayed.

STARTF_PREVENTPINNING
0x00002000

Indicates that any windows created by the process cannot be pinned on the taskbar.

This flag must be combined with STARTF_TITLEISAPPID.

STARTF_RUNFULLSCREEN
0x00000020

Indicates that the process should be run in full-screen mode, rather than in windowed mode.

This flag is only valid for console applications running on an x86 computer.

STARTF_TITLEISAPPID
0x00001000

The lpTitle member contains an AppUserModelID. This identifier controls how the taskbar and Start menu present the application, and enables it to be associated with the correct shortcuts and Jump Lists. Generally, applications will use theSetCurrentProcessExplicitAppUserModelID and GetCurrentProcessExplicitAppUserModelID functions instead of setting this flag. For more information, seeApplication User Model IDs.

If STARTF_PREVENTPINNING is used, application windows cannot be pinned on the taskbar. The use of any AppUserModelID-related window properties by the application overrides this setting for that window only.

This flag cannot be used with STARTF_TITLEISLINKNAME.

STARTF_TITLEISLINKNAME
0x00000800

The lpTitle member contains the path of the shortcut file (.lnk) that the user invoked to start this process. This is typically set by the shell when a .lnk file pointing to the launched application is invoked. Most applications will not need to set this value.

This flag cannot be used with STARTF_TITLEISAPPID.

STARTF_USECOUNTCHARS
0x00000008

The dwXCountChars and dwYCountChars members contain additional information.

STARTF_USEFILLATTRIBUTE
0x00000010

The dwFillAttribute member contains additional information.

STARTF_USEHOTKEY
0x00000200

The hStdInput member contains additional information.

This flag cannot be used with STARTF_USESTDHANDLES.

STARTF_USEPOSITION
0x00000004

The dwX and dwY members contain additional information.

STARTF_USESHOWWINDOW
0x00000001

The wShowWindow member contains additional information.

STARTF_USESIZE
0x00000002

The dwXSize and dwYSize members contain additional information.

STARTF_USESTDHANDLES
0x00000100

The hStdInput, hStdOutput, and hStdError members contain additional information.

If this flag is specified when calling one of the process creation functions, the handles must be inheritable and the function's bInheritHandles parameter must be set to TRUE. For more information, seeHandle Inheritance.

If this flag is specified when calling the GetStartupInfo function, these members are either the handle value specified during process creation or INVALID_HANDLE_VALUE.

This flag cannot be used with STARTF_USEHOTKEY.

 

wShowWindow

If dwFlags specifies STARTF_USESHOWWINDOW, this member can be any of the values that can be specified in the nCmdShow parameter for theShowWindow function, except for SW_SHOWDEFAULT. Otherwise, this member is ignored.

For GUI processes, the first time ShowWindow is called, its nCmdShow parameter is ignored wShowWindow specifies the default value. In subsequent calls to ShowWindow, the wShowWindow member is used if the nCmdShow parameter of ShowWindow is set to SW_SHOWDEFAULT.

cbReserved2

Reserved for use by the C Run-time; must be zero.

lpReserved2

Reserved for use by the C Run-time; must be NULL.

hStdInput

If dwFlags specifies STARTF_USESTDHANDLES, this member is the standard input handle for the process. If STARTF_USESTDHANDLES is not specified, the default for standard input is the keyboard buffer.

If dwFlags specifies STARTF_USEHOTKEY, this member specifies a hotkey value that is sent as the wParam parameter of aOnlineWM_SETHOTKEY message to the first eligible top-level window created by the application that owns the process. If the window is created with the WS_POPUP window style, it is not eligible unless the WS_EX_APPWINDOW extended window style is also set. For more information, see OnlineCreateWindowEx.

Otherwise, this member is ignored.

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 theShowWindow 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.

If a GUI process is being started and neither STARTF_FORCEONFEEDBACK or STARTF_FORCEOFFFEEDBACK is specified, the process feedback cursor is used. A GUI process is one whose subsystem is specified as "windows."



DuplicateHandle Function

Visual Studio 2010

Duplicates an object handle.

Syntax

C++
View ColorizedCopy to ClipboardPrint
BOOL WINAPI DuplicateHandle(  __in   HANDLE hSourceProcessHandle,  __in   HANDLE hSourceHandle,  __in   HANDLE hTargetProcessHandle,  __out  LPHANDLE lpTargetHandle,  __in   DWORD dwDesiredAccess,  __in   BOOL bInheritHandle,  __in   DWORD dwOptions);
BOOL WINAPI DuplicateHandle(  __in   HANDLE hSourceProcessHandle,  __in   HANDLE hSourceHandle,  __in   HANDLE hTargetProcessHandle,  __out  LPHANDLE lpTargetHandle,  __in   DWORD dwDesiredAccess,  __in   BOOL bInheritHandle,  __in   DWORD dwOptions);

Parameters

hSourceProcessHandle [in]

A handle to the process with the handle to be duplicated.

The handle must have the PROCESS_DUP_HANDLE access right. For more information, seeProcess Security and Access Rights.

hSourceHandle [in]

The handle to be duplicated. This is an open object handle that is valid in the context of the source process. For a list of objects whose handles can be duplicated, see the following Remarks section.

hTargetProcessHandle [in]

A handle to the process that is to receive the duplicated handle. The handle must have the PROCESS_DUP_HANDLE access right.

lpTargetHandle [out]

A pointer to a variable that receives the duplicate handle. This handle value is valid in the context of the target process.

If hSourceHandle is a pseudo handle returned by GetCurrentProcess or GetCurrentThread, DuplicateHandle converts it to a real handle to a process or thread, respectively.

If lpTargetHandle is NULL, the function duplicates the handle, but does not return the duplicate handle value to the caller. This behavior exists only for backward compatibility with previous versions of this function. You should not use this feature, as you will lose system resources until the target process terminates.

dwDesiredAccess [in]

The access requested for the new handle. For the flags that can be specified for each object type, see the following Remarks section.

This parameter is ignored if the dwOptions parameter specifies the DUPLICATE_SAME_ACCESS flag. Otherwise, the flags that can be specified depend on the type of object whose handle is to be duplicated.

bInheritHandle [in]

A variable that indicates whether the handle is inheritable. If TRUE, the duplicate handle can be inherited by new processes created by the target process. If FALSE, the new handle cannot be inherited.

dwOptions [in]

Optional actions. This parameter can be zero, or any combination of the following values.

ValueMeaning
DUPLICATE_CLOSE_SOURCE
0x00000001

Closes the source handle. This occurs regardless of any error status returned.

DUPLICATE_SAME_ACCESS
0x00000002

Ignores the dwDesiredAccess parameter. The duplicate handle has the same access as the source handle.

 

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, callGetLastError.

Remarks

The duplicate handle refers to the same object as the original handle. Therefore, any changes to the object are reflected through both handles. For example, if you duplicate a file handle, the current file position is always the same for both handles. For file handles to have different file positions, use the CreateFile function to create file handles that share access to the same file.

DuplicateHandle can be called by either the source process or the target process (or a process that is both the source and target process). For example, a process can useDuplicateHandle to create a noninheritable duplicate of an inheritable handle, or a handle with different access than the original handle.

The source process uses the GetCurrentProcess function to get a handle to itself. This handle is a pseudo handle, butDuplicateHandle converts it to a real process handle. To get the target process handle, it may be necessary to use some form of interprocess communication (for example, a named pipe or shared memory) to communicate the process identifier to the source process. The source process can use this identifier in the OpenProcess function to obtain a handle to the target process.

If the process that calls DuplicateHandle is not also the target process, the source process must use interprocess communication to pass the value of the duplicate handle to the target process.

DuplicateHandle can be used to duplicate a handle between a 32-bit process and a 64-bit process. The resulting handle is appropriately sized to work in the target process. For more information, seeProcess Interoperability.

DuplicateHandle can duplicate handles to the following types of objects.

ObjectDescriptionAccess tokenThe handle is returned by the CreateRestrictedToken, DuplicateToken, DuplicateTokenEx, OpenProcessToken, or OpenThreadToken function.Change notificationThe handle is returned by the FindFirstChangeNotification function.Communications deviceThe handle is returned by the CreateFile function.Console inputThe handle is returned by the CreateFile function when CONIN$ is specified, or by theGetStdHandle function when STD_INPUT_HANDLE is specified. Console handles can be duplicated for use only in the same process.Console screen bufferThe handle is returned by the CreateFile function when CONOUT$ is specified, or by theGetStdHandle function when STD_OUTPUT_HANDLE is specified. Console handles can be duplicated for use only in the same process.DesktopThe handle is returned by the GetThreadDesktop function.EventThe handle is returned by the CreateEvent or OpenEvent function.FileThe handle is returned by the CreateFile function.File mappingThe handle is returned by the CreateFileMapping function.JobThe handle is returned by the CreateJobObject function.MailslotThe handle is returned by the CreateMailslot function.MutexThe handle is returned by the CreateMutex or OpenMutex function.PipeA named pipe handle is returned by the CreateNamedPipe or CreateFile function. An anonymous pipe handle is returned by theCreatePipe function.ProcessThe handle is returned by the CreateProcess, GetCurrentProcess, or OpenProcess function.Registry keyThe handle is returned by the RegCreateKey, RegCreateKeyEx, RegOpenKey, or RegOpenKeyEx function. Note that registry key handles returned by theRegConnectRegistry function cannot be used in a call to DuplicateHandle.SemaphoreThe handle is returned by the CreateSemaphore or OpenSemaphore function.ThreadThe handle is returned by the CreateProcess, CreateThread, CreateRemoteThread, or GetCurrentThread functionTimerThe handle is returned by the CreateWaitableTimer or OpenWaitableTimer function.TransactionThe handle is returned by the CreateTransaction function.Window stationThe handle is returned by the GetProcessWindowStation function.

 

You should not use DuplicateHandle to duplicate handles to the following objects:

  • I/O completion ports. No error is returned, but the duplicate handle cannot be used.
  • Sockets. No error is returned, but the duplicate handle may not be recognized by Winsock at the target process. Also, usingDuplicateHandle interferes with internal reference counting on the underlying object. To duplicate a socket handle, use theWSADuplicateSocket function.

The dwDesiredAccess parameter specifies the new handle's access rights. All objects support thestandard access rights. Objects may also support additional access rights depending on the object type. For more information, see the following topics:

  • Desktop Security and Access Rights
  • File Security and Access Rights
  • File-Mapping Security and Access Rights
  • Job Object Security and Access Rights
  • Process Security and Access Rights
  • Registry Key Security and Access Rights
  • Synchronization Object Security and Access Rights
  • Thread Security and Access Rights
  • Window-Station Security and Access Rights

In some cases, the new handle can have more access rights than the original handle. However, in other cases,DuplicateHandle cannot create a handle with more access rights than the original. For example, a file handle created with the GENERIC_READ access right cannot be duplicated so that it has both the GENERIC_READ and GENERIC_WRITE access right.

Normally the target process closes a duplicated handle when that process is finished using the handle. To close a duplicated handle from the source process, callDuplicateHandle with the following parameters:

  • Set hSourceProcessHandle to the target process from the DuplicateHandle call that created the handle.
  • Set hSourceHandle to the duplicated handle to close.
  • Set lpTargetHandle to NULL.
  • Set dwOptions to DUPLICATE_CLOSE_SOURCE.

Examples

The following example creates a mutex, duplicates a handle to the mutex, and passes it to another thread. Duplicating the handle ensures that the reference count is increased so that the mutex object will not be destroyed until both threads have closed the handle.

View ColorizedCopy to ClipboardPrint
#include <windows.h>    DWORD CALLBACK ThreadProc(PVOID pvParam);    int main()  {      HANDLE hMutex = CreateMutex(NULL, FALSE, NULL);      HANDLE hMutexDup, hThread;      DWORD dwThreadId;        DuplicateHandle(GetCurrentProcess(),                       hMutex,                       GetCurrentProcess(),                      &hMutexDup,                       0,                      FALSE,                      DUPLICATE_SAME_ACCESS);        hThread = CreateThread(NULL, 0, ThreadProc,           (LPVOID) hMutexDup, 0, &dwThreadId);        // Perform work here, closing the handle when finished with the      // mutex. If the reference count is zero, the object is destroyed.      CloseHandle(hMutex);        // Wait for the worker thread to terminate and clean up.      WaitForSingleObject(hThread, INFINITE);      CloseHandle(hThread);      return 0;  }    DWORD CALLBACK ThreadProc(PVOID pvParam)  {      HANDLE hMutex = (HANDLE)pvParam;        // Perform work here, closing the handle when finished with the      // mutex. If the reference count is zero, the object is destroyed.      CloseHandle(hMutex);      return 0;  }  
#include <windows.h>DWORD CALLBACK ThreadProc(PVOID pvParam);int main(){    HANDLE hMutex = CreateMutex(NULL, FALSE, NULL);    HANDLE hMutexDup, hThread;    DWORD dwThreadId;    DuplicateHandle(GetCurrentProcess(),                     hMutex,                     GetCurrentProcess(),                    &hMutexDup,                     0,                    FALSE,                    DUPLICATE_SAME_ACCESS);    hThread = CreateThread(NULL, 0, ThreadProc,         (LPVOID) hMutexDup, 0, &dwThreadId);    // Perform work here, closing the handle when finished with the    // mutex. If the reference count is zero, the object is destroyed.    CloseHandle(hMutex);    // Wait for the worker thread to terminate and clean up.    WaitForSingleObject(hThread, INFINITE);    CloseHandle(hThread);    return 0;}DWORD CALLBACK ThreadProc(PVOID pvParam){    HANDLE hMutex = (HANDLE)pvParam;    // Perform work here, closing the handle when finished with the    // mutex. If the reference count is zero, the object is destroyed.    CloseHandle(hMutex);    return 0;}


GetStdHandle Function

Visual Studio 2010

Retrieves a handle to the specified standard device (standard input, standard output, or standard error).

Syntax

C++
View ColorizedCopy to ClipboardPrint
HANDLE WINAPI GetStdHandle(  __in  DWORD nStdHandle);
HANDLE WINAPI GetStdHandle(  __in  DWORD nStdHandle);

Parameters

nStdHandle [in]

The standard device. This parameter can be one of the following values.

ValueMeaning
STD_INPUT_HANDLE
(DWORD)-10

The standard input device. Initially, this is the console input buffer, CONIN$.

STD_OUTPUT_HANDLE
(DWORD)-11

The standard output device. Initially, this is the active console screen buffer, CONOUT$.

STD_ERROR_HANDLE
(DWORD)-12

The standard error device. Initially, this is the active console screen buffer, CONOUT$.

 

Return Value

If the function succeeds, the return value is a handle to the specified device, or a redirected handle set by a previous call toSetStdHandle. The handle has GENERIC_READ and GENERIC_WRITE access rights, unless the application has usedSetStdHandle to set a standard handle with lesser access.

If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, callGetLastError.

If an application does not have associated standard handles, such as a service running on an interactive desktop, and has not redirected them, the return value is NULL.

Remarks

Handles returned by GetStdHandle can be used by applications that need to read from or write to the console. When a console is created, the standard input handle is a handle to the console's input buffer, and the standard output and standard error handles are handles of the console's active screen buffer. These handles can be used by theReadFile and WriteFile functions, or by any of the console functions that access the console input buffer or a screen buffer (for example, theReadConsoleInput, WriteConsole, or GetConsoleScreenBufferInfo functions).

The standard handles of a process may be redirected by a call to SetStdHandle, in which caseGetStdHandle returns the redirected handle. If the standard handles have been redirected, you can specify the CONIN$ value in a call to theCreateFile function to get a handle to a console's input buffer. Similarly, you can specify the CONOUT$ value to get a handle to a console's active screen buffer.




原创粉丝点击