Parsing command line

Started by
3 comments, last by Floru 23 years, 11 months ago
Hi folks! I have created a simple Win32 application. Now the program starts from WinMain, right? I can give command line parametres for the program and read them from the LPSTR lpCmdLine-variable. But what if I have code for a ms-dos program and want to run it. The ms-dos program has also ability to read command line parametres: int main(int argc, char* argv[])... I just cannot run it like main(lpCmdLine), it doesn''t work. Also I cannot use the GetCommandLine function from Windows. I tested it and it didn''t work. The ms-dos program is supposed to work in a separate thread so I cannot use the CreateProcess funtion... I think I have to parse the command line parameter somehow before giving it to the main() function... Could someone please help?? Thanks for any help Floru
Advertisement
This should work (include stdlib.h) - I think this is what you''re asking about:

char buffer[200];
sprintf(buffer, "%s %s", dosprogramname, lpcmdline);
system(buffer);

aig
aig
Thanks for the help but... It works if you wan''t to run it as a separate program using the system function. But I have to use the code from the dos program and start everything by calling it''s main not actually run a compiled program. It''s actually not a dos-program it''s made in ANSI-C and it''s supoosed to be used in many platforms.

Thanks for help
Floru
I''m not sure about threading, so I won''t comment on that.

There is a CommandLineToArgvW function that sounds like what you''re looking for, except it handles unicode. Otherwise, you''ll have to parse the line yourself (which isn''t too hard). First, count the number of arguments (ie. number of whitespaces + 1), declare a char *argv[numargs], then loop allocating space for each arg and copying the sections from lpcmdline (don''t forget to null terminate).

aig
aig
I found accidentally a (easy) solution. Here it is if you wan''t to check it out...

DWORD dwArgc = 0;
LPSTR* lpszArgv = NULL;

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
dwArgc = (DWORD) __argc;
lpszArgv = __argv;

main(dwArgc, lpszArgv);
}

Well... what can I say? It works!

Floru

This topic is closed to new replies.

Advertisement