I have a c++ program that I associated with .bin files, so that whenever the .bin file is opened, it is opened with myProgram.exe.
How do I get the filename of the associated file that opened my program?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In plain C++ you can use the arguments of
main:But there is a problem with that, namely that C and C++
mainwas designed for *nix, not Windows. The Holy C++ Standard recommends that the runtime should supply themainarguments UTF-8-encoded, but alas, that does not happen with conventional Windows C++ compilers. So in Windows this method might not work for filenames with e.g. Norwegian, Greek or Russian characters.In Windows, for a program intended to be used by others you can instead use the (Unicode version of the) Windows API function
GetCommandLine, and its parser cousinCommandLineToArgvWin order to split the command line into individual arguments.Alternatively, with Microsoft’s implementations of the C and C++ languages you can use the non-standard startup function
wmain. I don’t recommend that, however. I think it is a good idea to keep to the international standards for C and C++ as much as practically possible, and there is certainly no pressing reason to usewmain, even though it can be convenient for small toy programs.Cheers & hth.,