windows 如何启用在 *.exe 上拖动文件并将其作为参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4659105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:57:30  来源:igfitidea点击:

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

c++windowsparametersdrag-and-dropexecutable

提问by Beasly

What do I have to do to make my program use a file that has been dragged and dropped onto its icon as a parameter?

我该怎么做才能让我的程序使用已拖放到其图标上的文件作为参数?

My current mainmethod looks like this:

我目前的main方法是这样的:

int main(int argc, char* argv[])
{
    if (argc != 2) {
        cout << "ERROR: Wrong amount of arguments!" << endl;
        cout << "\n" << "Programm closed...\n\n" << endl;
        exit(1);
        return 0;
    }

    Converter a(argv[1]);
    // ...

    cout << "\n" << "Programm finished...\n\n" << endl;

    // cin.ignore();
    return 0;
}

What I'd really like to be able to do is select 10 (or so) files, drop them onto the EXE, and process them from within my application.

我真正想做的是选择 10 个(左右)文件,将它们放到 EXE 中,然后在我的应用程序中处理它们。



EDIT:

编辑:

The incomming parameter is used as filename, constructed in the cunstructor.

传入参数用作文件名,在 cunstructor 中构造。

Converter::Converter(char* file) {
       // string filename is a global variable
   filename = file;
   myfile.open(filename.c_str(), ios_base::in);
}

The method where the textfile gets read:

读取文本文件的方法:

string Converter::readTextFile() {
char c;
string txt = "";

if (myfile.is_open()) {

    while (!myfile.eof()) {
        myfile.get(c);
        txt += c;
    }

} else {
    error("ERROR: can't open file:", filename.c_str());
}
return txt;
}


EDIT2:deleted

EDIT2:删除

Update:
I got again to this point.

更新:
我又到了这一点。

Actual Mainmethod:

实际Main方法:

// File path as argument

int main(int argc, char* argv[]) { if (argc < 2) { cout << "ERROR: Wrong amount of arguments! Give at least one argument ...\n" << endl; cout << "\n" << "Programm closed...\n\n" << endl; cin.ignore(); exit(1); return 0; }

int main(int argc, char* argv[]) { if (argc < 2) { cout << "错误:参数数量错误!至少给出一个参数......\n" << endl; cout << "\n" << "程序关闭...\n\n" << endl; cin.ignore(); 退出(1);返回0;}

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;
}

Actually the console window apears for maybe 0.5 sec and closes again.
It doen't stop on any of my cin.ignore();Maybe it doesn't get there?

实际上控制台窗口可能会出现 0.5 秒并再次关闭。
它不会在我的任何一个上停止cin.ignore();也许它没有到达那里?

Can anyone help?

任何人都可以帮忙吗?

回答by Joey

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.

如果您期望的只是文件名列表,那么只需遍历所有参数,对它们执行任何您想做的操作并完成。这将适用于零到几乎任意多个参数。

回答by pyon

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.

编辑:看看乔伊的回答。

回答by jave.web

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](orderOfTheFileis from 1-n),
howeverhow does windows create that order, now thatis a real mystery...

所有拖放文件都可以作为argv[orderOfTheFile]orderOfTheFile从 1-n 开始),
但是Windows 如何创建该顺序,现在是一个真正的谜......

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

不管怎样,让我们说,我会创造26个纯文本文件(* .TXT从),a.txtz.txt我的桌面,
现在如果我拖&下降了他们对我的ArgsPrinter_c++.exe直接位于C:\驱动器,
输出将类似于此:

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

My ArgsPrinter_c++.exesource code:

我的ArgsPrinter_c++.exe源代码:

#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 itselfran JUST FINE=> if your program exits too soon, the problem will be in your class...

我创建了一个简单的程序,它只创建了你的类的一个 sceleton 以便可以使用它,并且程序的主程序本身运行得很好 => 如果你的程序退出太快,问题将出在你的类中......

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;
}