Click to See Complete Forum and Search --> : Windows Graphics Programming book, WinMainCRTStartup() and VC++ 2005


RSSole
May 25th, 2006, 09:46 AM
I've just started reading Win Graphics Programming by Feng Yuan book. Unfortunately, problem have arisen with one of first sample programs, here is source:

#define STRICT
#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <tchar.h>
#include <assert.h>

void CenterText(HDC hDC, int x, int y, LPCTSTR szFace,
LPCTSTR szMessage, int point)
{
HFONT hFont = CreateFont(- point * GetDeviceCaps(hDC, LOGPIXELSY) / 72,
0, 0, 0, FW_BOLD, TRUE, FALSE, FALSE,
ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS,
PROOF_QUALITY, VARIABLE_PITCH, szFace);
// assert(hFont);

HGDIOBJ hOld = SelectObject(hDC, hFont);

SetTextAlign(hDC, TA_CENTER | TA_BASELINE);

SetBkMode(hDC, TRANSPARENT);
SetTextColor(hDC, RGB(0, 0, 0xFF));
TextOut(hDC, x, y, szMessage, _tcslen(szMessage));

SelectObject(hDC, hOld);
DeleteObject(hFont);
}

const TCHAR szMessage[] = _T("Hello, World");
const TCHAR szFace[] = _T("Times New Roman");

#pragma comment(linker, "-merge:.rdata=.text")
#pragma comment(linker, "-align:512")

extern "C" void WinMainCRTStartup()
{
HDC hDC = GetDC(NULL);
// assert(hDC);

CenterText(hDC, GetSystemMetrics(SM_CXSCREEN) / 2,
GetSystemMetrics(SM_CYSCREEN) / 2,
szFace, szMessage, 72);

ReleaseDC(NULL, hDC);
ExitProcess(0);
}

When I' ve tried to build application, linker reported error that there is unresolved external reference (if I understood well - external reference on WinMainCRTStartup). I know that sample is created with VC++ 6 (I am using VC++ 2005) and that is probably cause of problem but I've not been able to solve it yet. Even replaced WinMainCRT with main() and WinMain(...) but that didn't solve problem or did cause unexpected behavior of program. Any suggestion/advice on this problem would be appreciated. Thanks in advance.

Andreas Masur
May 25th, 2006, 10:41 AM
[ Redirected thread ]

philkr
May 25th, 2006, 11:37 AM
The function WinMainCRTStartup() is part of the C/C++ Runtime Library. You should not implement it in your application. Put the code in WinMain() instead.

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd)
{
HDC hDC = GetDC(NULL);
// assert(hDC);

CenterText(hDC, GetSystemMetrics(SM_CXSCREEN) / 2,
GetSystemMetrics(SM_CYSCREEN) / 2,
szFace, szMessage, 72);

ReleaseDC(NULL, hDC);
return 0;
}

If you still have errors this way, please post them and we may find out what is also wrong.

nolxev
May 25th, 2006, 04:11 PM
I've just started reading Win Graphics Programming by Feng Yuan book. Unfortunately, problem have arisen with one of first sample programs, here is source:
.....
.....


If you open the DSP/DWS files that comes with the book the code will compile because if you're creating that code from zero the problems are in "project setting". To compile your code make you sure that your project setting are set as the following ones:

C/C++
- Code Generation
- - Basic Runtime Checks = Default

Linker
- General
- - Enable Incremental Linking = No
- - Suppress Startup Banner = Yes

It should works now (I hope I remember correctly, times ago I got the same problem).

But as philkr said before, try to avoid that and use WinMain instead in your application if you target is not a competition to write the smallest code.

RSSole
May 26th, 2006, 08:53 AM
The function WinMainCRTStartup() is part of the C/C++ Runtime Library. You should not implement it in your application. Put the code in WinMain() instead.

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd)
{
HDC hDC = GetDC(NULL);
// assert(hDC);

CenterText(hDC, GetSystemMetrics(SM_CXSCREEN) / 2,
GetSystemMetrics(SM_CYSCREEN) / 2,
szFace, szMessage, 72);

ReleaseDC(NULL, hDC);
return 0;
}

If you still have errors this way, please post them and we may find out what is also wrong.

First, thanks for help :)

Yes, this solved compliation problem, however, program behaves... strange.
According to topic in book it should display blue text in center of the screen, but with code changed this way, screen becomes whole white with no text ("Hello world") at all... I know that my questions are quite boring but I am just trying to deal with topics in book :) Do you think I should get VC++6 (although already got VC++ 2005? ) :(

Browsing MSDN I've found out that WinMainCRTStartup is single threaded and single threading is no longer supported in VC++ 2005...etc.

nolxev... I've changed options you suggested but linker still reports error...

Any (further) help would be appreciated :) I'am sorry for being annoying :)

nolxev
May 26th, 2006, 09:33 AM
I remember I was on VC++ 6 when I got the same problems and with those settings WinMainCRTStartup() works fine, I have not VC++ 2005 :)