简体   繁体   English

通过双击关联文件打开c ++程序。 如何获得文件名?

[英]Opening c++ program by double clicking associated file. How do I get the file name?

I have a c++ program that I associated with .bin files, so that whenever the .bin file is opened, it is opened with myProgram.exe. 我有一个与.bin文件相关联的c ++程序,因此无论何时打开.bin文件,都将使用myProgram.exe打开它。

How do I get the filename of the associated file that opened my program? 如何获取打开程序的关联文件的文件名?

The process of opening a file via its extension is controlled by a set of entries in the system registry. 通过文件扩展名打开文件的过程由系统注册表中的一组条目控制。 One of the final steps specifies the command that should be executed, and usually the file name is included in the command. 最后一个步骤之一是指定应执行的命令,通常文件名包含在命令中。 Check your command line parameters to see if the file name is given. 检查您的命令行参数以查看是否提供了文件名。

The process is described here: http://msdn.microsoft.com/en-us/library/cc144175(v=vs.85).aspx 此处描述了该过程: http : //msdn.microsoft.com/zh-cn/library/cc144175(v=vs.85).aspx

IIRC, it will be the second command line parameter passed into your application. IIRC,它将是传递到您的应用程序中的第二个命令行参数。 The first parameter will the name of your actual program (executable). 第一个参数将是您实际程序的名称(可执行文件)。

In plain C++ you can use the arguments of main : 在普通的C ++中,您可以使用main的参数:

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

But there is a problem with that , namely that C and C++ main was designed for *nix, not Windows. 但这有一个问题 ,即C和C ++ main是为* nix而不是Windows设计的。 The Holy C++ Standard recommends that the runtime should supply the main arguments UTF-8-encoded, but alas, that does not happen with conventional Windows C++ compilers. Holy C ++标准建议运行时提供UTF-8编码的main参数,但可惜,传统Windows C ++编译器不会发生这种情况。 So in Windows this method might not work for filenames with eg Norwegian, Greek or Russian characters. 因此,在Windows中,此方法可能不适用于带有挪威语,希腊语或俄语字符的文件名。

In Windows, for a program intended to be used by others you can instead use the (Unicode version of the) Windows API function GetCommandLine , and its parser cousin CommandLineToArgvW in order to split the command line into individual arguments. 在Windows中,对于打算供其他人使用的程序,您可以使用Windows API函数GetCommandLine (的Unicode版本)及其解析器表亲CommandLineToArgvW ,以便将命令行拆分为单独的参数。

Alternatively, with Microsoft's implementations of the C and C++ languages you can use the non-standard startup function wmain . 另外,通过Microsoft的C和C ++语言实现,您可以使用非标准启动功能wmain I don't recommend that, however. 但是,我不建议这样做。 I think it is a good idea to keep to the international standards for C and C++ as much as practically possible, and there is certainly no pressing reason to use wmain , even though it can be convenient for small toy programs. 我认为在实践中尽可能地保持C和C ++的国际标准是一个好主意,即使使用小型玩具程序也很方便,使用wmain肯定没有紧迫的理由。

Cheers & hth., 干杯&hth。,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Delphi 获取与文件扩展名关联的程序名称? - How can I get the Name of the Program associated with a file extension using Delphi? 如何访问与批处理文件关联的原始文件名 - How do I access the original file name that is associated a batch file 如何使用C#检查与文件扩展名关联的程序是否有效? - How do I use C# to check if program associated with file extension is valid? 如何在C ++中打开文件(即.txt文件)(有点像在Windows中双击它)? - how to open a file (ie. .txt file) in C++ (kinda like double clicking it in windows)? 如何在C++中获取可执行文件名? - How to get the executable file name in C++? Windows/C++:如何确定与共享驱动器关联的共享名称? - Windows/C++: How do I determine the share name associated with a shared drive? 如何检查C ++中的文件是否存在于Windows程序中? - How do I check whether a file exists in C++ for a Windows program? 如何让我的Qt C ++程序在Windows中打开控制台? - How do I keep my Qt C++ program from opening a console in Windows? 将Windows批处理连接到html目录,该路径将回显到文件中。 如何阻止文件名被包含? - Concatenating a directory of html with windows batch, the path is echoed into the file. How do I stop the file name from being included? 如何使用 Windows 程序在 C++ 中获得控制台输出? - How do I get console output in C++ with a Windows program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM