CreateDIBSection issue #1331
Replies: 3 comments
-
Is there some example code for getting this to work? I'm just never going to work this out. I've asked copilot several times, it keeps suggesting using IntPtr, but that doesn't work. I've looked up how to turn IntPtr into a void pointer pointer. Like this...
But then, cannot convert from void** to void*. So I drop one of the stars...
Then I get 'cannot implicitly convert type DeleteObjectSafeHandle to nint. I don't even know what the CreateDIBSection is asking of me. I'm just going around in wobbly circles. I feel like asking for a void** is the problem here. There is no such thing as a void** AFAIC, it should be byte[] shouldn't it? |
Beta Was this translation helpful? Give feedback.
-
Try using 3 backticks followed by Here's an example, but using apostrophes instead of backticks so that github won't hide the syntax I'm trying to show you. '''cs I've also edited your original post to fix the first snippet, so you can "edit" it again to see how it actually looks. |
Beta Was this translation helpful? Give feedback.
-
There are two angles to your question, with different sources for answers.
At a high level, functions are declared once or twice (as overloads) by CsWin32. The first is typically an When multiple overloads exist, you should have a clear idea in your head of which overload you are trying to call. Otherwise you'll be frustrated by the compiler giving you misleading or unhelpful error messages about what you are passing in and what it is expecting. In this case, CsWin32 appears to be generating these two overloads: winmdroot.Graphics.Gdi.HBITMAP CreateDIBSection(winmdroot.Graphics.Gdi.HDC hdc, winmdroot.Graphics.Gdi.BITMAPINFO* pbmi, winmdroot.Graphics.Gdi.DIB_USAGE usage, void** ppvBits, winmdroot.Foundation.HANDLE hSection, uint offset)
DeleteObjectSafeHandle CreateDIBSection(winmdroot.Graphics.Gdi.HDC hdc, winmdroot.Graphics.Gdi.BITMAPINFO* pbmi, winmdroot.Graphics.Gdi.DIB_USAGE usage, out void* ppvBits, SafeHandle hSection, uint offset) Notice how the first uses structs that you would see in native C code, and the latter uses using System.Runtime.InteropServices;
using Windows.Win32;
using Windows.Win32.Graphics.Gdi;
unsafe {
HDC dc = default;
BITMAPINFO bmInfo = default;
void* bits;
SafeHandle? hSection = null;
DeleteObjectSafeHandle hBitmap = PInvoke.CreateDIBSection(
dc,
&bmInfo,
DIB_USAGE.DIB_RGB_COLORS,
out bits,
hSection,
0);
} In your question you appear to be getting hung up on the using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.Graphics.Gdi;
unsafe {
HDC dc = default;
BITMAPINFO bmInfo = default;
void* bits;
HANDLE hSection = default;
HBITMAP hBitmap = PInvoke.CreateDIBSection(
dc,
&bmInfo,
DIB_USAGE.DIB_RGB_COLORS,
&bits,
hSection,
0);
} When calling the overload that takes struct handles, the onus is on your program to be sure to avoid native memory leaks by always closing handles, even in exceptional cases. This is something that using Of course, both of these samples are strictly compilable but would probably misbehave because I'm initializing the inputs only to their default values. To know what values to pass in would be the other category of question, which is better directed at Win32 forums. Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
I've tried
0
null
HANDLE.Null
IntPtr.Zero
Update. Sry, my formatting is not being preserved using 'code'. How do I pass in a null SafeHandle? I think the normal argument there is a zero.
An alternate question is ...
Another way of trying it...
How can i make this work?
Beta Was this translation helpful? Give feedback.
All reactions