I’m using a capture card dll which exports a function that provides an HGLOBAL handle containing a DIB. I want to write this DIB to a file. After I lock the resource and retrieve the data pointer, I request the size and it’s about 1mb. But then when I write it to a file, it’s always 0 bytes. This is the first time I’ve worked with an HGLOBAL type so I’m guessing that I’m doing something wrong. I appreciate your help.
char* dibData = (char*)LockResource(DIB);
if(NULL != dibData)
{
SIZE_T dibSize = GlobalSize (DIB);
f = fopen("thedib.dib", "wb");
if (f)
{
n = fwrite(dibData,dibSize,1,f);
fflush(f);
fclose(f);
wrote = true;
}
UnlockResource(DIB);
}
You seem to be mixing resource APIs and global memory APIs. If it’s really a resource, use LockResource(), SizeofResource(), UnlockResource(). If it’s just a memory block allocated via GlobalAlloc(), use GlobalLock(), GlobalSize(), GlobalUnlock().
Also, a .bmp file should start with a
BITMAPFILEHEADERstructure. This structure is not present for in-memory DIBs, so you need to write that structure prior to writing your DIB data.http://msdn.microsoft.com/en-us/library/dd183374%28v=vs.85%29.aspx