控制台应用程序只运行一个实例,该怎么处理
控制台应用程序只运行一个实例
有一个控制台应用程序,通过CreateSemaphore()来检测是否有本程序的实例在运行,若已存在则退出。
现在想进一步,若已存在本程序的实例,则激活该实例(控制台应用程序窗口有激活的概念吗?)。
网上搜了一下,都是针对窗口应用程序的,不知道针对控制台应用程序是否存在类似的办法 谢谢
------解决方案--------------------
http://blog.****.net/luxiaoyu_sdc/article/details/6534783 --- 根据进程名获取句柄
SetWindowPos ---移动窗口。
没有实际试过,你可以试一下
------解决方案--------------------
SetForegroundWindow
The SetForegroundWindow function puts the thread that created the specified window into the foreground and activates the window. Keyboard input is directed to the window, and various visual cues are changed for the user. The system assigns a slightly higher priority to the thread that created the foreground window than it does to other threads.
BOOL SetForegroundWindow(
HWND hWnd // handle to window to bring to foreground
);
Parameters
hWnd
Handle to the window that should be activated and brought to the foreground.
Return Values
If the window was brought to the foreground, the return value is nonzero.
If the window was not brought to the foreground, the return value is zero.
Remarks
The foreground window is the window at the top of the Z order. It is the window that the user is working with. In a preemptive multitasking environment, you should generally let the user control which window is the foreground window.
Windows NT 5.0 and later: An application cannot force a window to the foreground while the user is working with another window. Instead, SetForegroundWindow will activate the window (see SetActiveWindow) and call theFlashWindowEx function to notify the user.
Windows CE: The thread that owns the window is not given a priority boost.
QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Header: Declared in winuser.h.
Import Library: Use user32.lib.
See Also
Windows Overview, Window Functions,FlashWindowEx, GetForegroundWindow, SetActiveWindow
SetActiveWindow
The SetActiveWindow function activates a window. The window must be associated with the calling thread's message queue.
HWND SetActiveWindow(
HWND hWnd // handle to window to activate
);
Parameters
hWnd
Handle to the top-level window to be activated.
Return Values
If the function succeeds, the return value is the handle to the window that was previously active.
If the function fails, the return value is NULL. To get extended error information, callGetLastError.
Remarks
The SetActiveWindow function activates a window, but not if the application is in the background. The window will be brought into the foreground (top of Z order) if the application is in the foreground when it sets the activation.
If the window identified by the hWnd parameter was created by the calling thread, the active window status of the calling thread is set to hWnd. Otherwise, the active window status of the calling thread is set to NULL.
The SetForegroundWindow window function, on the other hand, activates a window and forces it into the foreground. An application should only call SetForegroundWindow if it needs to display critical errors or information that needs the user's immediate attention .
By using theAttachThreadInput function, a thread can attach its input processing to another thread. This allows a thread to call SetActiveWindow to activate a window associated with another thread's message queue.
有一个控制台应用程序,通过CreateSemaphore()来检测是否有本程序的实例在运行,若已存在则退出。
现在想进一步,若已存在本程序的实例,则激活该实例(控制台应用程序窗口有激活的概念吗?)。
网上搜了一下,都是针对窗口应用程序的,不知道针对控制台应用程序是否存在类似的办法 谢谢
------解决方案--------------------
http://blog.****.net/luxiaoyu_sdc/article/details/6534783 --- 根据进程名获取句柄
SetWindowPos ---移动窗口。
没有实际试过,你可以试一下
------解决方案--------------------
SetForegroundWindow
The SetForegroundWindow function puts the thread that created the specified window into the foreground and activates the window. Keyboard input is directed to the window, and various visual cues are changed for the user. The system assigns a slightly higher priority to the thread that created the foreground window than it does to other threads.
BOOL SetForegroundWindow(
HWND hWnd // handle to window to bring to foreground
);
Parameters
hWnd
Handle to the window that should be activated and brought to the foreground.
Return Values
If the window was brought to the foreground, the return value is nonzero.
If the window was not brought to the foreground, the return value is zero.
Remarks
The foreground window is the window at the top of the Z order. It is the window that the user is working with. In a preemptive multitasking environment, you should generally let the user control which window is the foreground window.
Windows NT 5.0 and later: An application cannot force a window to the foreground while the user is working with another window. Instead, SetForegroundWindow will activate the window (see SetActiveWindow) and call theFlashWindowEx function to notify the user.
Windows CE: The thread that owns the window is not given a priority boost.
QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 1.0 or later.
Header: Declared in winuser.h.
Import Library: Use user32.lib.
See Also
Windows Overview, Window Functions,FlashWindowEx, GetForegroundWindow, SetActiveWindow
SetActiveWindow
The SetActiveWindow function activates a window. The window must be associated with the calling thread's message queue.
HWND SetActiveWindow(
HWND hWnd // handle to window to activate
);
Parameters
hWnd
Handle to the top-level window to be activated.
Return Values
If the function succeeds, the return value is the handle to the window that was previously active.
If the function fails, the return value is NULL. To get extended error information, callGetLastError.
Remarks
The SetActiveWindow function activates a window, but not if the application is in the background. The window will be brought into the foreground (top of Z order) if the application is in the foreground when it sets the activation.
If the window identified by the hWnd parameter was created by the calling thread, the active window status of the calling thread is set to hWnd. Otherwise, the active window status of the calling thread is set to NULL.
The SetForegroundWindow window function, on the other hand, activates a window and forces it into the foreground. An application should only call SetForegroundWindow if it needs to display critical errors or information that needs the user's immediate attention .
By using theAttachThreadInput function, a thread can attach its input processing to another thread. This allows a thread to call SetActiveWindow to activate a window associated with another thread's message queue.