You are here:

C++/Invalid Handle Error while using WINAPI call GetModuleFileNameEx in c++

Advertisement


Question
Dear Sir,

I am trying to write  a program in c++ that gets the  name of the EXE for an external process thats running.

i have obtained the Handle of the program using FindWindow call. I subsequently used the GetModuleFileNameEx to retrieve the path of the EXE but am not able to.
here is the code


=============================================

#include <windows.h>
#include<iostream>
#include <psapi.h>
#pragma comment(lib, "psapi.lib")

using namespace std;

int main(int argc, char *argv[])
{
   
   HWND hYahoo;
   char x[10];  //dummy variable
   
   hYahoo=FindWindow("YahooBuddyMain",NULL); // get the handle of the the mainwindow of YahooMessenger
   

   if(hYahoo==NULL){
         cout<<"Failed";
         }
   else{
        cout<<"Success";
       }
        
        TCHAR value[512];
        LPTSTR lpStr;
        lpStr=value;
        DWORD dw;
        dw=GetModuleFileNameEx(hYahoo, NULL,lpStr,512); //also used '_MAX_PATH'
        DWORD derr;
        derr=GetLastError();
        cout<<" CODE:"<<derr; //Gives error Code
        cout<<dw;   //returns Zero if there is an error
        cin>>x;
        }

=================================================
i have a prob compiling this in Dev C++. gives me a linker error. I checked to see if i have "psapi.dll" in my system32 folder and i do. i got hold of "psapi.lib" and included it for the linker.. but still gives me an error saying
"  [Linker error] undefined reference to 'GetModuleFileNameExA@16' "

so i shifted to Borland C++ builder 6. I initially had a problem with the psapi.lib as i was getting an error message:
contains invalid OMF record, type 0x21 (possibly COFF)

so i used the Borland IMPLIB tool to create psapi.lib from the psapi.dll.
The program now compiles and executes..
but the GetModuleFileNameEx does not return the value of the process EXE file with path.

GetLastError tells me that it is an error code 6, which i found from MSDN as  'Invalid Handle'



I am unable to figure out the problem. i looked at  various code samples in MSDN and other websites, but i am not able to figure out what is wrong. I did not get a chance to try it on VC++ . Could it be a problem with the psapi.lib? or am i missing out something? also i would like to know if there is anyway to find out the name of the EXE if i have the process ID . i am successfully able to extract the process ID using the optional parameter in the GetWindowProcessThreadID.

I would be grateful for any comments or suggestions.
Thank you,
Jimmy

Answer
First I must apologise for not getting back to you sooner. I work away from home at the moment and forgot to place myself on AllExperts vacation this week - sorry!

Yes you missed the point on something - FindWindow returns a HWND, a HANDLE to a window not a HANDLE to a process. GetModuleFileNameEx requires the process handle. If you have the process ID you can use the OpenProcess function (windows.h, kernel32.lib)to obtain a HANDLE to the process. You can also use EnumProcesses (psapi) to enumerate through all processes. Check out the MSDN library example "Enumerating All Modules For a Process" (the MSDN Library can be found online at http://www.msdn.microsoft.com/, the example can be found at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/en  -- long URL might wrap!).

Hope this helps.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.