471,325 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Problem using DIB handle with C++ DLL

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?

Jan 18 '06 #1
3 4834
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Geoff Tanaka" <Ge**********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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?

Jan 18 '06 #2
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

Jan 18 '06 #3
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++...

Jan 20 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by matt dittman | last post: by
1 post views Thread by Tony Johansson | last post: by
3 posts views Thread by Tony Johansson | last post: by
reply views Thread by Tony Johansson | last post: by
15 posts views Thread by James | last post: by
23 posts views Thread by VB Programmer | last post: by
8 posts views Thread by Chizl | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.