실행되고 있는 모듈(exe, dll)의 경로를 알아낼 때 사용.
GetModuleFileName의 첫번째 파라메터에 hInstance를 넣으면 진짜 자기자신을(-_-),
0 을 넣으면 최상위 실행파일명이 나옴.

예를 들어 이 함수를 실행하는 곳이 xxx.dll 이고 yyy.exe 파일에서 해당 dll을 로딩했다면,
hInstance를 넣으면 xxx.dll 의 경로가 반환되고,
0 을 넣으면 yyy.exe 의 경로가 반환됨.


function GetModulePath: String;
var ModulePath: array [0..MAX_PATH-1] of Char;
begin
  GetModuleFileName(hInstance, ModulePath, MAX_PATH);
  Result:=ModulePath;
end;














GetModuleFileName

Retrieves the fully-qualified path for the file that contains the specified module. The module must have been loaded by the current process.

To locate the file for a module that was loaded by another process, use the GetModuleFileNameEx function.

DWORD WINAPI GetModuleFileName(
  HMODULE hModule,
  LPTSTR lpFilename,
  DWORD nSize
);

Parameters

hModule
[in] Handle to the loaded module whose path is being requested. If this parameter is NULL, GetModuleFileName retrieves the path of the executable file of the current process.
lpFilename
[out] Pointer to a buffer that receives the fully-qualified path of the module. If the length of the path exceeds the size that the nSize parameter specifies, the function succeeds, and the string is truncated to nSize characters and cannot be null terminated.

The string returned will use the same format that was specified when the module was loaded. Therefore, the path can be a long or short file name, and can use the prefix "\\?\". For more information, see Naming a File.

nSize
[in] The size of the lpFilename buffer, in TCHARs.

Return Value

If the function succeeds, the return value is the length of the string that is copied to the buffer, in TCHARs. If the buffer is too small to hold the module name, the string is truncated to nSize, the function returns nSize, and the function sets the last error to ERROR_INSUFFICIENT_BUFFER.

If the function fails, the return value is 0 (zero). To get extended error information, call GetLastError.

Remarks

If a DLL is loaded in two processes, its file name in one process may differ in case from its file name in the other process.

For the ANSI version of the function, the number of TCHARs is the number of bytes; for the Unicode version, it is the number of characters.

The global variable _pgmptr is automatically initialized to the full path of the executable file, and can be used to retrieve the full path name of an executable file.

Windows Me/98/95:  This function retrieves long file names when an application version number is greater than or equal to 4.00 and the long file name is available. Otherwise, it returns only 8.3 format file names.


Posted by bloodguy
,