running in windows and from command line

K

Ken Innes

I am trying to write a little program that can be run as a Windows
program or as a command line program. I'm really not sure the best way
to do this, so what I came up with so far was to check for command
line parameters (code shown below). This works, except that printf()'s
don't display anything. Is there a better way to do this?

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int pszCmdLine)
{
if(ParamStr(5)!="") {
main1(); //command line mode
return(0);
}
try
{
Application->Initialize();
Application->CreateForm(__classid(TForm1), &Form1);
Application->Run();
}
 
G

Gustav Svensson

You dont need any of that windows-api stuff, just write a normal main function:

int main()
{
printf("Hello world!!\n");
return 0;
}

and you should get a command line program.
 
B

Buster Copley

Ken said:
I am trying to write a little program that can be run as a Windows
program or as a command line program. I'm really not sure the best way
to do this, so what I came up with so far was to check for command
line parameters (code shown below). This works, except that printf()'s
don't display anything. Is there a better way to do this?

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int pszCmdLine)
{
if(ParamStr(5)!="") {
main1(); //command line mode
return(0);
}
try
{
Application->Initialize();
Application->CreateForm(__classid(TForm1), &Form1);
Application->Run();
}
 
U

Unforgiven

Ken said:
I am trying to write a little program that can be run as a Windows
program or as a command line program. I'm really not sure the best way
to do this, so what I came up with so far was to check for command
line parameters (code shown below). This works, except that printf()'s
don't display anything. Is there a better way to do this?

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int pszCmdLine)
{
if(ParamStr(5)!="") {
main1(); //command line mode
return(0);
}
try
{
Application->Initialize();
Application->CreateForm(__classid(TForm1), &Form1);
Application->Run();
}

And again, this is OFF TOPIC here. Go to the microsoft.public.* tree for
better results.
 
K

Ken Innes

Gustav Svensson said:
You dont need any of that windows-api stuff, just write a normal main function:

int main()
{
printf("Hello world!!\n");
return 0;
}

and you should get a command line program.

But I do need that windows-api stuff, because I want the program to
run as a Windows program with a Window-interface as well as on a
command line without a Window-interface.

Originally, I was hoping that I could just put a main() function in
there and that when run from the command line the program would
automatically call it instead of WinMain(), but that doesn't seem to
be the case.
 
K

Kevin Goodsell

Ken said:
But I do need that windows-api stuff, because I want the program to
run as a Windows program with a Window-interface as well as on a
command line without a Window-interface.

I'm not aware of any way to do such a thing, but there certainly is no
way using standard C++, therefore the issue is not topical here. You
should ask on a Windows programming group. Please read the welcome message:

http://www.slack.net/~shiva/welcome.txt

-Kevin
 
A

Alf P. Steinbach

But I do need that windows-api stuff, because I want the program to
run as a Windows program with a Window-interface as well as on a
command line without a Window-interface.

Originally, I was hoping that I could just put a main() function in
there and that when run from the command line the program would
automatically call it instead of WinMain(), but that doesn't seem to
be the case.

First, see Kevin Goodsell's message about topicality, and please
follow that advice.

Now, a standard C++ program (for a hosted implementation) _must_ have a
'main' function. Otherwise it isn't standard. Some compilers, among
them Windows compilers, allow e.g. 'WinMain' as a non-standard
extension to the language, but note well: all compilers I'm aware of
also support standard 'main' for any kind of program.

One solution to the apparent problem is to create two different
programs, of different kinds, where e.g. one uses the other. <ot>It would
be a good idea to rename the Windows console one to [.com]; that little
trick, which has to do with the Windows command interpreter's search for
programs, was used by e.g. MS Developer Studio 6.0.</ot>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top