Problem using DIB handle with C++ DLL

  • Thread starter Thread starter Geoff Tanaka
  • Start date Start date
G

Geoff Tanaka

Hi, I have created a DIB in C# and I am having a problem using it in a
C++ DLL.

Basically I have a C++ DLL which requires a DIB handle to be passed
into it. I create the DIB using:
IntPtr myDIB = CreateDIBSection(hdc, pBitmapInfo, DIB_RGB_COLORS,
&pPixelSource, (IntPtr)0, (uint);

I can put the myDIB into the function and it will compile, but when the
code runs it returns a NullReferenceException. I have tried to get some
help with this matter and the only advice that I have gotten was that I
may not be getting the proper handle back from CreateDIBSection. Some
suggestions were to dereference the myDIB when passing it into the dll
function. I tried using myDIB.ToPointer() (though I'm not sure I was
using it properly) and even just going &myDIB. Neither of these have
worked. Does anyone know what I need to do to pass in the correct
handle?
 
Geoff,

If you are getting NullReferenceException then it might be that the
IntPtr that is returned is null. Of course, it could mean a number of other
things as well, since this is just one line of code.

Are you actually getting a value back in myDIB? If it is zero, then you
have an error and should get the windows error through a call to the static
GetLastWin32Error method on the Marshal class.

Hope this helps.
 
Thanks for the reply! Yes, I am getting a value back in myDIB. Would it
help if I posted the function that creates the DIB?

static public IntPtr CreateDIB(IntPtr hwnd, short bpp, int width, int
height)
{
// Get the DeviceContext
IntPtr hdc = GetDC(hwnd);

// Create the bitmap file and info headers
BITMAPINFOHEADER infoHdr = new BITMAPINFOHEADER(bpp, width, height);
BITMAPFILEHEADER fileHdr = new BITMAPFILEHEADER(infoHdr);

byte[] dummyBitmapInfo = new byte[52];
MemoryStream msDummy = new MemoryStream(dummyBitmapInfo);
BinaryWriter bwDummy = new BinaryWriter(msDummy);
infoHdr.Store(bwDummy, true);

unsafe
{
// pBitmapInfo points to the locked location of the bitmap info
structure
// pPixelDest points to the pixel data destination
fixed (byte* pBitmapInfo = &dummyBitmapInfo[0])//, pPixelDest =
&bytes[fileHdr.bfOffBits])
{
// Pointer to pixel data source
byte* pPixelSource;

// Create a DIBSection using the dummyBitmapInfo structure. This
acquires a
// pointer (pPixelSource) to the actual bit data for the image
IntPtr hDibSect = CreateDIBSection(hdc, pBitmapInfo,
DIB_RGB_COLORS, &pPixelSource, (IntPtr)0, (uint)0);
#if DEBUG
// if something failed creating the DIBSection, put up a
MessageBox with
// the value from GetLastError().
if (hDibSect == (IntPtr)0)
{
MessageBox.Show(Windows.GetLastError().ToString());
DeleteObject(hDibSect);
return (IntPtr)0;
}
#endif
// Select the DIBSection into the memory DC
//IntPtr hbmOld = SelectObject(hdcComp, hDibSect);

return hDibSect;
}
}

}

So then in my main function I call:
IntPtr myDIBHandle = CreateDIB(hwnd, 24, imagePanel.Width,
imagePanel.Height);

MyCPlusFunction(myDIBHandle);


Most of that function to create the DIB I found on the Internet and
modified, so I am assuming that it is correct.

Thanks again, hopefully you can make some sense of why this is
happening.
-Geoff
 
Well, this issue has been resolved now. What happened was that we had
to get the guy who made the DLL to change it to accept a Bitmap object
instead of a DIB. Just doesn't seem to be a way to get it to work from
C# to C++...
 
Back
Top