Way to differentiate between cmd line args and drag-onto-icon args in Windows?
I have a Windows GUI app written in C (MinGW) and would like to have the app perform different tasks depending on whether it was launched via the command line with a filename argument or by dragging a file onto the application icon. The way it is now, the following function doesn't differentiate between the two:
int argc;
LPWSTR *argv = CommandLineToArgvW(GetCommandLineW(),&argc);
When a file is dragged onto the application's icon, it assumes it was launched via the command line. The problem with this is that I need additional arguments that must be passed via command line in order to do anything useful. The filename itself is not enough, so the app just quits because it doesn't have enough information to proceed.
What I would like is for the user to be able to drag a file onto the app 开发者_Python百科icon, and have a window come up asking for the required options. If the user launches the app via command line with the required options already supplied, the app would immediately start processing without asking for additional input. Is this possible?
Another issue I am having is that sometimes when a file is dragged onto the app's icon, it crashes. I narrowed it down to anything operating on the argv[] values. It doesn't do this if launched via command line with the same argument. For example, this will crash the app about 20% of the time:
fprintf(stderr,"argv[3] was %ls\n",(LPWSTR)argv[3]);
Why would this only happen when launching via drag-n-drop? I am on Windows 7 x64.
Windows will by default call your program with the file name of the file you dropped on the its icon as the first and only argument. So you will get exactly the same invocation parameters in this case that you get when somebody starts your program from the command line with the full qualified name of the same file as the one and only argument.
You can however add additional arguments to a shortcut icon when you install your application i.e. "path\myapp.exe -gui"
. That allows you to differentiate between invocations via the icon in general (also applies to double clicking without any parameter) and invocations on the command line where the -gui
parameter will usually not be specified.
It's certainly possible. Let's say the user must specify -slow
or -fast
on the command line. Your code then looks something like:
int main( int argc, char *argv[] ) {
if ( argv contains "-slow" or -"fast" ) {
we were launched fronm the command line
else
we were either launched from an icon, or the user has
not specified -slow or -fast. In either case, pop up
a dialog to get the options
endif
}
I don't think you can, when you drag a file over an icon, the OS executes the program using the file name as argument in the command line, so they are effectively the same.
精彩评论