c++getmodulefilename

How to properly use GetModuleFileName?


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?


Solution

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

     wchar_t buffer[MAX_PATH]; 
    

    for example.