How to properly use GetModuleFileName?

Sunrise picture Sunrise · Sep 13, 2013 · Viewed 60.5k times · Source

Following code:

#include <iostream>
#include <Windows.h>

using namespace std;

int main ()
{   LPWSTR buffer; //or wchar_t * buffer;
    GetModuleFileName(NULL, buffer, MAX_PATH) ;
    cout<<buffer;
    cin.get();
    cin.get();

}

Should show the full path where the program executes. But in VS 2012 I get the error:

uninitialized local variable 'buffer' used

What's wrong in code?

Answer

Mats Petersson picture Mats Petersson · Sep 13, 2013

You need to give it a buffer that can hold some characters;

 wchar_t buffer[MAX_PATH]; 

for example.