Gå til innhold

Problem med å kompilere Windows-program


Anbefalte innlegg

Hei!

 

Jeg har et problem med å kompilere ett program som bruker Windows.h

 

main.cpp:

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  PAINTSTRUCT ps;
  switch (message)
  {
  case WM_CREATE:
  {
     return 0;
  }
  case WM_CLOSE:
  {
     PostQuitMessage(0);
     return 0;
  }
  case WM_PAINT:
  {
     BeginPaint(hwnd, &ps);
     EndPaint(hwnd, &ps);
  }
  default:
     break;
  }
  return DefWindowProc(hwnd,message,wParam,lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{

  WNDCLASSEX windowClass;

  windowClass.cbSize = sizeof(WNDCLASSEX);
  windowClass.style = CS_HREDRAW | CS_VREDRAW;
  windowClass.lpfnWndProc = (WNDPROC)WndProc;
  windowClass.cbClsExtra = 0;
  windowClass.cbWndExtra = 0;
  windowClass.hInstance = hInstance;
  windowClass.hIcon = NULL;
  windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  windowClass.hbrBackground = NULL;
  windowClass.lpszMenuName = NULL;
  windowClass.lpszClassName = "MyClass";
  windowClass.hIconSm = NULL;

  RegisterClassEx(&windowClass);

  hwnd = CreateWindowEx(

  NULL,
  "MyClass",
  "My Windows Application!",
  WS_OVERLAPPEDWINDOW |
  WS_CLIPSIBLINGS |
  WS_CLIPCHILDREN,
  0, 0,
  200, 200,
  NULL,
  NULL,
  hInstance,
  NULL);

  ShowWindow(hwnd, nCmdShow);
  UpdateWindow();

  return 0;
}

 

Når jeg kompilerer i Code::Blocks får jeg disse feilmeldingene:

 

In function `WinMain':

line 51, error: `hwnd' undeclared (first use in this function)

line 51, error: (Each undeclared identifier is reported only once

line 51, error: for each function it appears in.)

line 64, warning: passing arg 1 of `CreateWindowExA' makes integer from pointer without a cast

line 66, error: `nCmdShow' undeclared (first use in this function)

line 67, error: too few arguments to function `UpdateWindow'

=== Build finished: 5 errors, 1 warnings ===

 

Kan noen hjelpe meg med hva som er galt? Det ser iallefall ut som det er noe med CreateWindowEx()...

 

Takker for alle svar!

 

Miromurr

Endret av Miromurr
Lenke til kommentar
Videoannonse
Annonse
(snip)

 

In function `WinMain':

line 51, error: `hwnd' undeclared (first use in this function)

line 51, error: (Each undeclared identifier is reported only once

line 51, error: for each function it appears in.)

Du har ikke deklarert variablen hwnd

 

line 64, warning: passing arg 1 of `CreateWindowExA' makes integer from pointer without a cast

Litt usikker her men jeg vil tro at NULL er definert som [void* NULL = 0;] eller noe i den dur så når du prøver å sette en unsigned int til NULL så gir den er advarsel siden unsigned int ikke er en peker. Bare bruk 0 istede for NULL så fjerner du den advarselen.

 

line 66, error: `nCmdShow' undeclared (first use in this function)

Jeg tror du mente å bruke nShowCmd her :)

 

line 67, error: too few arguments to function `UpdateWindow'

Du må sende med window handlere (HWND) med funksjonen.

 

Lykke til

Lenke til kommentar

Windows programmer fungerer på samme måte som "dos" programmer, når programmet er ferdig så slutter de :)

 

Mener å huske at du må ha en loop ala dette.

 

	bool done;
MSG	msg;
done = false;

while(!done)
{
	if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
	{
		if (msg.message==WM_QUIT)
		{
			done = true;
		}
		else
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}
	else
	{
		//loop koden din her
	}
}

 

Lenke til kommentar

I PDF-versionen av kapittel 2 i "OpenGL Game Programming", forklarer de hvordan man skal sette opp ett vindu med "windows.h" (kapittelet følger med på CD-en til "Beginning OpenGL Game Programming"). Fordi jeg er ganske lat, tok jeg bare å copy/paste't koden inn i Code::Blocks... Men programmet vil ikke kompilere! Når jeg prøver å kompilere kildekoden som kan lastes ned fra nettsiden deres, funker det helt fint!

 

Koden fra PDF'en:

#define WIN32_LEAN_AND_MEAN // trim the excess fat from Windows

#include <windows.h>        // standard Windows app include

// the Windows Procedure event handler
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
  PAINTSTRUCT paintStruct;
  HDC hDC; // device context
  char string[] = "Hello, world!"; // text to be displayed

  switch(message)
  {
     case WM_CREATE: // window is being created
        return 0;
        break;

     case WM_CLOSE: // windows is closing
        PostQuitMessage(0);
        return 0;
        break;

     case WM_PAINT: // window needs updating
        hDC = BeginPaint(hwnd, &paintStruct);
        SetTextColor(hDC, COLORREF(0x00FF0000)); // set text color to blue
        TextOut(hDC, 150, 150, string, sizeof(string)-1); // display text in middle of window
        EndPaint(hwnd, &paintStruct);
        return 0;
        break;

     default:
        break;
  }

  return (DefWindowProc(hwnd, message, wParam, lParam));
}
// The application entry point
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
  WNDCLASSEX windowClass; // window class
  HWND hwnd;              // window handle
  MSG  msg;               // message
  bool done;              // flag saying when your app is complete

// fill out the window class structure
  windowClass.cbSize         = sizeof(WNDCLASSEX);
  windowClass.style          = CS_HREDRAW | CS_VREDRAW;
  windowClass.lpfnWndProc    = WndProc;
  windowClass.cbClsExtra     = 0;
  windowClass.cbWndExtra     = 0;
  windowClass.hInstance      = hInstance;
  windowClass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
  windowClass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  windowClass.hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);
  windowClass.lpszMenuName   = NULL;
  windowClass.lpszClassName  = "MyClass";
  windowClass.hIconSm        = LoadIcon(NULL, IDI_WINLOGO);

// register the window class
  if (!RegisterClassEx(&windowClass))
     return 0;

// class registered, so now create your window
  hwnd = CreateWindowEx(NULL,       // extended style
     "MyClass",                     // class name
     "A REAL Windows Application!", // app name
     WS_OVERLAPPEDWINDOW |          // window style
     WS_VISIBLE |
     WS_SYSMENU,
     100, 100,                      // x,y coordinate
     400, 400,                      // width, height
     NULL,                          // handle to parent
     NULL,                          // handle to menu
     hInstance,                     // application instance
     NULL);                         // no extra params

// check if window creation failed (hwnd would equal NULL)
  if (!hwnd)
     return 0;

  done = false; // initialize the loop condition variable

// main message loop
  while (!done)
  {
     PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE);

     if (msg.message == WM_QUIT) // do you receive a WM_QUIT message?
     {
        done = true; // if so, time to quit the application
     }
     else
     {
        TranslateMessage(&msg); // translate and dispatch to event queue
        DispatchMessage(&msg);
     }
  }

  return msg.wParam;
}

 

Samme kode fra nettsiden til boken:

#define WIN32_LEAN_AND_MEAN				// trim the excess fat from Windows

#include <windows.h>					// standard Windows app include

// the Windows Procedure event handler
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT paintStruct;
HDC hDC;							// device context
char string[] = "Hello, world!";		// text to be displayed

switch(message)
{
	case WM_CREATE:				// window is being created
		return 0;
		break;

	case WM_CLOSE:					// windows is closing
		PostQuitMessage(0);
		return 0;
		break;

	case WM_PAINT:					// window needs updating
		hDC = BeginPaint(hwnd, &paintStruct);
		SetTextColor(hDC, COLORREF(0x00FF0000));			// set text color to blue
		TextOut(hDC, 150, 150, string, sizeof(string)-1);	// display text in middle of window
		EndPaint(hwnd, &paintStruct);
		return 0;
		break;

	default:
		break;
}

return (DefWindowProc(hwnd, message, wParam, lParam));
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX windowClass;		// window class
HWND	   hwnd;			// window handle
MSG		   msg;				// message
bool	   done;			// flag saying when our app is complete

// fill out the window class structure
windowClass.cbSize			= sizeof(WNDCLASSEX);
windowClass.style			   = CS_HREDRAW | CS_VREDRAW;
windowClass.lpfnWndProc		= WndProc;
windowClass.cbClsExtra		= 0;
windowClass.cbWndExtra		= 0;
windowClass.hInstance		= hInstance;
windowClass.hIcon			   = LoadIcon(NULL, IDI_APPLICATION);			// default icon
windowClass.hCursor			= LoadCursor(NULL, IDC_ARROW);				// default arrow
windowClass.hbrBackground	= (HBRUSH)GetStockObject(WHITE_BRUSH);		// white background
windowClass.lpszMenuName	= NULL;										// no menu
windowClass.lpszClassName	= "MyClass";
windowClass.hIconSm			= LoadIcon(NULL, IDI_WINLOGO);			// windows logo small icon

// register the windows class
if (!RegisterClassEx(&windowClass))
	return 0;

// class registered, so now create our window
hwnd = CreateWindowEx(NULL,								// extended style
     "MyClass",						// class name
     "A REAL Windows Application!",	// app name
     WS_OVERLAPPEDWINDOW |
     WS_VISIBLE |
     WS_SYSMENU,	// style
     100, 100,							// x,y coordinate
     400, 400,							// width, height
     NULL,								// handle to parent
     NULL,								// handle to menu
     hInstance,						// application instance
     NULL);							// no extra params

// check if window creation failed (hwnd would equal NULL)
if (!hwnd)
	return 0;

done = false;						// intialize the loop condition variable

// main message loop
while (!done)
{
	PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE);

	if (msg.message == WM_QUIT)		// do we receive a WM_QUIT message?
	{
		done = true;				// if so, time to quit the application
	}
	else
	{
		TranslateMessage(&msg);		// translate and dispatch to event queue
		DispatchMessage(&msg);
	}
}

return msg.wParam;
}

 

Jeg er nesten helt sikker på at de to kodene er identiske (med unntak av diverse comments og lingnende), men allikevel er det bare kildekoden fra nettet som kompilerer! Her er feilmeldingene jeg får:

 

In function `WndProc':

25 error: syntax error before "COLORREF"

In function `WinMain':

43 error: `bool' undeclared (first use in this function)

43 error: (Each undeclared identifier is reported only once

43 error: for each function it appears in.)

43 error: syntax error before "done"

75 warning: passing arg 1 of `CreateWindowExA' makes integer from pointer without a cast

81 error: `done' undeclared (first use in this function)

81 error: `false' undeclared (first use in this function)

86 warning: passing arg 3 of `PeekMessageA' makes integer from pointer without a cast

86 warning: passing arg 4 of `PeekMessageA' makes integer from pointer without a cast

90 error: `true' undeclared (first use in this function)

=== Build finished: 8 errors, 3 warnings ===

 

Noen som kan hjelpe meg med dette?

 

Miromurr

Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
  • Hvem er aktive   0 medlemmer

    • Ingen innloggede medlemmer aktive
×
×
  • Opprett ny...