VC 将CreateProcess 隐藏的窗口重新显示

STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory( &pi, sizeof(pi) );

// //隐藏进程窗口
si.dwFlags =STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;

// Start the child process.
if( !CreateProcess( NULL , // No module name (use command line).
exe,//process, // 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.
)
{
return FALSE;
}

以上是程序代码,通过
si.dwFlags =STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
这两个语句,我把进程窗口隐藏了,但是在以后的操作中我想重新显示这个进程的窗口,应该怎样实现呢?我试过加si.wShowWindow = SW_SHOW;或者将si.dwFlags =STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOW; 两句都加上,但是不能实现进程窗口的显示。
有正确答案的话我一定会追加三十分的。
现在我想问的是,如何得到我用CreateProcess创建这个进程程序的窗口句柄?只要回答这个问题就可以了。

没有直接的方法。用EnumWindows枚举窗口,然后用GetProcessIidFromHwnd获取窗口进程id,与你创建的进程id比较,直到相同,这个窗口就是你所要找的窗口。

以上函数可能有个别字符差异,我记不太清,没装msdn也没装vc,不能帮你查。总之就是枚举窗口,通过窗口句柄获取进程id,逐个与进程比较直到相同为止。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-12-28
直接对你的窗体发送一个按键的消息就行了。参考MSDN SendMessage。
第2个回答  2013-02-28
把CreateProcess之前的取反去掉,把return FALSE挪到else里,在if(..){}里添加如下代码:
Sleep(5000);
::EnumWindows(&EnumWindowsProc, pi.dwThreadId);//Iterate all windows
其中EnumWindowsProc是一个回调,具体实现如下:
int CALLBACK EnumWindowsProc(HWND hwnd, LPARAM param)
{
DWORD pID;
DWORD TpID = GetWindowThreadProcessId(hwnd, &pID);//get process id
if (TpID == (DWORD)param)
{
apphwnd=hwnd;//hwnd is the window handle
return false;
}
return true;
}
这里apphwnd之前定义好,枚举窗口结束时(就是return false),apphwnd就为CreateProcess的进程窗口句柄。
第3个回答  2009-12-29
EnumThreadWindows

VB声明
Declare Function EnumThreadWindows Lib "user32" Alias "EnumThreadWindows" (ByVal dwThreadId As Long, ByVal lpfn As Long, ByVal lParam As Long) As Long
说明
枚举与指定任务相关的窗口
返回值
Long,非零表示成功,零表示失败
参数表
参数 类型及说明
dwThreadId Long,某线程的标识符,它的窗口将被枚举
lpfn Long,指向一个函数的指针,要求为每个子窗口都调用这个函数。用AddressOf运算符获得函数在标准模式下的地址
lParam Long,在枚举期间,传递给dwcbkd32d.ocx定制控件之EnumWindows事件的值。这个值的含义是由程序员规定的
注解
子窗口下属的其他子窗口也可由这个函数枚举

'Enum Classnames
'in a form
Private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: [email protected]
Dim ThreadID As Long, ProcessID As Long ' receive id to thread and process of Form1
' Determine the thread which owns this window
ThreadID = GetWindowThreadProcessId(Me.hWnd, ProcessID)
' Use the callback function to list all of the enumerated thrad windows
EnumThreadWindows ThreadID, AddressOf EnumThreadWndProc, 0
'Show the results
Me.AutoRedraw = True
Me.Print sClasses
End Sub

'In a module
Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
Declare Function EnumThreadWindows Lib "user32" (ByVal dwThreadId As Long, ByVal lpfn As Long, ByVal lParam As Long) As Long
Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
'variable used to list all the classnames
Public sClasses As String
Public Function EnumThreadWndProc(ByVal hWnd As Long, ByVal lParam As Long) As Long
Dim Ret As Long, sText As String
'create a string-buffer
sText = Space(255)
'get the classname of the window handle
Ret = GetClassName(hWnd, sText, 255)
'cut off the unnecessary part of Chr$(0)'s
sText = Left$(sText, Ret)
'add this classname to the list of classnames
sClasses = sClasses + sText + vbCrLf
'continue the enumeration
EnumThreadWndProc = 1
End Function