Jump to content

Recommended Posts

Posted (edited)

My code is the following:

#include <Array.au3>
Global $dllhandle=DllOpen("kernel32.dll")
Func DebugDllCall($cstring)
$parameter_arr=StringSplit($cstring,";",1)
$parameter_arr[0]="CallArgArray"
_ArrayDisplay($parameter_arr)
Local $ret=Call("DllCall",$parameter_arr)
if @error then 
Local $errorcode=@error
ConsoleWrite(DllCallErrorTranslate($errorcode))
Return SetError(1,$errorcode,0) ;DllCall error
endif
if $ret[0]=0 then
ConsoleWrite(_WinAPI_GetLastErrorMessage())
Return SetError(2,-1,0)
endif
Return $ret
EndFunc
Func DllCallErrorTranslate($errorcode)
Select
Case $errorcode=1
Return "Unable to use the DLL file"
Case $errorcode=2
Return "Unknown return type"
Case $errorcode=3
Return "Function not found in DLL file"
Case $errorcode=4
Return "Bad number of parameters"
EndSelect
EndFunc
Func API_GlobalAlloc($nrbytes,$movable=1,$zinit=1)
Local $flags=0
if $zinit then $flags=BitOR($flags,0x0040)
if $movable then $flags=BitOR($flags,0x0002)
$ret=DebugDllCall($dllhandle&";ptr;GlobalAlloc;uint;"&$flags&";dword;"&$nrbytes)
;MsgBox(0,"",$ret)
Return $ret[0]
EndFunc
Func API_GlobalSize($ptr)
$ret=DebugDllCall($dllhandle&";dword;GlobalSize;ptr;"&$ptr)
Return $ret[0]
EndFunc
$h=API_GlobalAlloc(1024)
ConsoleWrite(API_GlobalSize($h))

I'm running this on Vista, latest official AutoIt version. The problem occurs when API_GlobalAlloc calls DebugDllCall, which takes the ";"-delimited string and makes a parameter array for the call function from it. It works when I just DllCall and supply all of the parameters. Otherwise I get "Bad Number Of Parameters". _ArrayDisplay within DebugDllCall looks good. What am I doing wrong?

EDIT: Error in my code: "Case $errorcode" should have been "Case $errorcode=4", which is the reason I was getting "Bad number of parameters".

EDIT2: By adding

MsgBox(0,"",@extended)

after $ret=DebugDllCall($dllhandle&";ptr;GlobalAlloc;uint;"&$flags&";dword;"&$nrbytes)

I get @extended=57005 (which is the @error returned by DllCall), and the documentation only provides an explanation for errors 1,2,3 and 4.What the heck does that mean?

EDIT3: Apparently, I'm getting an errorcode from the call function, @error=0xDEAD=57005, saying that either "DllCall" isn't a valid function name, or I'm calling it with the wrong number of parameters. Still, from the _ArrayDisplay, that doesn't seem to be the case.

EDIT4: Conclusion: I'm just an idiot that didn't rtfm. "Call" can't call "DllCall" because it can't call built-in AutoIt functions as mentioned in the Call Function Reference: "The function cannot be a built-in AutoIt function or plug-in function.". I was about to file a bug report. Meh..

Edited by VicTT
Quote

Together we might liveDivided we must fall

 

Posted

Just in case anyone cares, or needs the working code: This works after refactoring:

#include <Array.au3>
Global $dllhandle=DllOpen("kernel32.dll")
Func DebugDllCall($ret,$error,$extended)
if $error then 
ConsoleWrite("DllCall error: "&DllCallErrorTranslate($error))
Return SetError(1,$error,0) ;DllCall error
endif
if $ret[0]=0 then
ConsoleWrite("API error: "&_WinAPI_GetLastErrorMessage())
Return SetError(2,-1,0)
endif
Return $ret
EndFunc
Func DllCallErrorTranslate($errorcode)
Select
Case $errorcode=1
Return "Unable to use the DLL file"
Case $errorcode=2
Return "Unknown return type"
Case $errorcode=3
Return "Function not found in DLL file"
Case $errorcode=4
Return "Bad number of parameters"
Case Else
Return $errorcode
EndSelect
EndFunc
Func API_GlobalAlloc($nrbytes,$movable=1,$zinit=1)
Local $flags=0
if $zinit then $flags=BitOR($flags,0x0040)
if $movable then $flags=BitOR($flags,0x0002)
$ret=DllCall($dllhandle,"ptr","GlobalAlloc","uint",$flags,"dword",$nrbytes)
DebugDllCall($ret,@error,@extended)
Return $ret[0]
EndFunc
Func API_GlobalSize($ptr)
$ret=DllCall($dllhandle,"dword","GlobalSize","ptr",$ptr)
DebugDllCall($ret,@error,@extended)
Return $ret[0]
EndFunc
$h=API_GlobalAlloc(1028)
ConsoleWrite(API_GlobalSize($h))
Quote

Together we might liveDivided we must fall

 

  • 8 months later...
  • Moderators
Posted

veryBeginner,

Read the "Call" page in the Help file - it explains why you get @error = 0xDEAD (= 57005) and @extended = 0xBEEF (= 48879) in certain cases. :oops:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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
  • Recently Browsing   0 members

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