Hi, I am reading a SFML and C++ book, but I have come to a problem. I am making a Utilities file to get the working directory but there is an error here, and I can't figure it out
On the GetModuleFileName it says:
Error: argument of type "char *" is incompatible with parameter of type "Utils::LPWSTR"
I get the same error in PathRemoveFileSpec
Ahhh! So your problem was the infamous Visual Studio wide character default setting? Should have known!
Easier than using the TCHAR macros as Thomas suggested is just to use wchar_t instead of char. Also, I preface most of my code with this for cases where I test compile with GCC...
why is it easier? Imagine you have to change from Unicode to Multi-byte
And why would you have to do that? 1998 is a long time gone. The operating system internally is wide character. All calls to 'A' suffixed Win32 functions get translated via a wrapper to the 'W' functions, which are in fact the only ones which really exist. So just calling the 'W' functions directly saves a layer of translation between the two kinds of strings. And you can save yourself the whole TCHAR mess by simply defining UNICODE and using wchar_t instead of char. And in the case of Visual Studio where its pre-defined for you if you use the IDE you don't even have to define it yourself. All you have to do is use wchar_t instead of char and plow blitherly ahead using non A/W versions of the functions such as just GetCurrentDirectory() instead of GetCurrentDirectoryA or GetCurrentDirectoryW.
But if you really need to create narrow and wide builds for some reason then by all means use the TCHAR macros and includes.
And why would you have to do that? 1998 is a long time gone.
@ freddie,
if you need to run your code on Windows 2000 which doesn't support Unicode. Don't laugh, I once had a pen friend on the Phillippines who didn't have the money for sth. newer. I also saw on log files of websites that there are people from poor countries like Eastern Europe or East Asia where many people still use Windows 98 or 95.
Some time ago a friend of mine had to change from Multy-byte to Unicode and I still can hear him swearing. All the changes from char to wchar_t and using L in front of ""
With TCHAR you just change the project settings and you are done.
But if this doesn't concern you just use wchar_t
It think you should consider changing the name of your function to GetExeDirectory() or similar, so it does what it says it does.
Your function returns the directory where the running executable is located, which is not always going to be the working directory. In fact, it's only located in the working directory if you change to that directory and run the exe from there, or you get the exe to programmatically change the working directory to where it's located when running.
The WinAPI call to get the current working directory is GetCurrentDirectory() and gives the same folder as _getcwd() (MSVC CRT) / getcwd()
Andy
PS As GetModuleHandle(NULL) gets the handle of the exe for the current process, which is always going to be there, it is never going to fail. So the test for NULL is not needed in this case. In fact, you can just pass NULL to GetModuleFileName() to obtain the path you require.