简体   繁体   中英

How to read command line(argv) in a form of application of C++ Builder?

Using C++ with Borland , I tried to read command line argv

I need in the form, when user click one button it will read the value of command line argv . but the problem is, I cannot find void main(int argc, char ** argv) .

I have tried that in debug, I put argv or in the cpp file of form I put LPSTR , but it doesn't work.

I found that there is only WinMain , what is more, in the cpp file of form, there is no "WinMain", may I ask how to get it ?

Typically in a C++ Builder GUI project, you do not have a standard C++ main() function. Whilst you can use the Win32 GetCommandLine API function, you still have to parse the command line into separate arguments. So the runtime offers helper functions to read command line arguments, like System::ParamStr() , System::Sysutils::FindCmdLineSwitch , etc.

You can use the GetCommandLine function in conjunction with CommandLineToArgvW .

Note that the latter is only available in wide character version.

Alternatively you can replace WinMain with a standard main , and change the build options to use standard main , if that's supported (it is with Visual C++ and with GNU g++). Or if the compiler supports this language extension you can use the wide character wmain . A problem with the standard main is that the arguments are encoded as Windows ANSI, which among other shortcomings means that they cannot represent all valid Windows filenames, while the arguments of wmain , and the arguments retrieved byu CommandLineToArgvW , can.


A final alternative is to parse the command line that you get as fourth argument of WinMain . However, that has no advantages. WinMain is a residual from the 16-bit Windows days, it has never had a technical purpose, and is best removed if possible.


All the above solutions work with not just Borland C++ Builder but with any Windows C++ compiler.

You can either:

  1. use the RTL's ParamCount() and ParamStr() functions in System.hpp .

  2. use the C runtime's global _argc and _argv variables in stdlib.h .

  3. Use the Win32 API's GetCommandLineW() and CommandLineToArgvW() functions in windows.h and shellapi.h , respectively.

As for WinMain() , it is located in your project's main .cpp file, which is different than the .cpp file that your Form is implemented in. In the Project Manager, right-click on your .exe file and choose View Source . Or simply open the <ProjectName>.cpp file directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM