开发者

Get file handle to running executable

I am trying to call GetFileInformationByHandle on the executable of my own running program. This means I'll need to get a f开发者_开发技巧ile handle to the .exe that started my program. Is there any way to do this?

Failing that, is there any way to get the nFileIndexHigh and nFileIndexLow for a running executable?


DWORD WINAPI GetModuleFileNameEx(   
   __in      HANDLE hProcess,
   __in_opt  HMODULE hModule,
   __out     LPTSTR lpFilename,
   __in      DWORD nSize ); 

Second parameter should be NULL and you will get the name of the current executable.

EDIT:

Then open the file.


Here it is a way to do this. I hope this helps:

#include <Windows.h>
#include <iostream>
using namespace std;

//declare a BY_HANDLE_FILE_INFORMATION structure
BY_HANDLE_FILE_INFORMATION fileinfo;

int main()
{
    // clear everything in the structure, this is optional
    ZeroMemory(&fileinfo, sizeof(BY_HANDLE_FILE_INFORMATION));

    // obtain a handle to the file, in this case the file
    // must be in the same directory as your application
    HANDLE myfile = NULL;
    myfile = CreateFileA("example.exe",0x00,0x00,NULL,
                         OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

    // if we managed to obtain the desired handle
    if(myfile!=INVALID_HANDLE_VALUE)
    {
        //try to fill the structure with info regarding the file
        if(GetFileInformationByHandle(myfile, &fileinfo))
        {
            // Ex: show the serial number of the volume the file belongs to
            cout << endl << hex << fileinfo.dwVolumeSerialNumber << endl;
        }
        // you should close the handle once finished
        CloseHandle(myfile);
    }
    system("pause");
    return 0;
}


You should try GetCommandLine to get path to executable. Then open and here is your handle.


GetModuleHandle is the solution here.

http://msdn.microsoft.com/en-us/library/ms683199(VS.85).aspx

If this parameter is NULL, GetModuleHandle returns a handle to the file used to create the calling process (.exe file).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜