I've tried to clean down this code so that it will demonstrate my problem but
contains little else. Just enough to make a window. This code will compile on
"BCC32" or "cl". I can't figure out why I get an "Invalid handle error" when
trying to create a DIB. I'm running AMD, XP pack 1. I've included a little
snippet that will show you the returned handle. For me, it's always "0". I'm
missing some point but I can't see what.
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <tchar.h>
HDC hDC;
void *ppvBits;
HWND hwnd;
MSG msg;
WNDCLASS wc;
HDC hDCDIB;
// ***********START********PROTOTYPES*****
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow);
LRESULT WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam);
// ***********END**********PROTOTYPES*****
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
(void)UNREFERENCED_PARAMETER(hPrevInstance);
(void)UNREFERENCED_PARAMETER(lpCmdLine);
(void)UNREFERENCED_PARAMETER(nCmdShow);
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon((HINSTANCE) NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor((HINSTANCE) NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszClassName = "DIB";
if(!RegisterClass(&wc))
return FALSE;
hwnd = CreateWindowEx(NULL,wc.lpszClassName,"",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,(HWND)0,(HMENU)0,hInstance,(LPVOID)0);
if(!hwnd)
return FALSE;
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
while(GetMessage(&msg, (HWND)0, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
} // ********************* END WINMAIN
LRESULT WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{ // ********************* START WNDPROC 1
switch(message)
{ // ******************* START SWITCH
case WM_CREATE:
{
//--------------------------------- BITMAPINFO Page 370
CONST BITMAPINFO *bmi;
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[1];
//---------------------------------- BITMAPINFOHEADER Page 364
DWORD biSize = (sizeof(BITMAPINFOHEADER));
LONG biWidth = 340;
LONG biHeight = 255;
WORD biPlanes = 1;
WORD biBitCount = 24;
DWORD biCompression = BI_RGB;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
HBITMAP DIBFile;
HDC hDC;
void *ppvBits;
hDC = GetDC(hwnd);
DIBFile = CreateDIBSection(hDC,bmi,DIB_RGB_COLORS,&ppvBits,0,0);
char Mystring[4];
_snprintf (Mystring,6,"%X",DIBFile);
MessageBox(hwnd,Mystring,"MBOX1",MB_OK);
break;
}
case WM_CLOSE:
{
PostQuitMessage(0);
DestroyWindow(hwnd);
return 0;
}
default:
return DefWindowProc(hwnd, message, wparam, lparam);
} // ****************** END SWITCH
return (0);
} // *********************************************** END WINPROC 1
/* ------------------------------------------ TESTING SUPPLIES
char Mystring[4];
_snprintf (Mystring,6,"%X",DIBFile);
MessageBox(hwnd,Mystring,"MBOX1",MB_OK);
LPTSTR lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,GetLastError(),MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)(&lpMsgBuf),0,NULL);
MessageBox( NULL, lpMsgBuf, "GetLastError", MB_OK|MB_ICONINFORMATION );
LocalFree( lpMsgBuf ); */