"hInstance" is undefined. c++ WIN32 APP

5.4k Views Asked by At

Win32 application. In the MyRegisterClass the wc.hInsance = hInstance.Apparently "hInstane is a undefined idetifier. Why's that ? Im using Visual Studio 2013 and im following Jonathan S Harbours book about Game Programming.

Code.

include <Windows.h>
include <time.h>
include <iostream>

using namespace std;

const string APPTITLE = "Game Loop";
HWND window;
HDC device;

bool gameover = false;

void DrawBitmap(char *filename, int x, int y){
    HBITMAP image = (HBITMAP)LoadImage(0, "c.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

    BITMAP bm;
    GetObject(image, sizeof(BITMAP), &bm);

    HDC hdcImage = CreateCompatibleDC(device);
    SelectObject(hdcImage, image);

    BitBlt(
           device,
           x, y,
           bm.bmWidth, bm.bmHeight,
           hdcImage,
           0, 0,
           SRCCOPY);

    DeleteDC(hdcImage);
    DeleteObject((HBITMAP)image);
}

bool Game_Init(){

    srand(time(NULL));

    return 1;
}

void Game_Run(){

    if (gameover == true)return;

    RECT rect;
    GetClientRect(window, &rect);
    int x = rand() % (rect.right - rect.left);
    int y = rand() % (rect.bottom - rect.top);
    DrawBitmap("c.bmp", x, y);
}

void Game_End(){
    ReleaseDC(window, device);
}

LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
    switch (message){

        case WM_DESTROY:{
            gameover = true;
            PostQuitMessage(0);
            break;
        }
            return DefWindowProc(hWnd, message, wParam, lParam);
    }

    ATOM MyRegisterClass(HINSTANCE hInstance); {
        WNDCLASSEX wc;
        wc.cbSize = sizeof(WNDCLASSEX);
        wc.style = CS_HREDRAW | CS_VREDRAW;
        wc.lpfnWndProc = (WNDPROC)WinProc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hInstance = hInstance;
        wc.hIcon = NULL;
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
        wc.lpszMenuName = NULL;
        wc.lpszClassName = APPTITLE.c_str();
        wc.hIconSm = NULL;
    }
}
2

There are 2 best solutions below

0
On

The end of your code should be :

}
ATOM MyRegisterClass(HINSTANCE hInstance) {
    WNDCLASSEX wc;
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC)WinProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = NULL;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = APPTITLE.c_str();
    wc.hIconSm = NULL;
    return ::RegisterClassEx(&wc);
}

Note :

  • no semicolon in MyRegisterClass definition
  • only one brace at the end of MyRegisterClass
  • add the closing brace above MyRegisterClass definition

That way code compiles correctly.

0
On

You have a stray semicolon in your program:

ATOM MyRegisterClass(HINSTANCE hInstance); { // <-- Delete this

Try deleting that and see if it fixes things.

Hope this helps!