Click here to Skip to main content
15,885,782 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
AnswerRe: Timer() - anything faster? Pin
Sander Rossel12-May-11 10:57
professionalSander Rossel12-May-11 10:57 
GeneralRe: Timer() - anything faster? Pin
Cyclone_S12-May-11 13:49
Cyclone_S12-May-11 13:49 
AnswerRe: Timer() - anything faster? Pin
Luc Pattyn12-May-11 14:00
sitebuilderLuc Pattyn12-May-11 14:00 
AnswerRe: Timer() - anything faster? Pin
Sander Rossel12-May-11 20:22
professionalSander Rossel12-May-11 20:22 
AnswerRe: Timer() - anything faster? Pin
Dave Doknjas12-May-11 12:00
Dave Doknjas12-May-11 12:00 
AnswerRe: Plugging shamelessly Pin
Luc Pattyn12-May-11 12:50
sitebuilderLuc Pattyn12-May-11 12:50 
AnswerRe: Timer() - anything faster? Pin
LiangGuangLin3-Jul-11 18:50
LiangGuangLin3-Jul-11 18:50 
QuestionStrange: CreateWindow fails, but it doesn't Pin
Foothill12-May-11 8:13
professionalFoothill12-May-11 8:13 
I'm still new to programming in Windows, so don't kill me if the answer is obvious.

What I've attempted to do is write my own WinMain function. I've based it off of Visual's generated code with a little Programming Windows mixed in combined with my own code.

Well it doesn't work, but I can't spot why.

#include <Windows.h>
#include <strsafe.h>

ATOM RegisterMyClass(HINSTANCE, wchar_t*);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void ErrorExit(LPTSTR lpszFunction);

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hpInst, LPSTR cmd, int nCmdShow)
{
	HWND hwnd;
	MSG msg;
	wchar_t myWndClass[] = L"AWindow";
	wchar_t myWndTitle[] = L"My Window";

	RegisterMyClass(hInst, myWndClass);

	hwnd = CreateWindowW(myWndClass, myWndTitle, WS_OVERLAPPEDWINDOW,
			     10, 10, 300, 300, NULL, NULL, hInst, NULL);

	if ( hwnd == NULL )
		ErrorExit(L"CreateWindow");

	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

	while ( GetMessage (&msg, NULL, 0, 0 ) )
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return 0;
}

ATOM RegisterMyClass(HINSTANCE hInst, wchar_t* wndClass)
{
	WNDCLASSEX w;
	
	w.cbSize = sizeof(WNDCLASSEX);
	w.style = CS_HREDRAW | CS_VREDRAW;
	w.lpfnWndProc = WndProc;
	w.cbClsExtra = 0;
	w.cbWndExtra = 0;
	w.hInstance = hInst;
	w.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	w.hCursor = LoadCursor(NULL, IDC_ARROW);
	w.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	w.lpszMenuName = NULL;
	w.lpszClassName = wndClass;
	w.hIconSm = NULL;

	return RegisterClassEx(&w);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
	HDC hdc;
	PAINTSTRUCT ps;

	switch (msg)
	{
	case WM_CREATE:
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		DefWindowProc(hWnd, msg, wp, lp);
	}
	return 0;
}

void ErrorExit(LPTSTR lpszFunction) 
{ 
    // Retrieve the system error message for the last-error code

    LPVOID lpMsgBuf;
    LPVOID lpDisplayBuf;
    DWORD dw = GetLastError(); 

    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );

    // Display the error message and exit the process

    lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, 
        (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR)); 
    StringCchPrintf((LPTSTR)lpDisplayBuf, 
        LocalSize(lpDisplayBuf) / sizeof(TCHAR),
        TEXT("%s failed with error %d: %s"), 
        lpszFunction, dw, lpMsgBuf); 
    MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); 

    LocalFree(lpMsgBuf);
    LocalFree(lpDisplayBuf);
    ExitProcess(dw); 
}


Using Microsofts own ErrorExit code, I get the following message when CreateWindow returns NULL:

CreateWindow failed with error 0: the operation completed successfully

What confuses me is that the MSDN documentation on CreateWindow() says that if the function fails, it returns NULL and to use GetLastError to find out what it was. Well, it returns a null, but then reports that there is no error.

I'm seriously boggled by this one.
AnswerRe: Strange: CreateWindow fails, but it doesn't Pin
John Schroedl12-May-11 11:02
professionalJohn Schroedl12-May-11 11:02 
GeneralRe: Strange: CreateWindow fails, but it doesn't Pin
Foothill12-May-11 11:30
professionalFoothill12-May-11 11:30 
QuestionHow to read data from COMPORT Pin
bqhoang10-May-11 16:46
bqhoang10-May-11 16:46 
AnswerRe: How to read data from COMPORT Pin
Luc Pattyn10-May-11 16:56
sitebuilderLuc Pattyn10-May-11 16:56 
AnswerRe: How to read data from COMPORT Pin
LiangGuangLin3-Jul-11 19:03
LiangGuangLin3-Jul-11 19:03 
GeneralRe: How to read data from COMPORT Pin
bqhoang3-Jul-11 19:46
bqhoang3-Jul-11 19:46 
Questiondecoration of Identifiers Pin
Bram van Kampen7-May-11 14:41
Bram van Kampen7-May-11 14:41 
AnswerRe: decoration of Identifiers Pin
Luc Pattyn7-May-11 15:06
sitebuilderLuc Pattyn7-May-11 15:06 
GeneralRe: decoration of Identifiers Pin
Bram van Kampen8-May-11 5:55
Bram van Kampen8-May-11 5:55 
GeneralRe: decoration of Identifiers Pin
Luc Pattyn8-May-11 6:01
sitebuilderLuc Pattyn8-May-11 6:01 
QuestionCreation of a Word 2007 document using the Open XML Format SDK using C++/CLI Pin
AthreyaRaghu4-May-11 2:11
AthreyaRaghu4-May-11 2:11 
AnswerRe: Creation of a Word 2007 document using the Open XML Format SDK using C++/CLI Pin
jschell4-May-11 8:45
jschell4-May-11 8:45 
QuestionHow can I pass different CmdLine arguments to the already running process in VC++? Pin
DotNet.Agrawal27-Apr-11 0:23
DotNet.Agrawal27-Apr-11 0:23 
AnswerRe: How can I pass different CmdLine arguments to the already running process in VC++? Pin
Richard MacCutchan27-Apr-11 1:39
mveRichard MacCutchan27-Apr-11 1:39 
QuestionHow to run Linux cpp excutable in winows os? Pin
mathivanaan26-Apr-11 17:06
mathivanaan26-Apr-11 17:06 
AnswerRe: How to run Linux cpp excutable in winows os? Pin
Luc Pattyn26-Apr-11 17:52
sitebuilderLuc Pattyn26-Apr-11 17:52 
AnswerRe: How to run Linux cpp excutable in winows os? Pin
DaveX8628-Apr-11 7:38
DaveX8628-Apr-11 7:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.