[英]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 传递给您的应用程序。
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,否则它的实用性有限。
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 调用都可用。
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.