Simple messagebox LNK ERROR

I just started learning windows programming and my first program wont link correctly.

here is the error:

fatal error LNK1120: 1 unresolved externals

and here is the code:

1
2
3
4
5
6
7
#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstace, HINSTANCE hPrevInstance,
				   PSTR cmdLine, int showCmd)
{
	MessageBox(0, "First Win32 Program", "Window Title", MB_OK);
}
Last edited on
It should tell you what the unresolved external is. can you provide that additional information?

I am guessing that is is "main" or some variation thereof, and you have failed to specify your project as a Windows Application, rather than a console one. If you are unsure how to do this, I could probably help you with Visual Studio or Code::Blocks. What IDE are you using?
Last edited on
Oh my bad im using VC++ 2010 Express Edition and the whole error is:

1
2
3
1
1>LINK : error LNK2001: unresolved external symbol _WinMainCRTStartup
1>C:\Users\david\Documents\Visual Studio 2010\Projects\Win32 Module II\Debug\Win32 Module II.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Hmm... I've just successfully built your code in a new empty Win32 project in Visual Studio 2010.

Try setting WINDOWS as the Subsystem setting in the linker settings (Project -> Properties -> Configuration Properties -> Linker -> System -> Subsystem). If you are using both Debug and release configurations, remember to change it for both.

--------------------------
On a side note, consider wrapping your string literals with _T( ) when passing them to WinAPI functions.
MessageBox could turn into MessageBoxA (ASCII) or MessageBoxW (Unicode) depending on project settings. You are passing it ASCII strings, so if you were to change your project to Unicode, it would't compile.

If your project is using ASCII strings, _T() resolves to nothing during preprocessing. If your project is Unicode, it resolves to L, which makes your string literals Unicode. Thus it means your program will compile as ASCII and Unicode. Just a small tip :)
Oh alright i fixed it but now i have a new problem. Should i make a new topic for it?

if not:

my problem now is that to make the MessageBox function work i have to write it as MessageBoxA. Why is this?
This is what I was talking about in my previous post. It is because MessageBox is resolving to MessageBoxW (Unicode) not MessageBoxA (ASCII). (MessageBox itself is a macro...)

As you are getting MessageBoxW, you must pass Unicode literals. But the safest thing to do is #include the <tchar.h> header and wrap the string literals you pass to it in _T(), which will turn them into Unicode where necessary, but it won't if it's unnecessary. A long term and durable fix :)
Oh dude your last post wasnt there when i wrote my last post lol.

when you said

"If you are using both Debug and release configurations, remember to change it for both."

I changed the subsystem setting to WINDOWS, but how do i do it for Debug and relase configurations?

EDIT: never mind i figured that part out. BUT i still need to write it as 'MessageBoxA'. In the book im reading they dont have to write it like that. And they dont add in all those includes and _T's or whatever they type it in exactly the way i have it writtin in the OP
Last edited on
When you're in the project settings dialog, at the top on the left there should be a dropdown to change between Debug and Release. You can also select "All Configurations" to allow you to apply a setting to both configurations at once.

To build your project in a particular configuration, you select it from the dropdown on one of the toolbars at the top of the screen (not in the properties pages, but in the main IDE window).
Last edited on
Yeah i guess this is solved. Im just confused why i have to add 'A' to the end of MessageBox when the example im copying it from doesnt :/.

If i add the A is works perfectly but im changing the original code and i'de rather have it work the way the original code has it.
Don't put the A at the end. As I said, MessageBox depends on project settings, but if you put your string literals inside _T(): "hello world!" -> _T("hello world!") then the problem is solved in the best possible way and you can leave MessageBox without the A. read my previous posts for some explanation as to why this is necessary.
What errors do you get when you use MessageBox without the A?
@ L B

1
2
3
4
1
1>  WinMain.cpp
1>c:\users\david\documents\visual studio 2010\projects\win32 project\winmain.cpp(6): error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [20]' to 'LPCWSTR'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


@Xander314

Whats the difference? both ways solve the problem except adding an A is faster than using that _T thing.
Sorry, I was thinking about something else actually... You can use the A one if you want. Just note that you won't be able to do do that if you want to display a unicode string.
nano511, it's giving you that error because it's trying to use Unicode and you want to use ASCII. You need to remove your #define UNICODE
How do i do that LB?
You select the line, then hit backspace...unless you didn't type it in the first place, in which case there's problems.
Yeah the code is what's in the OP.

I'm pretty sure i diddnt type any Unicode stuff...
Well, instead of typing a string literal:
"some string"
type a wide string literal:
L"some Unicode string"
or better yet, do what has been recommended:
_T("some string that may or may not be Unicode") just make sure that you #include <tchar.h> after the windows.h include
Last edited on
According to Disch, typing the "L" is not a good option with the Windows API, because all the code would break were you to change your project to non Unicode.

Therefore, either to the A, or (better still, IMO), use the _T( ). Of course, you should only do this to the string literals which you pass to WinAPI functions.

Here is an extensive article on the subject:
http://cplusplus.com/articles/Gw6AC542/
Topic archived. No new replies allowed.