473,395 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 4939
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: matt dittman | last post by:
I have created a windows service that reads emails from a drop directory and moves them to the appropriate mail folder every 15 seconds. I can move, rename and delete the files as needed, up...
1
by: Tony Johansson | last post by:
Hello! I have some problem with template. I have two classes called Handle which is a template class and Integer which is not a template class. The Integer class is just a wrapper class for a...
3
by: Tony Johansson | last post by:
Hello! I have some problem with STL. I have two classes called Handle which is a template class and Integer which is not a template class. The Integer class is just a wrapper class for a...
0
by: Tony Johansson | last post by:
Hello! I have two classes called Handle which is a template class and a class Integer which is not a template class. The Integer class is just a wrapper class for a primitive int with some...
15
by: James | last post by:
In my code I have a problem in abtaining a windows handle. I do not know the namespace to obtain the windows hanle. here is the code. using System; using System.Collections; using...
9
by: Rajat Tandon | last post by:
Hello there, I am relatively new to the newsgroups and C#. I have never been disappointed with the groups and always got the prompt replies to my queries.This is yet another strange issue, I am...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
0
by: Fiona McBride | last post by:
Hi all, I have a really odd problem with some Visual Basic .NET 2003 code; I have a program that creates a number of windows which contain RichTextBox, Timers (disabled) and menus. The code...
8
by: Chizl | last post by:
I'm building a web server and having some issues with the TCPListener.Start(BackLog). It doesn't seem to do as expected. I'm using MS Web Stress Tool to test against my web server and when I...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.