hi there one more time))
guys ? please tell me - why this code'll get an error =
#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 =
QuotePOLINK: error: Unresolved external symbol '__imp__MessageBoxA@16'.
POLINK: fatal error: 1 unresolved external(s)
big thanks in advance ))
Did you read the whole description of the MessageBox function at MSDN (http://msdn.microsoft.com/en-us/library/windows/desktop/ms645505%28v=vs.85%29.aspx)?
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 , 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 ?
You can include the missing library using #pragma, but it is usually best to include it in the linker options of the project options.
#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
Try this.... as a console project...
#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/
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)
Quote from: vedro-compota 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)
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...
ooohh..i'm a fool))) sory) you example work, but in this case i don't understand - why doesn't work this =
#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).
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.
Quote from: CommonTater 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.
*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 )
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.
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 (http://msdn.microsoft.com/en-us/library/f9t8842e%28VS.71%29.aspx) 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.
QuoteYou 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) :)
Quote from: vedro-compota on October 16, 2011, 11:30:41 AM
QuoteYou 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.
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.
#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 (http://www.lua.org/) is one of those projects.
It is always good to check existing code, when you start a project, since you can avoid repeating mistakes.
Hi Stephan;
If you really want to mess with someone...
#include <windows.h>
int main (void)
{ return MessageBox(NULL,"Click Yes or No....","Choose",MB_YESNO | MB_ICONINFORMATION); }
The return value is useable in Batch files (.bat) error levels and can be used to control branching according to the user's input...
Example using that idea:
// ChooseYesNo.c
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
//int __cdecl WinMainCRTStartup(void)
int __cdecl mainCRTStartup(void)
{
ExitProcess(MessageBox(0, "Yes or No", "Choose", MB_YESNO | MB_ICONINFORMATION));
}
@ECHO OFF
@REM YesNo.bat
@ChooseYesNo.exe
@ECHO %ERRORLEVEL%
@IF NOT ERRORLEVEL 7 GOTO CaseYes
@ECHO No
GOTO TheEnd
:CaseYes
@ECHO Yes
GOTO TheEnd
:TheEnd
@ECHO TheEnd