How enable dragging a file on the *.exe and get it as parameter? How enable dragging a file on the *.exe and get it as parameter? windows windows

How enable dragging a file on the *.exe and get it as parameter?


Your program does not need to do anything special apart from handling command-line arguments. When you drag-drop a file onto an application in Explorer it does nothing more than to pass the file name as argument to the program. Likewise for multiple files.

If all you expect is a list of file names, then just iterate over all arguments, do whatever you want with them and be done. This will work for zero to almost arbitrarily many arguments.


Maybe you could write a test program like this:

int main(int argc, char* argv[]){    // argv[0] is not interesting, since it's just your program's path.    for (int i = 1; i < argc, ++i)        cout << "argv[" << i << "] is " << argv[i] << endl;    return 0;}

And see what happens after you throw different files at it.


EDIT: Just look at Joey's answer.


Answer to the main question

TO SEE THE ANSWER TO YOUR LAST PROBLEM SEE BOTTOM OF THIS ANSWER

All drag&dropped files are get-able as argv[orderOfTheFile] (orderOfTheFile is from 1-n),
however how does windows create that order, now that is a real mystery...

Anyway let's say I would create 26 plain text files ( *.txt ), from a.txt to z.txt on my Desktop,
now if I would drag&dropped them on my ArgsPrinter_c++.exe located directly on C:\ drive,
an output would be similar to this:

argc = 27argv[0] = C:\ArgsPrinter_c++.exeargv[1] = C:\Users\MyUserName\Desktop\c.txtargv[2] = C:\Users\MyUserName\Desktop\d.txtargv[3] = C:\Users\MyUserName\Desktop\e.txtargv[4] = C:\Users\MyUserName\Desktop\f.txtargv[5] = C:\Users\MyUserName\Desktop\g.txtargv[6] = C:\Users\MyUserName\Desktop\h.txtargv[7] = C:\Users\MyUserName\Desktop\i.txtargv[8] = C:\Users\MyUserName\Desktop\j.txtargv[9] = C:\Users\MyUserName\Desktop\k.txtargv[10] = C:\Users\MyUserName\Desktop\l.txtargv[11] = C:\Users\MyUserName\Desktop\m.txtargv[12] = C:\Users\MyUserName\Desktop\n.txtargv[13] = C:\Users\MyUserName\Desktop\o.txtargv[14] = C:\Users\MyUserName\Desktop\p.txtargv[15] = C:\Users\MyUserName\Desktop\q.txtargv[16] = C:\Users\MyUserName\Desktop\r.txtargv[17] = C:\Users\MyUserName\Desktop\s.txtargv[18] = C:\Users\MyUserName\Desktop\t.txtargv[19] = C:\Users\MyUserName\Desktop\u.txtargv[20] = C:\Users\MyUserName\Desktop\v.txtargv[21] = C:\Users\MyUserName\Desktop\w.txtargv[22] = C:\Users\MyUserName\Desktop\x.txtargv[23] = C:\Users\MyUserName\Desktop\y.txtargv[24] = C:\Users\MyUserName\Desktop\z.txtargv[25] = C:\Users\MyUserName\Desktop\a.txtargv[26] = C:\Users\MyUserName\Desktop\b.txt

My ArgsPrinter_c++.exe source code:

#include <iostream> using namespace std;int main(int argc, char* argv[]) {    cout << "argc = " << argc << endl;    for(int i = 0; i < argc; i++)       cout << "argv[" << i << "] = " << argv[i] << endl;    std::cin.ignore();   return 0; }

Your last problem

I have created a simple program that creates only a sceleton of your class so it can be used, and the program's main itself ran JUST FINE => if your program exits too soon, the problem will be in your class...

Tested source code:

#include <iostream> #include <vector>using namespace std;class Converter{    public:     Converter(const char* f){ cout << f << endl; }    void getATCommandsFromCSV(){ cout << "called getATCommandsFromCSV" << endl; }};int main(int argc, char* argv[]) {   vector<string> files;  for (int g = 1; g < argc; g++) {      string s = argv[g];      string filename = "";      int pos = s.find_last_of("\\", s.size());      if (pos != -1) {          filename = s.substr(pos + 1);          cout << "argv[1] " << argv[1] << endl;          cout << "\n filename: " << filename << "\n pos: " << pos << endl;          files.push_back(filename);          }      files.push_back(s);      }  for (unsigned int k = 0; k < files.size(); k++)      {      cout << "files.at( " << k << " ): " << files.at(k).c_str() << endl;      Converter a(files.at(k).c_str());      a.getATCommandsFromCSV();      }  cout << "\n" << "Programm finished...\n\n" << endl;  cin.ignore();  return 0;}