Jump to content

Dll Call like C++


Kip
 Share

Recommended Posts

Rewritten completely. I'm about to do some speed tests, it should be a lot faster. I'll keep you posted.

If you're gonna split it up into two functions, do it right :P

CODE

$New_Timer = TimerInit()

$Def_GetKeyState = _DllDefine("User32.dll", "int GetAsyncKeyState(int vKey)")

For $i = 0 to 99

_DllCall($Def_GetKeyState, 0x0D)

Next

$New_Diff = TimerDiff($New_Timer)

$Old_Timer = TimerInit()

For $i = 0 to 99

_DllCall_Old("User32.dll", "int GetAsyncKeyState(int vKey)", 0x0D)

Next

$Old_Diff = TimerDiff($Old_Timer)

MsgBox(0,"sdsd","New: "&$New_Diff&@CRLF&"Old: "&$Old_Diff)

Func _DllCall($aDefinition, $vParam1=0, $vParam2=0, $vParam3=0, $vParam4=0, $vParam5=0, $vParam6=0, $vParam7=0, $vParam8=0, $vParam9=0, $vParam10=0, $vParam11=0 _

, $vParam12=0, $vParam13=0, $vParam14=0, $vParam15=0, $vParam16=0, $vParam17=0, $vParam18=0, $vParam19=0, $vParam20=0, $vParam21=0, $vParam22=0, $vParam23=0, $vParam24=0 _

, $vParam25=0, $vParam26=0, $vParam27=0, $vParam28=0, $vParam29=0, $vParam30=0, $vParam31=0, $vParam32=0)

$iParams = UBound($aDefinition)-2

Local $sFinal

For $i = 1 to $iParams

$vValue = Execute("$vParam"&$i)

If IsString($vValue) Then

$vValue = StringReplace($vValue,'"','""')

$vValue = '"'&$vValue&'"'

EndIf

$sFinal &= ',"'&$aDefinition[$i][0]&'",'&$vValue

Next

$sFinal = "DllCall("&$aDefinition[$iParams+1][1]&',"'&$aDefinition[0][0]&'", "'&$aDefinition[0][1]&'"'&$sFinal&")"

Return Execute($sFinal)

EndFunc

Func _DllDefine($vDll, $sFormat)

If not $sFormat Then Return

$sFunction = StringLeft($sFormat, StringInStr($sFormat,"(")-1)

$aFunctionSplit = StringSplit($sFunction," ")

Local $sReturnType

Local $sFunctionName

For $i = 1 to $aFunctionSplit[0]

If $aFunctionSplit[$i] Then

If Not $sFunctionName Then

$sFunctionName = $aFunctionSplit[$i]

Else

$sReturnType = $sFunctionName

$sFunctionName = $aFunctionSplit[$i]

ExitLoop

EndIf

EndIf

Next

If Not $sFunctionName Then Return

If Not $sReturnType Then $sReturnType = "int"

$iStart = StringInStr($sFormat,"(")

$iEnd = StringInStr($sFormat,")")

$sParams = StringMid($sFormat,$iStart+1,$iEnd-$iStart-1)

$aParamsSplit = StringSplit($sParams,",")

$iParams = 0

If $sParams Then

If $aParamsSplit[0] > $iParams Then

$iParams = $aParamsSplit[0]

EndIf

EndIf

Local $aParameters[$iParams+2][2]

$aParameters[0][0] = $sReturnType

$aParameters[0][1] = $sFunctionName

For $i = 1 to $aParamsSplit[0]

$aParamSplit = StringSplit($aParamsSplit[$i]," ")

For $j = 1 to $aParamSplit[0]

If $aParamSplit[$j] Then

$aParameters[$i][0] = $aParamSplit[$j]

ExitLoop

EndIf

Next

Next

If $iParams > 32 Then Return

If IsString($vDll) Then

$vDll = StringReplace($vDll,'"','""')

$vDll = '"'&$vDll&'"'

EndIf

$aParameters[$iParams+1][1] = $vDll

Return $aParameters

EndFunc

; _DllCall by Kip

Func _DllCall_Old($vDll, $sFormat, $vParam1=0, $vParam2=0, $vParam3=0, $vParam4=0, $vParam5=0, $vParam6=0, $vParam7=0, $vParam8=0, $vParam9=0, $vParam10=0, $vParam11=0 _

, $vParam12=0, $vParam13=0, $vParam14=0, $vParam15=0, $vParam16=0, $vParam17=0, $vParam18=0, $vParam19=0, $vParam20=0, $vParam21=0, $vParam22=0, $vParam23=0, $vParam24=0 _

, $vParam25=0, $vParam26=0, $vParam27=0, $vParam28=0, $vParam29=0, $vParam30=0, $vParam31=0, $vParam32=0)

$iParams = @NumParams-2

If not $sFormat Then Return

$sFunction = StringLeft($sFormat, StringInStr($sFormat,"(")-1)

$aFunctionSplit = StringSplit($sFunction," ")

Local $sReturnType

Local $sFunctionName

For $i = 1 to $aFunctionSplit[0]

If $aFunctionSplit[$i] Then

If Not $sFunctionName Then

$sFunctionName = $aFunctionSplit[$i]

Else

$sReturnType = $sFunctionName

$sFunctionName = $aFunctionSplit[$i]

ExitLoop

EndIf

EndIf

Next

If Not $sFunctionName Then Return

If Not $sReturnType Then $sReturnType = "int"

$iStart = StringInStr($sFormat,"(")

$iEnd = StringInStr($sFormat,")")

$sParams = StringMid($sFormat,$iStart+1,$iEnd-$iStart-1)

$aParamsSplit = StringSplit($sParams,",")

If $sParams Then

If $aParamsSplit[0] > $iParams Then

$iParams = $aParamsSplit[0]

EndIf

EndIf

Local $aParameters[$iParams+1][2]

For $i = 1 to $aParamsSplit[0]

$aParamSplit = StringSplit($aParamsSplit[$i]," ")

For $j = 1 to $aParamSplit[0]

If $aParamSplit[$j] Then

$aParameters[$i][0] = $aParamSplit[$j]

ExitLoop

EndIf

Next

Next

If $iParams > 32 Then Return

Local $sFinal

For $i = 1 to $iParams

If Not $aParameters[$i][0] Then Return

$aParameters[$i][1] = Execute("$vParam"&$i)

If IsString($aParameters[$i][1]) Then

$aParameters[$i][1] = StringReplace($aParameters[$i][1],'"','""')

$aParameters[$i][1] = '"'&$aParameters[$i][1]&'"'

EndIf

$sFinal &= ',"'&$aParameters[$i][0]&'",'&$aParameters[$i][1]

Next

If IsString($vDll) Then

$vDll = StringReplace($vDll,'"','""')

$vDll = '"'&$vDll&'"'

EndIf

$sFinal = "DllCall("&$vDll&',"'&$sReturnType&'", "'&$sFunctionName&'"'&$sFinal&")"

Return Execute($sFinal)

EndFunc

Link to comment
Share on other sites

Manadar's/modified version is significantly faster.. but this method only seems logical if your going to call the dll more than once

$Def_GetKeyState = _DllDefine("User32.dll", "int GetAsyncKeyState(int vKey)")
For $y = 1 To 10
    $New_Timer = TimerInit()
    For $i = 0 To 99
        _DllCall($Def_GetKeyState, 0x0D)
    Next
    $New_Diff = TimerDiff($New_Timer)

    $Old_Timer = TimerInit()
    For $i = 0 To 99
        _DllCall_Old("User32.dll", "int GetAsyncKeyState(int vKey)", 0x0D)
    Next
    $Old_Diff = TimerDiff($Old_Timer)
    
    ConsoleWrite( "New: " & $New_Diff & ' | ' & "Old: " & $Old_Diff &@LF)
Next

#cs
New: 52.6374162079259 | Old: 65.7550051752388
New: 43.9080944645199 | Old: 110.223226695013
New: 35.5452997517841 | Old: 86.1860934839484
New: 35.6020108700966 | Old: 72.0865361379728
New: 37.2580872708682 | Old: 99.5092951757835
New: 33.624664587259 | Old: 81.0717563265722
New: 38.0431032435687 | Old: 89.8494082348455
New: 34.2551916514529 | Old: 80.260200667962
New: 34.3853757949684 | Old: 87.1334205883709
New: 35.6366521443368 | Old: 78.9885306652102
#ce
Edited by mrRevoked
Don't bother, It's inside your monitor!------GUISetOnEvent should behave more like HotKeySet()
Link to comment
Share on other sites

Manadar's/modified version is significantly faster.. but this method only seems logical if your going to call the dll more than once

Thanks for the test. I knew there was a significant speed-gain but couldn't really prove it. You're right about it being logical only if you are going to call it more than once, but it can be applied if you're just going to call it once and need the speed at that particular moment and not on initialization.

Edit: Modified my version to actually return the value of the DllCall. Still untested on a lot of things and I simply didn't spend as much time on it as Kip has. Error checking is bad in my version and the format you have to use to define the Dll is very precise.

Edited by Manadar
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...