views:

70

answers:

2

I have the problem that i am using relative paths for my files, so when i use "open with" method for opening a file into my program, all my paths are screwed (it will create my files in the folder where i used this "open with" method.)

How i can retrieve the full path to the .exe file that im using to open the file with "open with" method?

Edit: my main function:

int WINAPI WinMain( HINSTANCE   hInstance,          // Instance
                    HINSTANCE   hPrevInstance,      // Previous Instance
                    LPSTR       lpCmdLine,          // Command Line Parameters
                    int         nCmdShow)           // Window Show State
{
+4  A: 

The GetModuleFileName will give you the absolute path of your executable:

wchar_t executablePath[MAX_PATH];
if(GetModuleFileNameW(NULL, executablePath, MAX_PATH) == 0) { ... error ... }
else { ... find out executable path and set cwd ... }
AndiDog
+1  A: 
#include <windows.h>
#include <string>
#include <iostream>
using namespace std;;

string ExePath() {
    char buffer[MAX_PATH];
    GetModuleFileName( NULL, buffer, MAX_PATH );
    string::size_type pos = string( buffer ).find_last_of( "\\/" );
    if ( pos == string::npos ) {
        return "";
    else {
        return string( buffer ).substr( 0, pos);
    }
}

int main() {
    cout << "executable path is " << ExePath() << "\n";
}
anon