일반 Console Application으로 프로그램을 개발할 때 실행 프로그램 자기 자신의 경로를 알아내기 위해서는 매개변수로 넘어오는 argv와 argc를 참조하면 된다. 다음 소스를 보자.
위의 프로그램의 실행파일 이름이 "test.exe"라면, 위 프로그램을 실행시켰을 때 "test.exe"가 출력이 된다. 하지만 이 방법은 매개변수로 다른 값을 넘겨받는 WINAPI 프로그램에서는 사용할 수가 없다. 때문에 GetModuleFileName이라는 API 함수를 이용하여 자신의 파일 경로를 알아냈 수 있다.
GetModuleFileName
The GetModuleFileName function retrieves the full path and filename for the executable file containing the specified module.
Windows 95: The GetModuleFilename function will return long filenames when an application's version number is greater than or equal to 4.00 and the long filename is available. Otherwise, it returns only 8.3 format filenames.
DWORD GetModuleFileName( HMODULE hModule, // handle to module to find filename for LPTSTR lpFilename, // pointer to buffer to receive module path DWORD nSize // size of buffer, in characters );
Parameters
- hModule
- Handle to the module whose executable filename is being requested. If this parameter is NULL, GetModuleFileName returns the path for the file used to create the calling process.
- lpFilename
- Pointer to a buffer that is filled in with the path and filename of the given module.
- nSize
- Specifies the length, in characters, of the lpFilename buffer. If the length of the path and filename exceeds this limit, the string is truncated.
Return Values
If the function succeeds, the return value is the length, in characters, of the string copied to the buffer.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
If a module is loaded in two processes, its module filename in one process may differ in case from its module filename in the other process.
QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Unsupported.
Header: Declared in winbase.h.
Import Library: Use kernel32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT.
See Also
Dynamic-Link Libraries Overview, Dynamic-Link Library Functions, GetModuleHandle, LoadLibrary
간단히 설명하자면 다음과 같다.
먼저 첫 번째 매개변수에는 Module의 핸들을 넣어주어야 하는데 일반적으로 NULL을 넘겨 주어도 무방하다. 두 번째 매개변수에는 파일 경로를 저장할 문자열 포인터를 넘겨준다. 세 번째 매개변수에는 두 번째로 넘겨 준 문자열의 길이를 넘겨준다.
실제 사용 예는 다음과 같다.
위 소스는 FullPath에 자신의 경로를 저장해 준다.
참고적으로, 첫 번째 방법은 실행파일명(ex - "test.exe")만을 알아낼 수 있지만, 두 번째 방법은 절대 경로(ex - "c:\test\test.exe")를 알아낼 수 있다.
댓글을 달아 주세요
ㅋㅋ 이거 꽤 쓸만함 함수를 잘 정리해주셨군요.
감사합니다
감사합니다~ 저는 Lec 개발시 아주 유용하게 사용하고 있답니다ㅎ
Lec 아직도 개발중이었습니까?ㅋㅋ
네~ 열심히 개발중이랍니다.;; 더욱 강력해진 Lec을 기대하세요~
성실한 Harry씨 ㅋㅋ
'성실하다'라는 표현은 저와는 매우 동떨어진 표현이라고 생각합니다만,,
ㅋㅋㅡ.ㅡ;;
아녜용 우리 해리님은 성실해야 할 때는 성실해요~