GetModuleFileName not returning Path

6.6k views Asked by At

I'm trying to get the current directory under win7 with VS c++ but

TCHAR pBuf[MAX_PATH];
int bytes = GetModuleFileName(NULL, pBuf, MAX_PATH);
std::cout << bytes << "   " << pBuf << "   " <<   GetLastError() << std::endl;

returns length 58 and what I believe to be a pointer in pBuf pointing to 68. Errorcode is 0.

Thank you!

1

There are 1 answers

1
Roman Ryltsov On BEST ANSWER

Your << operator does not accept const TCHAR* as a string argument, and you have it printed out as a generic pointer.

One of the ways to fix the problem is to use A version of the API (CHAR buffer and GetModuleFileNameA function):

CHAR pBuf[MAX_PATH];
int bytes = GetModuleFileNameA(NULL, pBuf, MAX_PATH);
std::cout << bytes << "   " << pBuf << "   " <<   GetLastError() << std::endl;