Solved

How to get a Windows Explorer handle?

Posted on 2000-05-15
6
945 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now