Dropping a file onto an applications icon

Posted on 1998-04-29
Last Modified: 2011-08-18
I would like it so that I can drop a file onto my application's icon and then the application would start up with information gleaned from the file.  I'm using MS VC++ 5.0 and have experimented with the WM_DROPFILES message, but that only appears to work once the application has already started.  Any help would be appreciated.
Question by:tdubroff
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3

Expert Comment

ID: 1167389
You have to accept command line arguments in your application to be able to accept a file that is dropped on your icon.  Your main routine should look something like this:

void main(int argc, char* argv[])
   //argc is the argument count
   if (argc > 1)
      //read in your file whose name is in argv[1]
      //or do whatever file manipulation you are doing
      ReadFile( argv[1] );
   //put the rest of your main subroutine in here

Author Comment

ID: 1167390
You get command line args automatically with an AppWizard generated application.

LVL 10

Expert Comment

ID: 1167391
Ronslow, can you be a little more specific?  I have a global application called The App that runs my modal dialog box.  Where do I insert the use of command-line arguments?  I have put an OnDropFiles(HDROP hDrop) message handler in both The App and my dialog box, but that only accepts dropped files after the application has started up.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 1167392
Include this code in your application's InitInstance

      CCommandLineInfo cmdInfo;

Then look at cmdInfo to determine what to do with the file

LVL 10

Accepted Solution

RONSLOW earned 100 total points
ID: 1167393
SDI and MDI apps already have thie code .. They then call ProcessShellCommand which interprets the command line and sends commands like ID_FILE_NEW etc.  Ida dialog app, you (probably) don't want this last step .. you can check m_nShellCommand for the type of command line given (FileNew is no file dropped, FileOpen is a file was dropped etc).

This can also be used when your app is associated with a file type .. so double clicking on the file will launch your app with the filename as a command line arg, and the m_nShellCommand will be FileOpen and m_strFileName will be the name.
LVL 10

Expert Comment

ID: 1167394
Thanks for the help Ronslow.  I got it working.

Author Comment

ID: 1167395
Glad to help


Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to properly read C++ console input into several variables with scanf 2 41
C++ help/ Toy problem 19 70
learn programming 8 94
MFC COM Server not showing  form 4 25
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question