简体   繁体   English

如何获得 Win32 C/C++ 程序的命令行选项?

[英]How do I get the command line option for my Win32 C/C++ program?

I have a C/C++ winapi program that I'd like to extend a command line functionality to (which I've seen done in C#, but never C++).我有一个 C/C++ winapi 程序,我想将命令行功能扩展到(我在 C# 中看到过,但从未在 C++ 中看到过)。 If the executable is opened with no arguments, it opens the window as normal, but when called from a command line with arguments such as an input or output file, the window does not open and all user interaction is done through the command line. If the executable is opened with no arguments, it opens the window as normal, but when called from a command line with arguments such as an input or output file, the window does not open and all user interaction is done through the command line. How could I accomplish this?我怎么能做到这一点? Preferably I'd like to be able to do it in C, as that's where my WinMain() function is.最好我希望能够在 C 中执行此操作,因为那是我的 WinMain() function 所在的位置。

Thanks in advance for any help!提前感谢您的帮助!

There is LPSTR lpCmdLine parameter in WinMain . WinMain中有LPSTR lpCmdLine参数。 You can use CommandLineToArgvW function to parse lpCmdLine .您可以使用CommandLineToArgvW function 来解析lpCmdLine When necessary parameters exist, you will not create or show program window and do the job.当存在必要的参数时,您将不会创建或显示程序 window 并执行该工作。

There are several ways you can get the (parsed) command line arguments passed to your application.有几种方法可以获得(解析的)命令行 arguments 传递给您的应用程序。

Entry point入口点

The CRT passes the command line into the user-provided entry point ( WinMain ) as the third argument. CRT 将命令行作为第三个参数传递到用户提供的入口点 ( WinMain )。 Depending on whether your application is compiled for Unicode or not this is either a wide character string or a narrow character string.取决于您的应用程序是否为 Unicode 编译,这是宽字符串还是窄字符串。 Using the narrow character version cannot be guaranteed to work when accepting input you do not control.在接受您无法控制的输入时,不能保证使用窄字符版本。 The command line is accepting input you do not control, so you must compile for Unicode (by defining the UNICODE and _UNICODE preprocessor symbols).命令行正在接受您无法控制的输入,因此您必须针对 Unicode 进行编译(通过定义UNICODE_UNICODE预处理器符号)。

Either way, the command line is passed as a single character string.无论哪种方式,命令行都作为单个字符串传递。 As such it is of limited utility unless you parse out the individual arguments.因此,除非您解析出单独的 arguments,否则它的实用性有限。

Using the Windows API使用 Windows API

The Windows API provides the GetCommandLineW function that allows you get a pointer to the command line at any point in your program. Windows API 提供了GetCommandLineW function,它允许您在程序中的任何位置获得指向命令行的指针。 Again, this only returns a single character string.同样,这仅返回单个字符串。 The string can be parsed into individual arguments by calling CommandLineToArgvW , producing both an array of arguments as well as its size.可以通过调用CommandLineToArgvW将字符串解析为单独的 arguments ,从而生成 arguments 数组及其大小。 Take note that there is no narrow character version of CommandLineToArgvW , so you cannot apply it to the command line argument passed into WinMain if you aren't compiling for Unicode.请注意, CommandLineToArgvW没有窄字符版本,因此如果您不为 Unicode 编译,则不能将其应用于传递给WinMain的命令行参数。

Regardless, both of these API calls are available even if you aren't compiling for Unicode.无论如何,即使您没有为 Unicode 编译,这两个 API 调用都可用。

CRT阴极射线管

If you don't care about portability, Microsoft's CRT implementation provides the __argc, __argv, __wargv global variables, that can be used anywhere in your program to get the parsed command line.如果您不关心可移植性,Microsoft 的 CRT 实现提供了__argc、__argv、__wargv全局变量,可以在程序中的任何位置使用它们来获取已解析的命令行。 While convenient, it depends on the preprocessor symbols defined whether __wargv stores a valid pointer.虽然方便,但它取决于定义的预处理器符号__wargv是否存储有效指针。 It only does if _UNICODE is defined.仅当定义了_UNICODE时才会这样做。

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

相关问题 如何在 C++/Win32 中为我的程序禁用程序兼容性助手? - How can I disable the Program Compatibility Assistant for my program in C++/Win32? 如何在 C/C++ Win32 程序中终止以 _spawnl() 启动的进程? - How do I terminate a process started with _spawnl() in a C/C++ Win32 program? 如何在 Win32 C++ 程序中以编程方式最大化顶级 window? - How do I programmatically maximize a top-level window in a Win32 C++ program? 筛选C ++ / Win32命令行输出消息 - Filtering C++/Win32 command line output message 如何获得刷子的句柄,Win32 C ++ - How do I get the handle to a brush, Win32 C++ 在给定_EXCEPTION_POINTERS结构的情况下,如何获取导致结构化异常的模块名称? (win32 C ++) - How do I get the module name that caused a structured exception given a _EXCEPTION_POINTERS struct? (win32 C++) 如何在win32 c ++中打印预览? - how do I do print preview in win32 c++? win32 滚动条在 C/C++ 程序中不起作用 - win32 scrollbar in not working in C/C++ program 你如何在win32下的C++中首先实例化一个全局变量? - How do you get a global var to be instantiated as the very first thing in C++ under win32? 如何将 Toast 进度条与我的 C++ Win32 应用程序绑定? - How may I bind a Toast progress bar with my C++ Win32 application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM