Solved

How to get a Windows Explorer handle?

Posted on 2000-05-15
6
953 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

770 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