Solved

How to get a Windows Explorer handle?

Posted on 2000-05-15
6
960 Views
Last Modified: 2012-06-21
I need to lauch NT Windows Explorer from an VC++ application, get the handle of the explorer (Note, not Internet Explorer!), and direct the explorer to a specific folder. Someone please tell me how to achieve this?
0
Comment
Question by:yzmao
  • 2
  • 2
  • 2
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 2812009
To open the explorer & direct it to a specific directory is quite easy, simply use

ShellExecute(handle, "explore", <path_to_folder>, NULL, NULL, SW_SHOWNORMAL);

Getting the handle turns out to be more difficult, the easiest solution that I could imagine would be sth. like this:

// globals
HWND  g_hwnd;
int  g_nFound;

BOOL CALLBACK FindHwndFromPID( HWND hwnd, LPARAM lParam) :


HWND GetWindowHandleFromPID ( DWORD dwProcessId)
{
 g_hwnd = NULL;
 g_nFound = 0;

 EnumWindows (  FindHwndFromPID,    ( LPARAM) dwProcessId));

 if ( g_hwnd)  // we found one...
      return ( g_hwnd);

 // nothing found :-(

 return (NULL);
}

BOOL CALLBACK FindHwndFromPID( HWND hwnd, LPARAM lParam)
{
    DWORD   dwPID2Find  =   ( DWORD) lParam;
    DWORD   dwPID       =   0;

    if  (   GetWindowThreadProcessId    (   hwnd,   &dwPID))
        {
            if  (   dwPID   ==  dwPID2Find)
                {
                    g_hwnd  =   hwnd;

                    return  (   FALSE);
                }
        }

    return  (   TRUE);
}

    STARTUPINFO         si;
    PROCESS_INFORMATION pi;

    BOOL                bRes;

    DWORD               dwCode  =   0;

    ZeroMemory  (   &si,    sizeof  (   STARTUPINFO));

    si.cb           =   sizeof  (   STARTUPINFO);
    si.dwFlags      =   STARTF_USESHOWWINDOW;
    si.wShowWindow  =   SW_SHOWNORMAL;


    bRes    =   CreateProcess   (   NULL,
                                    "explorer.exe /root,<path_to_folder>",
                                    NULL,
                                    NULL,
                                    TRUE,
                                    NORMAL_PRIORITY_CLASS,
                                    NULL,
                                    NULL,
                                    &si,
                                    &pi
                                );


    //...
    HWND hwnd = GetWindowHandleFromPID ( pi.dwProcessId);

Feel free to ask if you need more information!

0
 

Author Comment

by:yzmao
ID: 2812446
Hi jkr:

Thank you very much for your quick and detailed response. I appreciate your help very much.

I'm testing t your code now... one question - there is an error from the follwing line:

 EnumWindows (  FindHwndFromPID,    ( LPARAM) dwProcessId));

where the varialbe g_hwnd gets the return value from function FindHwndFromPID?

Thank you agin!

yzmao
0
 
LVL 22

Expert Comment

by:nietod
ID: 2812741
That probable is because th FindhwndFromPID procedure has not been defined at that point.  You should move the procedure before that point, or add a forward declaration for the procedure
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Expert Comment

by:jkr
ID: 2813866
>>there is an error from the follwing >>line

Well, which error do you get? nietod is probably right, though there is a forward declaration in the example above...
0
 
LVL 22

Expert Comment

by:nietod
ID: 2814158
Missed that.
0
 

Author Comment

by:yzmao
ID: 2817061
I couldn't get the handle yet. But I don't have time to play this for a while. Thanks anyway.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to copy an image file into clipboard C/C++? 1 196
Autosar OS Multicore Share Resources confusion ? 2 69
How do i run a c++ file? 15 46
Indy 10 not Receiving UDP broadcast 3 21
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

766 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question