Solved

How to get a Windows Explorer handle?

Posted on 2000-05-15
6
971 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

688 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