f



running in windows and from command line

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();
    }
0
primehalo (11)
9/17/2003 9:34:10 PM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

7 Replies
659 Views

Similar Articles

[PageSpeed] 40

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.

-- 
Gustav Svensson
gurra@matris.org
0
gurra (2)
9/17/2003 9:42:21 PM
Ken Innes wrote:
> 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();
>     }

0
buster (84)
9/17/2003 9:47:58 PM
Buster Copley wrote [nothing]

Sorry. Slipped.

0
buster (84)
9/17/2003 9:48:47 PM
Ken Innes wrote:
> 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.

-- 
Unforgiven

"Most people make generalisations"
Freek de Jonge

0
jaapd3000 (157)
9/17/2003 10:44:54 PM
Gustav Svensson <gurra@matris.org> wrote in message news:<20030917234735.7301d4bf.gurra@matris.org>...
> 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.
0
primehalo (11)
9/18/2003 10:28:48 PM
Ken Innes wrote:
> 
> 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
-- 
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

0
9/18/2003 10:34:08 PM
On 18 Sep 2003 15:28:48 -0700, primehalo@hotmail.com (Ken Innes) wrote:

>Gustav Svensson <gurra@matris.org> wrote in message news:<20030917234735.7301d4bf.gurra@matris.org>...
>> 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.

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>

0
alfps (7389)
9/18/2003 11:37:45 PM
Reply: