NO

Author Topic: messagebox in console program  (Read 16451 times)

vedro-compota

  • Guest
messagebox in console program
« on: October 15, 2011, 03:11:48 PM »
hi there one more time))

guys ? please tell me - why this code'll get an error =
 
Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#if defined(_WIN32) || defined (_WIN64)
#include <windows.h>
#define WINDOWSS 1
#endif



 #ifdef WINDOWSS
int main()
{
char* mtext = " hi Billy !! )) WE'RE IN WINDOWS NOW !))\n build as win32 CONSOLE program \n" ;
  printf("\n%s",mtext );
return TRUE;
     MessageBox(NULL,"Compiled as WIN32 program (not win 32 console program) (you'll need special functions to get console window)) !","Test",MB_OK);
}
#else
int main()
{
char* mtext = " Glory to the great hacker, Linus Torvalds! we're not in Windows!\n" ;
  printf("\n%s",mtext );
        return TRUE;
}
#endif
 


int WINAPI WinMain(HINSTANCE hInst,  HINSTANCE qwe,LPSTR qwer, int ss)
{
 
char* mtext = "  it's WinMain() code \n" ;
  printf("\n%s",mtext );
MessageBox(NULL,"Compiled as WIN32 program (not win 32 console program) (you'll need special functions to get console window)) !","Test",MB_OK);
 return 0;
 }
error 'll be =
Quote
POLINK: error: Unresolved external symbol '__imp__MessageBoxA@16'.
POLINK: fatal error: 1 unresolved external(s)
big thanks in advance ))

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: messagebox in console program
« Reply #1 on: October 15, 2011, 03:33:40 PM »
Did you read the whole description of the MessageBox function at MSDN?
Especially pay attention to the requirements box at the bottom of the article.

Did you include the library in the linker options of the project?



Personally, I would never mix GUI objects with console applications, you just get into trouble.



Mixing Windows API functions into the code will not allow it to run flawlessly on all platforms.
You will have to use a huge amount of "#if" statements.

The code for Linux runs perfectly well on Windows in this case.
---
Stefan

Proud member of the UltraDefrag Development Team

vedro-compota

  • Guest
Re: messagebox in console program
« Reply #2 on: October 15, 2011, 05:06:59 PM »
Stefan , thank you for good  advice)
as i understand the including <windows.h> isn't enough......but it's my experimental code ))  - i need it to understand c as os language )
please tell me what should i include in the code to get messagebox()   worked  ?

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: messagebox in console program
« Reply #3 on: October 15, 2011, 05:40:25 PM »
You can include the missing library using #pragma, but it is usually best to include it in the linker options of the project options.

Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#if defined(_WIN32) || defined (_WIN64)
    #include <windows.h>
    #define WINDOWSS 1

    #pragma comment(lib, "user32.lib")
#endif



 #ifdef WINDOWSS
int main()
{
char* mtext = " hi Billy !! )) WE'RE IN WINDOWS NOW !))\n build as win32 CONSOLE program \n" ;
  printf("\n%s",mtext );
return TRUE;
     MessageBox(NULL,"Compiled as WIN32 program (not win 32 console program) (you'll need special functions to get console window)) !","Test",MB_OK);
}
#else
int main()
{
char* mtext = " Glory to the great hacker, Linus Torvalds! we're not in Windows!\n" ;
  printf("\n%s",mtext );
        return TRUE;
}
#endif

2011-10-15 Edit: corrected lib pragma
« Last Edit: October 15, 2011, 06:17:38 PM by Stefan Pendl »
---
Stefan

Proud member of the UltraDefrag Development Team

CommonTater

  • Guest
Re: messagebox in console program
« Reply #4 on: October 15, 2011, 05:46:49 PM »
Try this....  as a console project...
Code: [Select]
#define WIN32_DEFAULT_LIBS
#include <windows.h>

int main (void)
{  char* str = "Hello World";

    MessageBox(NULL,str."Nice",MB_OK);

    return 0;
}
Console programs have access to the entire Windows API... They can even run message loops, register classes and create windows... However; Windows isn't Linux and it's a mistake to try and make it behave like Linux.

If you want actual windows GUI programs instead of console programs that use message boxes, you can get started with these tutorials...
http://www.winprog.org/tutorial/
« Last Edit: October 15, 2011, 05:59:07 PM by CommonTater »

vedro-compota

  • Guest
Re: messagebox in console program
« Reply #5 on: October 16, 2011, 07:19:54 AM »
Tater , your exaple doesn't work as a win32 console project , errors in messagebox() line are =
 error #2047: Expected a field name.
 error #2001: Syntax error: expected ')' but found 'string constant'.
 error #2070: Insufficient number of arguments to 'function'.
 error #2001: Syntax error: expected ';' but found 'string constant'.
error #2001: Syntax error: expected ';' but found ')'.
 error #2061: Illegal statement termination.

thank you for good tuturial!)))

Stefan , thanks for example of lib. including)
« Last Edit: October 16, 2011, 07:34:01 AM by vedro-compota »

CommonTater

  • Guest
Re: messagebox in console program
« Reply #6 on: October 16, 2011, 08:41:52 AM »
Tater , your exaple doesn't work as a win32 console project , errors in messagebox() line are =
 error #2047: Expected a field name.
 error #2001: Syntax error: expected ')' but found 'string constant'.
 error #2070: Insufficient number of arguments to 'function'.
 error #2001: Syntax error: expected ';' but found 'string constant'.
error #2001: Syntax error: expected ';' but found ')'.
 error #2061: Illegal statement termination.

thank you for good tuturial!)))

Stefan , thanks for example of lib. including)


Look closely... I made a mistake that you should have easily been able to fix... note that between the two strings I hit a dot instead of a comma... my bad... fix that and try again.  I did and it works here just fine.

Also note that it's MessageBox() not messagebox()... C is case sensitive...
« Last Edit: October 16, 2011, 08:44:33 AM by CommonTater »

vedro-compota

  • Guest
Re: messagebox in console program
« Reply #7 on: October 16, 2011, 08:51:43 AM »
ooohh..i'm a fool))) sory) you example work, but in this case i don't understand - why doesn't work this =
Code: [Select]
#include <stdio.h>
#include <stdlib.h>
#if defined(_WIN32) || defined (_WIN64)
#include <windows.h>
#define WINDOWSS 1
#endif
#define WIN32_DEFAULT_LIBS



 #ifdef WINDOWSS
int main()
{
char* mtext = " hi Billy !! )) WE'RE IN WINDOWS NOW !))\n build as win32 CONSOLE program \n" ;
  printf("\n%s",mtext );
    MessageBox(NULL," build as win32 CONSOLE program with message box !","Test",MB_OK);


return TRUE;
   
}
#else
int main()
{
char* mtext = " Glory to the great hacker, Linus Torvalds! we're not in Windows!\n" ;
  printf("\n%s",mtext );
        return TRUE;
}
#endif
error here is =
 POLINK: error: Unresolved external symbol '__imp__MessageBoxA@16'.
POLINK: fatal error: 1 unresolved external(s).
« Last Edit: October 16, 2011, 08:53:22 AM by vedro-compota »

CommonTater

  • Guest
Re: messagebox in console program
« Reply #8 on: October 16, 2011, 09:02:52 AM »
Move the #define WIn32_DEFAULT_LIBS *above* your #include <windows.h> ... it's probably not seeing the define.

And... since both of your code sections start with int main() you don't need them and the whole thing reduces to what I posted. 

Because Windows GUI programs are compiled differently than Windows Console programs, you cannot mix them in the same file.  That's why the Projects Wizard has separate selections for each... Also why the compiler has different entry points and the linker has different settings for each...

As I said, Windows isn't linux and it's a mistake to try to make it behave like linux.



vedro-compota

  • Guest
Re: messagebox in console program
« Reply #9 on: October 16, 2011, 09:22:02 AM »
Move the #define WIn32_DEFAULT_LIBS *above* your #include <windows.h> ... it's probably not seeing the define.

And... since both of your code sections start with int main() you don't need them and the whole thing reduces to what I posted. 

Because Windows GUI programs are compiled differently than Windows Console programs, you cannot mix them in the same file.  That's why the Projects Wizard has separate selections for each... Also why the compiler has different entry points and the linker has different settings for each...

As I said, Windows isn't linux and it's a mistake to try to make it behave like linux.



*above* - is right solution) your words sounds good and explanation is clear (about linker and entry points) thank you , mate)
I can't understand your thesis about windows - because i haven't deep knowledge of both of  these systems )
but it seems strange for me to have two different entry points in one os , because it's not important does this program show window on not especially if there's a console , which is window too  )

CommonTater

  • Guest
Re: messagebox in console program
« Reply #10 on: October 16, 2011, 10:59:37 AM »
The two entry points are in C ... not windows.  It's done for a reason, the information passed into a windows program is different than in a console program... The startup code in your executable has different jobs to do.  To give you one example... GUI windows need an instance handle which is not available on the Console command line (although it can be fetched by api calls) so the GUI entry point pass it into the program.    There are many other reasons --lots of them-- why the two separate modes are necessary.

It's not strange at all... It is quite simply, the way it is.


Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: messagebox in console program
« Reply #11 on: October 16, 2011, 11:20:00 AM »
You must do your homework first and understand the basics of developing for the operating systems.
You can't just start with zero knowledge at all.

You are mixing GUI and console programs, which is not supported on Windows.

Start reading the Entry Point section at MSDN.
You will find further information when searching MSDN for the different entry points.

If you want to learn, you have to listen.

BTW, programming is 90% research and only 10% writing code.
You did not spend 90% of your time for research for this small code snippet.
---
Stefan

Proud member of the UltraDefrag Development Team

vedro-compota

  • Guest
Re: messagebox in console program
« Reply #12 on: October 16, 2011, 11:30:41 AM »
Quote
You did not spend 90% of your time for research for this small code snippet.
Stefan , you're right at all you've said but not in this))) i've spend 95% but i'm ready to spent 99,9999% .
Thank you guys for good advices)  :)

CommonTater

  • Guest
Re: messagebox in console program
« Reply #13 on: October 16, 2011, 12:17:30 PM »
Quote
You did not spend 90% of your time for research for this small code snippet.
Stefan , you're right at all you've said but not in this))) i've spend 95% but i'm ready to spent 99,9999% .
Thank you guys for good advices)  :)

In all due respect... Stephan is right on this one... you certainly did not do your homework for this.  Instead you went off with your own bright idea only to discover that it cannot work... There are good reasons why things are done as they are... and even better reasons for us to do them in an OS conforming manner.



Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: messagebox in console program
« Reply #14 on: October 16, 2011, 12:52:22 PM »
Now I will stop criticizing and do something useful.

Here is how I would create your code.
There might be simpler ways to detect compiling for Windows, which involves checking only one definition, so we can get rid of our own definition.

Code: [Select]
#include <stdio.h>
#include <stdlib.h>

#if defined(_WIN32) || defined(_WIN64)
#define WIN32_DEFAULT_LIBS
#include <windows.h>
#define _NOT_LINUX_
#endif

#ifdef _NOT_LINUX_
#define MY_MESSAGE " hi Billy !! )) WE'RE IN WINDOWS NOW !))\n build as win32 CONSOLE program \n"
#else
#define MY_MESSAGE " Glory to the great hacker, Linus Torvalds! we're not in Windows!\n"
#endif /* _NOT_LINUX_ */

int main(void)
{
    char *mtext = MY_MESSAGE;
    printf("\n%s\n", mtext);

#ifdef _NOT_LINUX_
    MessageBox(NULL, " build as win32 CONSOLE program with message box !", "Test", MB_OK);
#endif /* _NOT_LINUX_ */

    return (int)TRUE;
}

If you want to write for multiple platforms get hold of the source code of open source projects, which support multiple platforms.
Lua is one of those projects.
It is always good to check existing code, when you start a project, since you can avoid repeating mistakes.
---
Stefan

Proud member of the UltraDefrag Development Team