CreateDIBSection issue

Started by
10 comments, last by Dragon_Strike 15 years, 9 months ago
im using CreateDIBSection to get a HBITMAP handle... my problem is that CreateDIBSection automaticly allocates memory... however id like to decide myself what memory should be used... is that possible?
Advertisement
I was recently doing something similar (trying to draw from my own memory, instead of from memory allocated by windows); I ended up using the SetDIBitsToDevice function.

	const unsigned char* pData = myPixelMemory...;	BITMAPINFO bmpInfo;	UINT colours, bpp;	colours = format == RGB ? 3 : 4;	bpp = BitsPerPixel();		memset(&bmpInfo, 0, sizeof(bmpInfo));	bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);	bmpInfo.bmiHeader.biWidth = uiWidth;	bmpInfo.bmiHeader.biHeight = -(int)uiHeight;	bmpInfo.bmiHeader.biPlanes = 1;	bmpInfo.bmiHeader.biBitCount = bpp;	int ok = SetDIBitsToDevice(hdcWin, 		0, 0, 		bmpInfo.bmiHeader.biWidth, -bmpInfo.bmiHeader.biHeight, 		0, 0, 		0, -bmpInfo.bmiHeader.biHeight,		pData,		&bmpInfo,		DIB_RGB_COLORS );
CreateDIBSection only automatically allocates memory if you tell it to by passing NULL for the hSection parameter. Pass your own section object if you don't want one created for you.

If you don't want to use a section object all then use SetDIBitsToDevice like Hodgman suggested.
-Mike
and how do i create a "handle to file mapping object" ?
Quote:Original post by Dragon_Strike
and how do i create a "handle to file mapping object" ?


Quote:If hSection is not NULL, it must be a handle to a file-mapping object created by calling the CreateFileMapping function with the PAGE_READWRITE or PAGE_WRITECOPY flag. Read-only DIB sections are not supported. Handles created by other means will cause CreateDIBSection to fail.
well yea... but it says to a file...? i want it to write to memory
Quote:Original post by Dragon_Strike
well yea... but it says to a file...? i want it to write to memory
Quote:If hFile is INVALID_HANDLE_VALUE, the calling process must also specify a size for the file mapping object in the dwMaximumSizeHigh and dwMaximumSizeLow parameters. In this scenario, CreateFileMapping creates a file mapping object of a specified size that is backed by the system paging file instead of by a file in the file system.


I presume a DIB section uses a file mapping internally, so you have to use a file mapping object if you want to provide your own memory for the DIB section. However, a file mapping object doesn't have to be a file, it can be a block of memory itself (So long as that memory is allocated by the file mapping functions).
something liek this then...


unsigned char* pData = new unsigned char[fmtDesc.height*fmtDesc.width*4];
HANDLE data = CreateFileMapping(pData,
NULL,
PAGE_READWRITE,
fmtDesc.height*fmtDesc.width*4 /*????*/,
0,
NULL
);

what do i set for the

dwMaximumSizeHigh [in]
The high-order DWORD of the maximum size of the file mapping object.

??
Quote:Original post by Dragon_Strike
something liek this then...


unsigned char* pData = new unsigned char[fmtDesc.height*fmtDesc.width*4];
HANDLE data = CreateFileMapping(pData,
NULL,
PAGE_READWRITE,
fmtDesc.height*fmtDesc.width*4 /*????*/,
0,
NULL
);

what do i set for the

dwMaximumSizeHigh [in]
The high-order DWORD of the maximum size of the file mapping object.

??
dwMaximumSizeHigh can be 0 if you don't want a size of > 4GB. Otherwise it's the high 32-bits of a 64-bit size.
Also, you want CreateFileMapping to allocate the memory for you, you don't allocate it then hand it over to the OS. Your code should be something like:
LARGE_INTEGER liSize;liSize.QuadPart = fmtDesc.height * fmtDesc.width * 4;HANDLE hData = CreateFileMapping(NULL, NULL, PAGE_READWRITE,   liSize.HighPart, liSize.LowPart, NULL);unsigned char* pData = (unsigned char*)MapViewOfFile(hData,   FILE_MAP_ALL_ACCESS, 0, 0, 0);

hData is now a handle to a region of memory that's the required size that you can pass to CreateDIBSection, and pData is a pointer to that region of memory that you should be able to use to modify or read the bits.
Quote:Original post by Evil Steve
Quote:Original post by Dragon_Strike
something liek this then...


unsigned char* pData = new unsigned char[fmtDesc.height*fmtDesc.width*4];
HANDLE data = CreateFileMapping(pData,
NULL,
PAGE_READWRITE,
fmtDesc.height*fmtDesc.width*4 /*????*/,
0,
NULL
);

what do i set for the

dwMaximumSizeHigh [in]
The high-order DWORD of the maximum size of the file mapping object.

??
dwMaximumSizeHigh can be 0 if you don't want a size of > 4GB. Otherwise it's the high 32-bits of a 64-bit size.
Also, you want CreateFileMapping to allocate the memory for you, you don't allocate it then hand it over to the OS. Your code should be something like:
*** Source Snippet Removed ***
hData is now a handle to a region of memory that's the required size that you can pass to CreateDIBSection, and pData is a pointer to that region of memory that you should be able to use to modify or read the bits.


but my original problem was that i want to allocate the memory myself and not let the OS do it....

This topic is closed to new replies.

Advertisement