   void DrawBitmap(HDC hdcDest, HBITMAP hBitmap, int x, int y)
  I'm trying to take the following code from the Jonathan Harbor book and modify the DrawBitmap function so that you can load bitmap from a resource instead of from a file. I would like to do this so you can shuffle multiple images by number without being hampered by a string name. Any suggestions would be most appreciated. I've changed a few things acound and come up with this, but the bitmaps not loading.// Beginning Game Programming// Chapter 4// GameLoop project#include <windows.h>#include <winuser.h>#include <stdio.h>#include <stdlib.h>#include <time.h>#include <algorithm>#include <cmath>#define APPTITLE "Game Loop"//function prototypesLRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);ATOM MyRegisterClass(HINSTANCE);BOOL InitInstance(HINSTANCE,int);void DrawBitmap(HDC,HBITMAP,int,int);void Game_Init();void Game_Run();void Game_End();//local variablesHWND global_hwnd;HDC global_hdc;HBITMAP hBitmap;HINSTANCE hInstance;//the window event callback functionLRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){    global_hwnd = hWnd;    global_hdc = GetDC(hWnd);switch (message) {case WM_DESTROY:  Game_End();PostQuitMessage(0);break;    }return DefWindowProc(hWnd, message, wParam, lParam);}//helper function to set up the window propertiesATOM MyRegisterClass(HINSTANCE hInstance){    //create the window class structure    WNDCLASSEX wc;    wc.cbSize = sizeof(WNDCLASSEX);//fill the struct with info    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;    wc.hIconSm  = NULL;    //set up the window with the class info    return RegisterClassEx(&wc);}//helper function to create the window and refresh itBOOL InitInstance(HINSTANCE hInstance, int nCmdShow){    HWND hWnd;    //create a new window    hWnd = CreateWindow(  APPTITLE,    //window class  APPTITLE,    //title bar  WS_OVERLAPPEDWINDOW,   //window style  CW_USEDEFAULT,    //x position of window  CW_USEDEFAULT,    //y position of window  500,    //width of the window  400,    //height of the window   NULL,   //parent window  NULL,   //menu  hInstance,   //application instance  NULL);  //window parameters    //was there an error creating the window?    if (!hWnd) return FALSE;    //display the window    ShowWindow(hWnd, nCmdShow);    UpdateWindow(hWnd);    return TRUE;}//entry point for a Windows programint WINAPI WinMain(HINSTANCE hInstance,    HINSTANCE hPrevInstance,    LPSTRlpCmdLine,    int  nCmdShow){    int done = 0;MSG msg;// register the classMyRegisterClass(hInstance);    // initialize applicationif (!InitInstance (hInstance, nCmdShow)) return FALSE;    //initialize the game    Game_Init();    // main message loopwhile (!done){   if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))    {  //look for quit message  if (msg.message == WM_QUIT) done = 1;  //decode and pass messages on to WndProc  TranslateMessage(&msg;    DispatchMessage(&msg;   } //process game loop   Game_Run();}return msg.wParam;}void DrawBitmap(HDC hdcDest, HBITMAP hBitmap, int x, int y){    //HBITMAP hBitmap;    BITMAP bm;HDC hdcMem;    //load the bitmap image    //image = (HBITMAP)LoadImage(0,"131",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);hBitmap=LoadBitmap(hInstance, MAKEINTRESOURCE(131));    //read the bitmap's properties    GetObject(hBitmap, sizeof(BITMAP), &bm;    //create a device context for the bitmap    hdcMem = CreateCompatibleDC(global_hdc);SelectObject(hdcMem, hBitmap);    //draw the bitmap to the window (bit block transfer)    BitBlt(    global_hdc,    //destination device context   x, y,//x,y location on destination   bm.bmWidth, bm.bmHeight, //width,height of source bitmap    hdcMem,   //source bitmap device context   0, 0,//start x,y on source bitmap   SRCCOPY); //blit method    //delete the device context and bitmap    DeleteDC(hdcMem);    DeleteObject((HBITMAP)hBitmap);}void Game_Init(){    //initialize the game...    //load bitmaps, meshes, textures, sounds, etc.    srand(time(NULL));}void Game_Run(){    //this is called once every frame    //do not include your own loop here!   int x[]= {25,50,100,125,150,175}; int y[]= {25,50,100,125,150,175};int i;    RECT rect;//int deck[]={50,150,250,350,450};//std::random_shuffle(deck, deck+5);    GetClientRect(global_hwnd, ▭);    //if (rect.right > 0)    //{for(i=0; i<6; ++i){    //    x = rand() % (rect.right - rect.left);//   y = rand() % (rect.bottom - rect.top);   DrawBitmap(global_hdc,hBitmap, x, y);}    //}}void Game_End(){}
  Moving.   ;  This should do the trick:LoadImage( 0, MAKEINTRESOURCE( 131 ), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR );If 131 is really the resource define value for the bitmap. You should rather insert the actual define though.   ;  That didn't seem to make any difference. Here's the way he had it originally. I'm trying to get rid of all this "char* filename" stuff in the function definition so that I can call the bitmap by a number rather than "c.bmp" the file name.  void DrawBitmap(HDC hdcDest, char *filename, int x, int y){    HBITMAP image;    BITMAP bm;HDC hdcMem;    //load the bitmap image    image = (void*)LoadImage(0,"c.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);    //read the bitmap's properties    GetObject(image, sizeof(BITMAP), &bm;    //create a device context for the bitmap    hdcMem = CreateCompatibleDC(global_hdc);SelectObject(hdcMem, image);    //draw the bitmap to the window (bit block transfer)    BitBlt(    global_hdc,    //destination device context   x, y,//x,y location on destination   bm.bmWidth, bm.bmHeight, //width,height of source bitmap    hdcMem,   //source bitmap device context   0, 0,//start x,y on source bitmap   SRCCOPY); //blit method    //delete the device context and bitmap    DeleteDC(hdcMem);    DeleteObject((HBITMAP)image);}   ;  My bad, that's what i get when reading too fast:For loading anything from resources you need to provide the HINSTANCE handle which you get passed in the WinMain function:HBITMAP hbm = (HBITMAP)LoadImage( hInstance, MAKEINTRESOURCE( IDB_BITMAP ), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR );   ;  That didn't seem to work either. Here is the unaltered source code for the entire program. Copy and paste into your complier. Where it says "c.bmp" just use some small bitmap. You might have to put the address of the bitmap in there too then it works fine.    If you can see what you can do to change it to use an interger resource rather than a file name, it would be most appreciated.// Beginning Game Programming// Chapter 4// GameLoop project#include <windows.h>#include <winuser.h>#include <stdio.h>#include <stdlib.h>#include <time.h>#define APPTITLE "Game Loop"//function prototypesLRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);ATOM MyRegisterClass(HINSTANCE);BOOL InitInstance(HINSTANCE,int);void DrawBitmap(HDC,char*,int,int);void Game_Init();void Game_Run();void Game_End();//local variablesHWND global_hwnd;HDC global_hdc;//the window event callback functionLRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){    global_hwnd = hWnd;    global_hdc = GetDC(hWnd);switch (message) {case WM_DESTROY:  Game_End();PostQuitMessage(0);break;    }return DefWindowProc(hWnd, message, wParam, lParam);}//helper function to set up the window propertiesATOM MyRegisterClass(HINSTANCE hInstance){    //create the window class structure    WNDCLASSEX wc;    wc.cbSize = sizeof(WNDCLASSEX);//fill the struct with info    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;    wc.hIconSm  = NULL;    //set up the window with the class info    return RegisterClassEx(&wc);}//helper function to create the window and refresh itBOOL InitInstance(HINSTANCE hInstance, int nCmdShow){    HWND hWnd;    //create a new window    hWnd = CreateWindow(  APPTITLE,    //window class  APPTITLE,    //title bar  WS_OVERLAPPEDWINDOW,   //window style  CW_USEDEFAULT,    //x position of window  CW_USEDEFAULT,    //y position of window  500,    //width of the window  400,    //height of the window   NULL,   //parent window  NULL,   //menu  hInstance,   //application instance  NULL);  //window parameters    //was there an error creating the window?    if (!hWnd) return FALSE;    //display the window    ShowWindow(hWnd, nCmdShow);    UpdateWindow(hWnd);    return TRUE;}//entry point for a Windows programint WINAPI WinMain(HINSTANCE hInstance,    HINSTANCE hPrevInstance,    LPSTRlpCmdLine,    int  nCmdShow){    int done = 0;MSG msg;// register the classMyRegisterClass(hInstance);    // initialize applicationif (!InitInstance (hInstance, nCmdShow)) return FALSE;    //initialize the game    Game_Init();    // main message loopwhile (!done){   if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))    {  //look for quit message  if (msg.message == WM_QUIT) done = 1;  //decode and pass messages on to WndProc  TranslateMessage(&msg;    DispatchMessage(&msg;   }    //process game loop   Game_Run();}return msg.wParam;}void DrawBitmap(HDC hdcDest, char *filename, int x, int y){    HBITMAP image;    BITMAP bm;HDC hdcMem;    //load the bitmap image    image = (HBITMAP)LoadImage(0,"c.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);    //read the bitmap's properties    GetObject(image, sizeof(BITMAP), &bm;    //create a device context for the bitmap    hdcMem = CreateCompatibleDC(global_hdc);SelectObject(hdcMem, image);    //draw the bitmap to the window (bit block transfer)    BitBlt(    global_hdc,    //destination device context   x, y,//x,y location on destination   bm.bmWidth, bm.bmHeight, //width,height of source bitmap    hdcMem,   //source bitmap device context   0, 0,//start x,y on source bitmap   SRCCOPY); //blit method    //delete the device context and bitmap    DeleteDC(hdcMem);    DeleteObject((HBITMAP)image);}void Game_Init(){    //initialize the game...    //load bitmaps, meshes, textures, sounds, etc.    srand(time(NULL));}void Game_Run(){    //this is called once every frame    //do not include your own loop here!int x = 0, y = 0;    RECT rect;    GetClientRect(global_hwnd, ▭);    if (rect.right > 0)    {   x = rand() % (rect.right - rect.left);   y = rand() % (rect.bottom - rect.top);   DrawBitmap(global_hdc, "c.bmp", x, y);    }}void Game_End(){};  Ok, here's what i've done:1)Added a new resource, choose Bitmap, Import a file (.bmp)I kept the predefined name for the resource (IDB_BITMAP1)2)Added #include "resource".h3)Modified the line with LoadImage to:image = (HBITMAP)LoadImage( GetModuleHandle( NULL ),MAKEINTRESOURCE( IDB_BITMAP1 ), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR );This worked for me. Note that GetModuleHandle( NULL ) gives you the HINSTANCE handle for the starting process. Resources are always loaded from a certain instance. Note that the HMODULE you get from GetModuleHandle is castable to HINSTANCE. You could also pass the HINSTANCE parameter you get in your WinMain.I've used Visual Studio 2003, if you have a non Microsoft IDE the process to add the resource may vary.   ;Quote:Original post by EndurionNote that GetModuleHandle( NULL ) gives you the HINSTANCE handle for the starting process. Resources are always loaded from a certain instance. Note that the HMODULE you get from GetModuleHandle is castable to HINSTANCE. You could also pass the HINSTANCE parameter you get in your WinMain.Minor point: It's definitely worthwhile getting into the habbit of using the HINSTANCE from WinMain. Because if you use GetModuleHandle(NULL), you'll run into issues if your resources are in a DLL (Since GetModuleHandle(NULL) gives you the EXE handle, not the DLL handle).   ;  Are you compiling a 32-bit or 64-bit executable?I have a program that has been running for years.  Porting to 64-bits, none of the code using bitmaps works.  The GetObject call really crashes the program for me.   ;  That worked! Thanks so much. Also, I'm using Visual C++ 2005 6.0   