Sign in to follow this  
Followers 0
delray

Translate AHK to AU3

23 posts in this topic

These 2 pieces of code are AHK; what is the AU3 equivalent is:

DetectHiddenWindows, On
NumPut( WinExist( A_ScriptFullPath " ahk_class AutoHotkey ahk_pid " PID),NID,4 )
DetectHiddenWindows, Off

The main problem with the following is the ":" between operations:

IC := RC=0&&WC=0 ? 1 : RC!=0&&WC=0 ? 2 : RC=0&&WC!=0 ? 3 : RC!=0&&WC!=0 ? 4 : NumPut( hIcon%IC%,NID,20 )

Share this post


Link to post
Share on other sites



When posting code from another language, it might be best to include descriptions of what every command does. Unless someone is coding in both languages, we'd have no idea what those commands do or their format if we're able to guess what the commands do but not what the parameters are.

You're basically asking a small minority of people to help you because AHK and AutoIt3 don't use the same syntax and few common commands, unless you expect someone to learn what those commands do and how the parameters are supposed to work to help you.


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

Sorry I asked.

Share this post


Link to post
Share on other sites

Sorry I asked.

I wasn't insulting you, I was asking you to supply more information so that we would be able to help you. As it stands, as I said above, there's a small minority of forum users that would use both languages so you're not going to get many able to help without you helping yourself by supplying that information.

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

The code is part of a larger program all written in AHK which I am trying to translate myself so I can understand it. I look for answers in AHK and AU3 help files and didn't find an answer. I have finally translated the second part:

If $RC=0 And $WC=0 Then
$IC=1
ElseIf $RC<>0 And $WC=0 Then
$IC=2
ElseIf $RC=0 And $WC<>0 Then
$IC=3
ElseIf $RC<>0 and $WC<>0 Then
$IC=4
EndIf

Here is the AHK explanation of the second part:

DetectHiddenWindows, On = Determines whether invisible windows are "seen" by the script.
NumPut( WinExist( A_ScriptFullPath " ahk_class AutoHotkey ahk_pid " PID),NID,4 )
NumPut = Stores number in an array

A_ScriptFullPath = The combination of the above two variables to give the complete file specification of the script, e.g. C:\My Documents\My Script.ahk

ahk_class AutoHotkey = All windowing commands can operate upon a class of windows by the class text shown by Window Spy or retrieved by WinGetClass. In the following example, a split-view Explorer window would be activated: WinActivate ahk_class ExploreWClass

ahk_pid = All windowing commands can operate upon the window(s) belonging a process identifier (PID). For example, WinClose ahk_pid %VarContainingPID% would close the topmost window of that process. A PID can be retrieved via WinGet or Run or Process.

DetectHiddenWindows, Off = Determines whether visible windows are "seen" by the script.

Hopefully this is a little help.

Share this post


Link to post
Share on other sites

Here is the code I have come up with. The idea is to set an icon into the tray depending on its current status idle/read/write/read+write. The only problem is that it fails. Any and all help appreciated.

#include <Process.au3>
;#Include <Timers.au3>
Global $NID[444], $Ret, $nReturn
Global $ORC=0, $OWC=0, $DP[88]
main()
Func main()
ProcessSetPriority("HHD_Monitor.au3", 4)
$PID = DllCall("kernel32.dll", "handle", "GetCurrentProcessId")
; Run, diskperf.exe -y   ; Execute it once to enable Disk Performance monitoring.
; DISK_PERFORMANCE
$hDrv = DllCall("kernel32.dll", "ptr", "CreateFile", "Str", "\\.\PhysicalDrive0", "uint", 0, "uint", 3, "uint", 0, "uint", 3, "uint", 0, "uint", 0)
While 1
;_Timer_SetTimer(HDD_Monitor)
;HDD_Monitor:
  $Ret =   DllCall("kernel32.dll", "int", "DeviceIoControl", "uint", 0x70020, "uint", DllStructGetPtr($DP), "uint", 88, "uint_ptr", $nReturn, "uint", 0)
  $NRC = $DP[40] ;NRC:=NumGet(DP,40),
  $NWC = $DP[44] ;NWC:=NumGet(DP,44),
  $RC = $NRC - $ORC ;RC:=NRC-ORC,
  $WC = $NWC - $OWC ;WC:=NWC-OWC,
  $ORC = $NRC ;
  $ORC = $NRC ;ORC:=NRC,
  $OWC = $NWC ;OWC:=NWC
  If $RC=0 And $WC=0 Then
  ; $IC=1
   TraySetIcon("0.icon")
  ElseIf $RC<>0 And $WC=0 Then
  ; $IC=2
   TraySetIcon("1.icon")
  ElseIf $RC=0 And $WC<>0 Then
  ; $IC=3
   TraySetIcon("2.icon")
  ElseIf $RC<>0 and $WC<>0 Then
  ; $IC=4
   TraySetIcon("3.icon")
  EndIf
WEnd
EndFunc

Share this post


Link to post
Share on other sites

The main problem with the following is the ":" between operations:

IC := RC=0&&WC=0 ? 1 : RC!=0&&WC=0 ? 2 : RC=0&&WC!=0 ? 3 : RC!=0&&WC!=0 ? 4 : NumPut( hIcon%IC%,NID,20 )

What does ":", ":=", "&&" mean in AHK language?

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

&& is the logical-AND operator

:= is the Assign operator

according to this docu site.

":" seems to be like "elseif" in AutoIt.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

What does ":", ":=", "&&" mean in AHK language?

If $RC=0 And $WC=0 Then
  ; $IC=_
   TraySetIcon("Drive.ico")
  ElseIf $RC<>0 And $WC=0 Then
  ; $IC=2
   TraySetIcon("Read.ico")
  ElseIf $RC=0 And $WC<>0 Then
  ; $IC=3
   TraySetIcon("Write.ico")
  ElseIf $RC<>0 and $WC<>0 Then
  ; $IC=4
   TraySetIcon("Read_Write.ico")
  EndIf

Found part of the problem, but need an answer:

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Heres just a few things that stood out to me after really looking at it. First you dont have to use a dllcall to get the PID. Just use the @AutoItPID

Next is the other dllcall posted below. I belive its wrong for a few reasons:

  • DllstructGetPtr() returns a ptr to a Dll Structure created with DllStructCreate()
  • Going by the what I read on the msdn I think your missing some parameters.
  • $nReturn variable cant be used the way your trying. I'm pretty sure that would need to be another dll structure.
Global $NID[444], $Ret, $nReturn
Global $ORC = 0, $OWC = 0, $DP[88]
$Ret = DllCall("kernel32.dll", "int", "DeviceIoControl", _
                                                  "uint", 0x70020, _ ;             hDevice [in]
                                                  "uint", DllStructGetPtr($DP), _; dwIoControlCode [in]
                                                  "uint", 88, _;                  lpInBuffer [in, optional]
                                                  "uint_ptr", $nReturn, _;      nInBufferSize [in]
                                                  "uint", 0);                        lpOutBuffer [out, optional]
                                                  ;??????                          nOutBufferSize [in]
                                                  ;??????                          lpBytesReturned [out, optional]
                                                  ;??????                          lpOverlapped [in, out, optional]
Edited by Beege

Share this post


Link to post
Share on other sites

Heres just a few things that stood out to me after really looking at it. First you dont have to use a dllcall to get the PID. Just use the @AutoItPID

Next is the other dllcall posted below. I belive its wrong for a few reasons:

  • DllstructGetPtr() returns a ptr to a Dll Structure created with DllStructCreate()
  • Going by the what I read on the msdn I think your missing some parameters.
  • $nReturn variable cant be used the way your trying. I'm pretty sure that would need to be another dll structure.
Global $NID[444], $Ret, $nReturn
Global $ORC = 0, $OWC = 0, $DP[88]
$Ret = DllCall("kernel32.dll", "int", "DeviceIoControl", _
                                                  "uint", 0x70020, _ ;        hDevice [in]
                                                  "uint", DllStructGetPtr($DP), _; dwIoControlCode [in]
                                                  "uint", 88, _;                    lpInBuffer [in, optional]
                                                  "uint_ptr", $nReturn, _;      nInBufferSize [in]
                                                  "uint", 0);                        lpOutBuffer [out, optional]
                                                  ;??????                          nOutBufferSize [in]
                                                  ;??????                          lpBytesReturned [out, optional]
                                                  ;??????                          lpOverlapped [in, out, optional]

This is what I currently have:
#include <WinAPI.au3>
#include <WinAPIEx.au3>

Local $tDGEX[444]
Local $hDrv = _WinAPI_CreateFile( "\\.\PhysicalDrive0", 2, 3, 0, 3, 0)

MsgBox(0, 0, $hDrv) ; Returns handle

Local $ret = DllCall('kernel32.dll', 'int', 'DeviceIoControl', _
           'ptr', $hDrv, _
           'dword', 0x70020, _
           'ptr', 0, _
           'dword', 0, _
           'ptr', ptr($tDGEX), _
           'dword', 88, _
           'dword*', 0, _
           'ptr', 0)

MsgBox(0, 0, $ret) ; Returns null

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

DllCall() alway returns an array unless an error happened. Whats the outcome if you change your msgbox to this:

If @error Then
     MsgBox(0,0,'error = ' & @error)
Else
     MsgBox(0, 0, $ret[0])
EndIf

Edit: Also if $ret[0] = 0 you can further debug the problem by calling _WinAPI_GetLastError()

If @error Then
     MsgBox(0,0,'error = ' & @error)
Else
     If Not $ret[0] then
          MsgBox(0,'WinApi Error', _WinAPI_GetLastError())
     Else
          MsgBox(0, 0, $ret[0])
     EndIf
EndIf
Edited by Beege

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

Edit: Also if $ret[0] = 0 you can further debug the problem by calling _WinAPI_GetLastError()

If @error Then
     MsgBox(0,0,'error = ' & @error)
Else
     If Not $ret[0] then
          MsgBox(0,'WinApi Error', _WinAPI_GetLastError())
     Else
          MsgBox(0, 0, $ret[0])
     EndIf
EndIf

$ret[0] = 0

_WinAPI_GetLastError()) = 122 System buffer too small... What ever that means.

Edited by delray

Share this post


Link to post
Share on other sites

Sorry I should have put _WinAPI_GetLastErrorMessage() and not _WinAPI_GetLastError().

And whats the value your getting from MsgBox(0, 0, $hDrv) ; Returns handle ??

I just ran this and mine did not return a handle. It returned 0, which is a failure..

Share this post


Link to post
Share on other sites

Sorry I should have put _WinAPI_GetLastErrorMessage() and not _WinAPI_GetLastError().

And whats the value your getting from MsgBox(0, 0, $hDrv) ; Returns handle ??

I just ran this and mine did not return a handle. It returned 0, which is a failure..

WinApi Error: The data area passed to a system call is too small.

$hDrv = 0x00000170

Don't be sorry, I am grateful.

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

Ok make these changes:

Local $tDGEX = DllStructCreate("byte[444]"); not sure what the real size should be..

And in the DllCall().

Local $ret = DllCall('kernel32.dll', 'int', 'DeviceIoControl', _
           'ptr', $hDrv, _
           'dword', 0x70020, _
           'ptr', 0, _
           'dword', 0, _
           'ptr', DllStructGetPtr($tDGEX), _; Ptr to dllstructure.
           'dword', 88, _
           'dword*', 0, _
           'ptr', 0)

Also note that ptr() function is to more or less change a integer value into a ptr value. Not to get a ptr to a variable.

Edited by Beege

Share this post


Link to post
Share on other sites

Ok make these changes:

Local $tDGEX = DllStructCreate("byte[444]"); not sure what the real size should be..

And in the DllCall().

Local $ret = DllCall('kernel32.dll', 'int', 'DeviceIoControl', _
           'ptr', $hDrv, _
           'dword', 0x70020, _
           'ptr', 0, _
           'dword', 0, _
           'ptr', DllStructGetPtr($tDGEX), _; Ptr to dllstructure.
           'dword', 88, _
           'dword*', 0, _
           'ptr', 0)

Also note that ptr() function is to more or less change a integer value into a ptr value. Not to get a ptr to a variable.

That made the difference. No error and $ret[0] = 1

Now I will try and move to the next step which will take a few minutes. Thanks again Mass Spammer...

Share this post


Link to post
Share on other sites

Here is the entire code and I am not getting any data in $NRC and $NWC:

#include <Process.au3>
#include <array.au3>
#include <WinAPIEx.au3>

Global $NID[444], $Ret, $nReturn, $hDrv
Global $ORC=0, $OWC=0
main()
Func main()
Global $DP = DllStructCreate("byte[88]")
Global $hDrv = _WinAPI_CreateFile( "\\.\PhysicalDrive0", 2, 3, 0, 3, 0)
While 1
Local $ret = DllCall('kernel32.dll',  'int', 'DeviceIoControl', _
           'ptr', $hDrv, _
           'dword', 0x70020, _
           'ptr', 0, _
           'dword', 0, _
           'ptr', DllStructGetPtr($DP), _
           'dword', 88, _
           'dword*', 0, _
           'ptr', 0)

;ToolTip($hDrv & " : " & $ret[0])

  $NRC = DllStructGetData ($DP, 40) ;NRC:=NumGet(DP,40),
  $NWC = DllStructGetData ($DP, 44) ;NWC:=NumGet(DP,44),
  $RC = $NRC - $ORC ;RC:=NRC-ORC,
  $WC = $NWC - $OWC ;WC:=NWC-OWC,
  $ORC = $NRC ;
  $ORC = $NRC ;ORC:=NRC,
  $OWC = $NWC ;OWC:=NWC

ToolTip($NRC & " : " & $NWC)   ; 0 : 0 at this point
  Sleep(500)

If $RC=0 And $WC=0 Then
  ; $IC=_
   TraySetIcon("Drive.ico")
  ElseIf $RC<>0 And $WC=0 Then
  ; $IC=2
   TraySetIcon("Read.ico")
  ElseIf $RC=0 And $WC<>0 Then
  ; $IC=3
   TraySetIcon("Write.ico")
  ElseIf $RC<>0 and $WC<>0 Then
  ; $IC=4
   TraySetIcon("Read_Write.ico")
  EndIf
WEnd
EndFunc

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

Im assuming your trying to get elements 40 and 44 from the array? You only have 1 element in the dll structure and that element is byte[88]. Since the element is an array itself, you need to use the 3rd parameter to access the elements of that array. Try:

$NRC = DllStructGetData ($DP, 1, 40) ;NRC:=NumGet(DP,40)
$NWC = DllStructGetData ($DP, 1, 44) ;NWC:=NumGet(DP,44),

edit: million typos

Edited by Beege

Share this post


Link to post
Share on other sites

I sent the output to num and caps lock and they are fluctuating as they should be, but the icons are not activating other than the first one (Drive.ico).

#include <Process.au3>
#include <array.au3>
#include <WinAPIEx.au3>
Global $NID[444], $Ret, $nReturn, $hDrv
Global $ORC=0, $OWC=0
Global $DP = DllStructCreate("byte[88]")
Global $hDrv = _WinAPI_CreateFile( "\\.\PhysicalDrive0", 2, 3, 0, 3, 0)
While 1
Local $ret = DllCall('kernel32.dll',  'int', 'DeviceIoControl', _
           'ptr', $hDrv, _
           'dword', 0x70020, _
           'ptr', 0, _
           'dword', 0, _
           'ptr', DllStructGetPtr($DP), _
           'dword', 88, _
           'dword*', 0, _
           'ptr', 0)
;ToolTip($hDrv & " : " & $ret[0])
$NRC = DllStructGetData ($DP, 1, 40) ;NRC:=NumGet(DP,40),
$NWC = DllStructGetData ($DP, 1, 44) ;NWC:=NumGet(DP,44),
$RC = $NRC - $ORC ;RC:=NRC-ORC,
$WC = $NWC - $OWC ;WC:=NWC-OWC,
$ORC = $NRC ;
$OWC = $NWC ;OWC:=NWC
ToolTip($RC & " : " & $WC)

If $RC=0 And $WC=0 Then
Send ("{NUMLOCK}")
; $IC=_
  TraySetIcon("Drive.ico")
ElseIf $RC<>0 And $WC=0 Then
Send ("{CAPSLOCK}")
; $IC=2
  TraySetIcon("Read.ico")
ElseIf $RC=0 And $WC<>0 Then
Send ("{CAPSLOCK}")
; $IC=3
  TraySetIcon("Write.ico")
ElseIf $RC<>0 and $WC<>0 Then
Send ("{CAPSLOCK}")
; $IC=4
  TraySetIcon("Read_Write.ico")
EndIf
; Sleep(1000)
WEnd

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  
Followers 0