Jump to content
Sign in to follow this  

Converting HBITMAP to const unsigned char*

Recommended Posts

I'm trying to use a function from the leptonica dll. It seems like it should be straightforward, but I'm having no luck with it.

From the documentation:

typedef unsigned char l_uint8

PIX* pixReadMemBmp(const l_uint8 *cdata, size_t size)

Then, in AutoIT, I have:

$leptonicaHandle = DllOpen("path\to\liblept168.dll");

$image = _ScreenCapture_Capture("", $x1, $y1, $x2, $y2);

$tmp = _GDIPlus_BitmapCreateFromHBITMAP($image);
$width = _GDIPlus_ImageGetWidth($tmp);
$height = _GDIPlus_ImageGetHeight($tmp);

$pix = DllCall($leptonicaHandle, "ptr:cdecl", "pixReadMemBmp", "str", $image, "ULONG_PTR", (($width * $height * 24) + 54));
; @error == 0
; $pix[0] == 0x000000

There are no errors when this executes. The call to pixReadMemBmp just returns a bad memory address every time. I read that HBITMAP can be type-cast into uchar * without issue, but I think this may be where my code is off. I have "str" above, but I've tried "str*", "ptr" and "handle" as well.

Also, the last parameter to DllCall is supposed to be the size of the bitmap in bytes (width * height * 24bits/pixel) + 54bytesforheader. I'm not 100% sure this is correct, but I couldn't find any AutoIT functions to get image size from an HBITMAP.

I'd appreciate any thoughts on this. Thanks...

Share this post

Link to post
Share on other sites

Actually the documentation says this also.

Read/write to memory [only on linux]

PIX *pixReadMemBmp()

l_int32 pixWriteMemBmp()

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...