0

We can use

int main (int argc, char* argv[])

to pass the strings into a program when

  • drag the file and drop it to the executable's icon if you want to fill argv[1] with filepath.

  • or start the program with command line: Program.exe C:\File.txt to fill argv[1] with filepath.

But, is it feasible to get the filepath if drag and drop the file into the "Console Window" while executing?

If my post isn't clear, please help me re-edit it.

Thanks.

4
  • No certain I understand the problem. How are you in the 'Console Window' and executing at the same time? Commented Dec 6, 2013 at 15:41
  • Is this question about changing argv from outside of your program at runtime? Commented Dec 6, 2013 at 15:43
  • @KeithSmith Not command prompt. It is a console application compiled from GCC/mingw32 Commented Dec 6, 2013 at 15:46
  • How do you expect to drag and drop a filename into a 'console window'? There is not Windows assist to associate the console program(GCC/mingw32) which has no associated shortcut to an executable with the drag and drop filename. At least this is my understanding. I would think you would have to write a small Windows program to accept the drop and drag and then spawn the mingw32 program. Others can 'chime' in if I'm all wet. Commented Dec 6, 2013 at 15:50

2 Answers 2

2

Getting the file path on drag & drop on an executable icon is already a function of the GUI in question, and thus "OS specific" (although widespread).

Plain standard C has no notion of a "Console Window" in the first place (the closest thing being "line buffered I/O"), so the answer to your question is "no". Not without going beyond the ISO/IEC 9899 standard library, i.e. by perusing ncurses (Unix-ish) or whatever Windows provides in that arena.

Anyway, and this is the important point, once any executable is executing (and thus, having a "console window" to speak of), main() has already been entered, so there's no way to receive anything via its parameters (argv).

Sign up to request clarification or add additional context in comments.

Comments

1

The drag and drop feature is an addition of the "Window Shell" environment. It is not an item that is handled in a standard way, and is beyond the ability to control from the program's source code.

The window manager will decide which graphical program will receive the drag and drop event. That program then decides to do something.

In the "CMD.EXE" program on most modern windows machines, it handles this event by looking at the event's attached file system object, and then fetching the absolute path string to that file.

In the "Windows Shell" program, it checks to see if the receiving object can handle the dropped object, and if so, then it will typically pass the dropped object's path to the receiving object as a command line parameter. However, much of this is done by a convention, you might not get the ability to style that convention to your particular whims.

The key items to consider is that basically the programs that must do the work are not related to the programs you are writing. If you were writing a CMD.EXE you could alter the behavior as you wished. Unfortunately, as you are not writing CMD.EXE you must live with the behavior that someone else has provided.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.