|
Method1 (using _getcwd function):
In the first method we are going to use the
_getcwd
function. In order to use this function we need to add
its relevant header file called <direct.h>.
This function gets an ABSOLUTE PATHNAME of a current
working directory and returns a pointer to buffer. What
I mean with Absolute Pathname is this:
C:\Program Files\Adobe
Here are some examples of NONE ABSOLUTE PATHNAME:
C:\Program Files\Adobe\
C:\Program Files\Adobe\Photoshop.exe
In the next method you will see that we don't get the
ABSOLUTE PATHNAME but rather the full directory path
that holds our
.exe file. Now Lets take a look at the _getcwd function
and parameters that are passed to it.
char *_getcwd(char *buffer, int maxlen);
This function returns a pointer to buffer where it holds
the location for the path. Maximum length is the size in
bytes of the character array pointed to by the buffer
argument.
Method2
(using GetModuleFileName):
In the second method, we are going to use the windows
API function called
GetModuleFileName
and use STL String to tackle the character operations.
You also need to add its relevant header file called <Windows.h>
to use this function. Here is what you originally get
when you use GetModuleFileName:
C:\Program Files\Adobe\Photoshop.exe
Now Lets take a look at the GetModuleFileName function
and parameters that are passed to it:
DWORD WINAPI
GetModuleFileName(HMODULE hModule,LPTSTR
lpFilename,DWORD nSize);
hModule:
A 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:
A pointer to a buffer that
receives the fully-qualified path of the module.
nSize:
The size of the lpFilename
buffer, in TCHARs.
Now If you call getMyAppPathMethod2() function, you will
get an error:
error C2664: 'GetModuleFileName'
: cannot convert parameter 2 from 'char [260]' to 'LPWCH'
There are many ways to fix this problem. But for now go
to Project Properties(ALT+F7)>Configuration
Prop>General>Character Set>change it from "Use Unicode
Character Set" in this case to either "Not
Set"
or "Use
Multi-Byte Character Set". |