Retrieving the Filename of an Application or DLL

Last reviewed: July 22, 1997
Article ID: Q72385
3.00 3.10 WINDOWS kbprg

The information in this article applies to:

  • Microsoft Windows Software Development Kit (SDK) for Windows versions 3.1 and 3.0

SUMMARY

In the Microsoft Windows environment, the GetModuleFileName function provides the filename of the executable file that corresponds to a given module instance handle or data instance handle.

MORE INFORMATION

GetModuleFileName retrieves the full path of an application or a dynamic-link library (DLL). Specify the instance handle or module handle of the executable hModule parameter. The syntax of this function is:

   int GetModuleFileName(HANDLE hModule, LPSTR lpFilename, int nSize);

The hModule parameter identifies the module or instance handle, lpFilename points to the buffer to receive the file name, and nSize specifies the buffer size.

When Windows launches an application, its instance handle is a parameter to WinMain. For a DLL, the instance handle is a parameter to LibMain. The instance handle is also available through the GetWindow function, as follows:

   // hWnd is a handle to any one of the target
   // executable file's windows or controls.
   hAppInstance = GetWindowWord(hWnd, GWW_HINSTANCE);

   nPathLength = GetModuleFileName(hAppInstance,
                    (LPSTR)szPath, PATH_LENGTH);

The GetModuleHandle function provides the module handle for a specified module. For example, if Microsoft Excel is loaded, the full path to the Excel executable file is available through the following code:

   // "EXCEL" can be used instead of "EXCEL.EXE".
   hModule = GetModuleHandle("EXCEL");

   nPathLength = GetModuleFileName(hModule,
                    (LPSTR)szPath, PATH_LENGTH);

Given a handle to a window or control in an application, the GCW_HMODULE parameter to the GetClassWord function provides the application's module handle.


Additional reference words: 3.00 3.10
KBCategory: kbprg
KBSubcategory: KrMisc
Keywords : kb16bitonly


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 22, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.