Jump to content

DLL call with a char* and binary data


Recommended Posts

I'm having a hard time with calling a DLL. The examples I found only cover strings in that matter.

The DLL expects a char* as a parameter and delievers its result back into that area.

int MyFunc (char *chBuf, int Size);

This could be the correct invocation, but if chBuf contains binary zeros the string is trimmed:

$RetVal = "AAAAAAAAAAAAAAAAAAAAAAAAAAA"

$Read = DllCall ($DLLHandle, "int:stdcall", "MyFunc", "str", $RetVal, "int", 10)

MsgBox (0, "Test", "RetVal: " & $Read [1])

shows, for example: 12345

Nothing wrong, so far, but in reality I get back from the DLL: "12345" '\0' '\0' "678"

The binary zeros don't appear in the AutoIt string variable.

If I use Binary ($Read [1]) the result stays the same except that I get its hexadecimal representation back, but the buffer is still trimmed.

I tried to make it binary, like,

$Read = DllCall ($DLLHandle, "int:stdcall", "MyFunc", "ptr", $RetVal, "int", 10)

but I don't know how to provide $RetVal's address instead of its value (I always receive the value 0 back, which is of course correct because I'm not referencing the pointer).

In C/C++ I'd use the ampersand character (&RetVal). Is there a similar way in AutoIt script?

Link to comment
Share on other sites

What about:

$RetVal = DllStructCreate("char[128]")
$Read = DllCall($DLLHandle, "int:stdcall", "MyFunc", "ptr", DllStructGetPtr($RetVal), "int", 10)
MsgBox(0, "Test", "RetVal: " & DllStructGetData($RetVal, 1))

WBD

Edited by WideBoyDixon
Link to comment
Share on other sites

if the functions returns binary data with zeros, you should use byte instead of char :D

$RetValSize = 128
$RetVal = DllStructCreate("byte[" & $RetValSize & "]")
; set string to binary struct:
DLLStructSetData($RetVal,StringToBinary("A test string"))
$Read = DllCall($DLLHandle, "int:stdcall", "MyFunc", "ptr", DllStructGetPtr($RetVal), "int", $RetValSize)
MsgBox(0, "Test", "RetVal: " & DllStructGetData($RetVal, 1))
Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Oh I see. I thought the nulls were discardable; not part of the actual required result! :D

Link to comment
Share on other sites

if the functions returns binary data with zeros, you should use byte instead of char :D

$RetValSize = 128
$RetVal = DllStructCreate("byte[" & $RetValSize & "]")
; set string to binary struct:
DLLStructSetData($RetVal,StringToBinary("A test string"))
$Read = DllCall($DLLHandle, "int:stdcall", "MyFunc", "ptr", DllStructGetPtr($RetVal), "int", $RetValSize)
MsgBox(0, "Test", "RetVal: " & DllStructGetData($RetVal, 1))
Thanks a lot, both of you! That solved it. I didn't expect that it may be so complicated, I only kept looking for a simple solution. ;-)
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...