win processes

Hi, how to hide/show minimize/maximize only specified processes/programs and not the own console, for example if i have 10processes running for the program test.exe how to hide/show minimize/maximize only those processes?
Last edited on
i have 10processes running for the program test.exe

Do you mean that test.exe launched 10 other exes?
Well yes and no, depends on your question meaning, my program is launching another program(test.exe) for 10times and in this way there are 10instances of test.exe
Last edited on
If the application (test.exe) is (a) a windows app (WinMain, message loop, etc) and (b) you launched with CreateProcess, it's easy enough.

Using the hThread obtained when you launch the app, use GetThreadId followed by GetGUIThreadInfo to get the handle to the apps window. Then use ShowWindow with SW_HIDE/SW_SHOW and SW_MINIMIZE/SW_MAXIMIZE

(This approach might fail if (e.g.) the app has more than one the message pump, but that's not the normal case.)

If test.exe is a console app it's not possible to use the hThread to obtain the handle as above as the console window is owned by an instance of cmd.exe CSRSS (the Client-Server Runtime SubSystem) [1], not the app (note: I'm a bit hazy about this reasoning, but the code does fail).

(Note that as of Windows 7, the CSRSS delegates the work to an auxillary process for security reasons)

But you could use the lpTitle of the STARTUPINFO to give each console instance a unique name. Then you can use FindWindow to obtain the required window handle and call ShowWindow on that.

(This same trick could be used with the windows apps if you wanted, but I'd use GetGUIThread, etc)

Andy

P.S. There might be a way to find the console window via a more circuitous, but I don't know it)

[1] Why aren't console windows themed on Windows XP?
http://blogs.msdn.com/b/oldnewthing/archive/2007/12/31/6909007.aspx
Last edited on
It's a console app. launched with ShellExecute(), could be possible please a little example for startupinfo - showwindow? I have searched on the msdn.microsoft for those functions but i'm still confused about how to do that.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// RunApp.cpp

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>

DWORD
RunApp(const char* appPath, const char* cmdLine)
{
    DWORD dwRet = NOERROR;

    char windowTitle[256] = "EXAMPLE CONSOLE"; // this is passed to non-const
    // char* param, so make sure it's a real buffer (by giving it a size)

    STARTUPINFO startupInfo = {0};
    startupInfo.cb = sizeof(STARTUPINFO);
    startupInfo.lpTitle = windowTitle;
    // can use startupInfo.wShowWindow to create window initially hidden
    // see MSDN entries for CreateProcess/STARTUPINFO

    PROCESS_INFORMATION processInfo = {0};

    // cmdLine is also passed to non-const char* param
    const size_t bufferSize = 1024;
    char cmdLineBuffer[bufferSize] = {0};
    if(NULL != cmdLine)
        strncpy_s(cmdLineBuffer, bufferSize, cmdLine, _TRUNCATE);

    BOOL bRet = CreateProcess( appPath
                             , cmdLineBuffer
                             , NULL                // process security attributes
                             , NULL                // thread security attributes
                             , FALSE               // bInheritHandles
                             , CREATE_NEW_CONSOLE  // dwCreationFlags
                             , NULL                // lpEnvironment
                             , NULL                // lpCurrentDirectory
                             , &startupInfo
                             , &processInfo );

    if(FALSE != bRet)
    {
        Sleep(250); // give the window a chance to appear
        // (can't use WaitForInputIdle with console app)

        HWND hWnd = FindWindow(NULL, windowTitle);

        if(NULL != hWnd)
        {
            const DWORD twoSecs = 2000; // in millisecs

            // Wait, Hide, Wait, Show window
            std::cout << "Sleeping for 2 seconds..." << std::endl;
            Sleep(twoSecs);

            std::cout << "Hide window" << std::endl;
            ShowWindow(hWnd, SW_HIDE);

            std::cout << "Sleeping for 2 seconds..." << std::endl;
            Sleep(twoSecs);

            std::cout << "Show window" << std::endl;
            ShowWindow(hWnd, SW_SHOW);
        }
        else
        {
            std::cerr << "Failed to find window" << std::endl;
        }

        CloseHandle(processInfo.hThread);
        CloseHandle(processInfo.hProcess);
    }
    else
    {
        dwRet = GetLastError();
        std::cerr << "Failed to create process (" << dwRet << ")" << std::endl;
    }

    return dwRet;
}

int main(int argc, char* argv[])
{
    if(2 != argc)
    {
        std::cerr << "RunApp <console app path>" << std::endl;
        return 0;
    }

    const char* appPath = argv[1];
    const char* cmdLine = NULL; // might be useful someday?

    DWORD dwRet = RunApp(appPath, cmdLine);

    return (int)dwRet;
}
Last edited on
Wow such a big mess for such a tiny thing, thanks but -
1
2
error: '_TRUNCATE' was not declared in this scope
error: 'strncpy_s' was not declared in this scope

although i have included the stdlib .. why?
What IDE/compiler are you using?

The code compiles with Visual C++ 2008, but I think the headers in Visual C++ 2010 have been decoupled a bit, so I didn't "miss" the header...

I think this is prob. the header that was being sneaked in -- by <windows.h> ???

#include <string.h>
Last edited on
Use strncpy() if you use another compiler, it is a standard C function, not a Microsoft extension.
I'm using codeblocks, on visual yes it's fine, i have already included string.h and still the same errors, weird.
Last edited on
On codeblocks, you prob need

strncpy(cmdLineBuffer, cmdLine, bufferSize);

Yep, thanks, now i will try to understand your example.
Topic archived. No new replies allowed.