   Starting a Program from a File
  Hi, I would like to write a program, that is able to be started from a file. (and takes that file as input)Like PDF. 1. you open a file in a text editor and write "Hello World". 2. you save that file as input.fr 3. you tell windows to open the file input.fr with HelloWorldSayer.exe4. HelloWorldSayer.exe is started and gets input.fr as a parameter from WindowsHow to open that file and reading the input, I know.The "open the file input.fr with HelloWorldSayer.exe" part is what I don't know how to do. (I have also no clue, how this is handled internally by windows. I just figured, windows would give the file as an input parameter)I hope someone could explain me, how this stuff works (more or less detailed) or give me the link to a tutorial, that doesn't just cover the basic I/O functions.   
  If you want to programatically invoke a .exe file, use the CreateProcess function. There is an optional parameter for specifying the command-line arguments (this is where you would specify your input.fr as an argument).   ;  I suppose that the OP is asking how to make windows using a specific application to open a specific file type.You must associate the extension to your application. One way to do it is by using "Open with" and specifying the path of your application. That can also be done programmatically when installing it. Be sure to check the checkbox to make windows to always use the app for that kind of files.When you double click on the file IIRC the file name is passed as an argument to the application, so that you can handle it.   ;  thanks, exactly what I wanted to know.   ;  Follow up question:How do I tell the program to take the file name (from which the program is started) as an command line argument? By the way, I'm working with Visual Studio 2008, so I guess the question should be, how do I tell VS to compile my program that way. (How to set a command line argument for debugging I have found out. Works all fine.) My Code: int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow ){ HWND hWnd = CreateMainWindow( hInstance, 800, 600 ); char* filename = lpCmdLine; // Commandline argument as filename  ifstream file(lpCmdLine);if (file.is_open()){ ofstream file("cout.txt");file << "okay" << endl;file.close();}else{ofstream file("cout.txt");file << "bad" << endl;file << *lpCmdLine << endl;file.close();}MSG msg = {0};while(msg.message != WM_QUIT ){if(PeekMessage(&msg, NULL, 0,0, PM_REMOVE)){TranslateMessage(&msg;DispatchMessage(&msg;}else{}} return 0;};Quote:Original post by Elandar23Follow up question:How do I tell the program to take the file name (from which the program is started) as an command line argument?Did you check if it just does that automatically (i.e. look for the file name in lpCmdLine)? Because I would expect it to. :)   ;  If you've associated the file using the Open With... method above, it will have automatically set things up to send the file you double-click as an argument to the program.This is nothing to do with the actual program but more to do with the command that the Windows shell executes when you double-click the file.However, parsing a WinMain-style command line can be more tricky than it first seems and I'd recommend that you use CommandLineToArgvW to split the command line up.There does not seem to be a non-UNICODE version of this function and you don't seem to be using UNICODE in your code above, so here is a function I use to get the above method to convert the WinMain command line into a std::vector of non-UNICODE std::strings:std::vector<std::string> CommandArgs(){    int No;    LPWSTR *Args=CommandLineToArgvW(GetCommandLineW(),&No);    std::vector<std::string> V;    if(Args)   {   for(int I=0;I<No;++I)  {  size_t Ret;  std::vector<char> Mb(wcslen(Args)+1);  wcstombs_s(&Ret,&(Mb[0]),Mb.size(),Args,Mb.size()-1);  V.push_back(&(Mb[0]));  }   LocalFree(Args);   }    return V;}// exampleint WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow ){    // create window etc    std::vector<std::string> Args=CommandArgs();    // note first element is the executable name    if(Args.size()>1)   {   // Args[1] is a std::string containing the first parameter   }}Using this method will deal correctly with odd corner cases like Windows surrounding your path with double-quotes and the way that backslashes are handled. It basically converts the WinMain()-style command line (which is passed to WinMain just as a single string) into the kind of input you are used to getting into a main()-style command line.   