Jump to content
Sign in to follow this  
Morteza

Translate VisualBasic to AutoIT

Recommended Posts

Morteza

Dears, the following code work without any problem in VB:

' #VBIDEUtils#**********************************************

' * Programmer Name : Waty Thierry

' * Web Site : www.geocities.com/ResearchTriangle/6311/

' * E-Mail : waty.thierry@usa.net

' * Date : 22/10/98

' * Time : 15:50

' * Module Name : Start_Module

' * Module Filename : Start.bas

' *********************************************************

' * Comments : Show/Hide the start button

' *

' ********************************************************

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Public Function hideStartButton()

'This Function Hides the Start Button'

OurParent& = FindWindow("Shell_TrayWnd", "")

OurHandle& = FindWindowEx(OurParent&, 0, "Button", vbNullString)

ShowWindow OurHandle&, 0

End Function

Public Function showStartButton()

'This Function Shows the Start Button'

OurParent& = FindWindow("Shell_TrayWnd", "")

OurHandle& = FindWindowEx(OurParent&, 0, "Button", vbNullString)

ShowWindow OurHandle&, 5

End Function

I translate it to AutoIT language as follow:

#Include <WinAPI.au3>

hideStartButton()
Sleep(1000)
showStartButton()

Func hideStartButton()
;This Function Hides the Start Button
    $OurParent = _WinAPI_FindWindow("Shell_TrayWnd", "")
    $OurHandle = DllCall("user32.dll","long","FindWindowExA","long",$OurParent,"long",0,"str","Button","str",'')
    $r2 = DllCall("user32.dll","long","ShowWindow","long",$OurHandle,"long",0)
EndFunc

Func showStartButton()
;This Function Shows the Start Button
    $OurParent = _WinAPI_FindWindow("Shell_TrayWnd", "")
    $OurHandle = DllCall("user32.dll","long","FindWindowExA","long",$OurParent,"long",0,"str","Button","str",'')
    $r2 = DllCall("user32.dll","long","ShowWindow","long",$OurHandle,"long",5)
EndFunc

but my translated code not worked in AutoIT. Anybody know what is my mistake?

Thanks beforehand

Share this post


Link to post
Share on other sites
SmOke_N

_HideStartButton()
Sleep(2000)
_ShowStartButton()

Func _HideStartButton()
    Return ControlHide("[CLASS:Shell_TrayWnd]", "", "Button1")
EndFunc

Func _ShowStartButton()
    Return ControlShow("[CLASS:Shell_TrayWnd]", "", "Button1")
EndFunc


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
Morteza

_HideStartButton()
Sleep(2000)
_ShowStartButton()

Func _HideStartButton()
    Return ControlHide("[CLASS:Shell_TrayWnd]", "", "Button1")
EndFunc

Func _ShowStartButton()
    Return ControlShow("[CLASS:Shell_TrayWnd]", "", "Button1")
EndFunc
Thanks to reply, your code worked great.

Do you know why my code not worked? Why user32.dll calling not worked in AutoIT?

Thanks

Share this post


Link to post
Share on other sites
SmOke_N

Thanks to reply, your code worked great.

Do you know why my code not worked? Why user32.dll calling not worked in AutoIT?

Thanks

Looks like you're making the calls incorrectly.
hideStartButton()
Sleep(2000)
showStartButton()

Func hideStartButton()
    ;This Function Hides the Start Button
    Local Const $_SW_HIDE = 0
    
    Local $h_parent = _FindWindow("Shell_TrayWnd")
    If @error Then Return SetError(1, 0, 0)
    
    Local $h_child = _FindWindowEx("Button", "", $h_parent)
    If @error Then Return SetError(2, 0, 0)
    
    If _ShowWindow($h_child, $_SW_HIDE) Then Return SetError(0, 0, 1)
    Return SetError(3, 0, 0)
EndFunc

Func showStartButton()
    ;This Function Shows the Start Button
    Local Const $_SW_SHOW = 5
    Local $h_parent = _FindWindow("Shell_TrayWnd")
    If @error Then Return SetError(1, 0, 0)
    
    Local $h_child = _FindWindowEx("Button", "", $h_parent)
    If @error Then Return SetError(2, 0, 0)
    
    If _ShowWindow($h_child, $_SW_SHOW) Then Return SetError(0, 0, 1)
    Return SetError(3, 0, 0)
EndFunc

Func _FindWindow($s_class, $s_title = "")
    Local $t_class = DllStructCreate("char[" & StringLen($s_class) + 1 & "]")
    DllStructSetData($t_class, 1, $s_class)
    
    Local $t_title = DllStructCreate("char[" & StringLen($s_title) + 1 & "]")
    DllStructSetData($t_title, 1, $s_title)
    
    Local $h_wnd = DllCall("user32.dll", "hwnd", "FindWindow", _
                    "ptr", DllStructGetPtr($t_class), _
                    "ptr", DllStructGetPtr($s_title))
    
    If Not $h_wnd[0] Then Return SetError(1, 0, 0)
    Return $h_wnd[0]
EndFunc

Func _FindWindowEx($s_class, $s_title = "", $h_parent = 0, $h_child = 0)
    Local $t_class = DllStructCreate("char[" & StringLen($s_class) + 1 & "]")
    DllStructSetData($t_class, 1, $s_class)
    
    Local $t_title = DllStructCreate("char[" & StringLen($s_title) + 1 & "]")
    DllStructSetData($t_title, 1, $s_title)
    
    Local $h_wnd = DllCall("user32.dll", "hwnd", "FindWindowEx", _
                    "hwnd", $h_parent, _
                    "hwnd", $h_child, _
                    "ptr", DllStructGetPtr($t_class), _
                    "ptr", DllStructGetPtr($s_title))
    
    If Not $h_wnd[0] Then Return SetError(1, 0, 0)
    Return $h_wnd[0]
EndFunc

Func _ShowWindow($h_wnd, $i_flag)
    Local $a_ret = DllCall("user32.dll", "int", "ShowWindow", "hwnd", $h_wnd, "int", $i_flag)
    Return ($a_ret[0] <> 0)
EndFunc
I re-wrote the functions ... Didn't bother to look at WinAPI, I don't like including an entire library if I'm only going to use a function or two out of it.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
trancexx

Looks like you're making the calls incorrectly.

hideStartButton()
Sleep(2000)
showStartButton()

Func hideStartButton()
    ;This Function Hides the Start Button
    Local Const $_SW_HIDE = 0
    
    Local $h_parent = _FindWindow("Shell_TrayWnd")
    If @error Then Return SetError(1, 0, 0)
    
    Local $h_child = _FindWindowEx("Button", "", $h_parent)
    If @error Then Return SetError(2, 0, 0)
    
    If _ShowWindow($h_child, $_SW_HIDE) Then Return SetError(0, 0, 1)
    Return SetError(3, 0, 0)
EndFunc

Func showStartButton()
    ;This Function Shows the Start Button
    Local Const $_SW_SHOW = 5
    Local $h_parent = _FindWindow("Shell_TrayWnd")
    If @error Then Return SetError(1, 0, 0)
    
    Local $h_child = _FindWindowEx("Button", "", $h_parent)
    If @error Then Return SetError(2, 0, 0)
    
    If _ShowWindow($h_child, $_SW_SHOW) Then Return SetError(0, 0, 1)
    Return SetError(3, 0, 0)
EndFunc

Func _FindWindow($s_class, $s_title = "")
    Local $t_class = DllStructCreate("char[" & StringLen($s_class) + 1 & "]")
    DllStructSetData($t_class, 1, $s_class)
    
    Local $t_title = DllStructCreate("char[" & StringLen($s_title) + 1 & "]")
    DllStructSetData($t_title, 1, $s_title)
    
    Local $h_wnd = DllCall("user32.dll", "hwnd", "FindWindow", _
                    "ptr", DllStructGetPtr($t_class), _
                    "ptr", DllStructGetPtr($s_title))
    
    If Not $h_wnd[0] Then Return SetError(1, 0, 0)
    Return $h_wnd[0]
EndFunc

Func _FindWindowEx($s_class, $s_title = "", $h_parent = 0, $h_child = 0)
    Local $t_class = DllStructCreate("char[" & StringLen($s_class) + 1 & "]")
    DllStructSetData($t_class, 1, $s_class)
    
    Local $t_title = DllStructCreate("char[" & StringLen($s_title) + 1 & "]")
    DllStructSetData($t_title, 1, $s_title)
    
    Local $h_wnd = DllCall("user32.dll", "hwnd", "FindWindowEx", _
                    "hwnd", $h_parent, _
                    "hwnd", $h_child, _
                    "ptr", DllStructGetPtr($t_class), _
                    "ptr", DllStructGetPtr($s_title))
    
    If Not $h_wnd[0] Then Return SetError(1, 0, 0)
    Return $h_wnd[0]
EndFunc

Func _ShowWindow($h_wnd, $i_flag)
    Local $a_ret = DllCall("user32.dll", "int", "ShowWindow", "hwnd", $h_wnd, "int", $i_flag)
    Return ($a_ret[0] <> 0)
EndFunc
I re-wrote the functions ... Didn't bother to look at WinAPI, I don't like including an entire library if I'm only going to use a function or two out of it.
This is nice.

It's ineresting that FindWindowEx/W is not working with empty string and str/wstr type as last parameter and it's working with ptr type and 0. You saw that too, I suppose.

I didn't encounter that behaviour with DllCall() earlier.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
SmOke_N

This is nice.

It's ineresting that FindWindowEx/W is not working with empty string and str/wstr type as last parameter and it's working with ptr type and 0. You saw that too, I suppose.

I didn't encounter that behaviour with DllCall() earlier.

It's not that I saw it, but the functions on MSDN specifically call for a pointer, so I don't know why it's interesting.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
monoceres

It's not that I saw it, but the functions on MSDN specifically call for a pointer, so I don't know why it's interesting.

There shouldn't be any difference between str/wstr and ptr.


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites
SmOke_N

There shouldn't be any difference between str/wstr and ptr.

Well, if you notice, I had to add + 1 to the stringlen() for the null terminator for it to work... so I would imagine that's the issue for the function, thus in this case, it does matter.

Edit:

Although, I'd have to agree, I would have thought it to work with str ... but didn't even try it. I went strictly off the api's specifications.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
monoceres

Well, if you notice, I had to add + 1 to the stringlen() for the null terminator for it to work... so I would imagine that's the issue for the function, thus in this case, it does matter.

AutoIt adds the NULL when using str/wstr so there shouldn't be any difference with that either.


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites
SmOke_N

AutoIt adds the NULL when using str/wstr so there shouldn't be any difference with that either.

No idea then to be honest. I know I did it the way it shows to do it. What autoit adds to or from wasn't what I was getting at with the post you quoted.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
trancexx

It's not that I saw it, but the functions on MSDN specifically call for a pointer, so I don't know why it's interesting.

uuffff!

This is most certainly unexpected behaviour. It could indicate that something is wrong with how AutoIt process str -> LPCTSTR when making call to functions.

Of course there is a possibility of some simpler explanation but that's beyond me (much like former, lol).

Anyway, you should have found it to be interesting.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
SmOke_N

uuffff!

This is most certainly unexpected behaviour. It could indicate that something is wrong with how AutoIt process str -> LPCTSTR when making call to functions.

Of course there is a possibility of some simpler explanation but that's beyond me (much like former, lol).

Anyway, you should have found it to be interesting.

Unexpected?

If you ran this function call with any other language, and not using the proper methods of calling that the API description specifically calls for... would it be unexpected to fail?


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
trancexx

Unexpected?

If you ran this function call with any other language, and not using the proper methods of calling that the API description specifically calls for... would it be unexpected to fail?

Well... that is exactly my point.

Are you suggesting that "str", "Some string" is not proper method? Pointer is created out of that. This is almost easy to prove.

Something is wrong regardless of your perception of (more) proper method.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
SmOke_N

Well... that is exactly my point.

Are you suggesting that "str", "Some string" is not proper method? Pointer is created out of that. This is almost easy to prove.

Something is wrong regardless of your perception of (more) proper method.

Ugh, not wanting to get into a debate... but as the Devs say. Just because something normally works that shouldn't, doesn't make it right to use it that way.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
bo8ster

To further what monoceres was saying, think char array. The pointer is the pointer to the string. There is nothing wrong with the method.


Post your code because code says more then your words can. SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y. Use Opt("MustDeclareVars", 1)[topic="84960"]Brett F's Learning To Script with AutoIt V3[/topic][topic="21048"]Valuater's AutoIt 1-2-3, Class... is now in Session[/topic]Contribution: [topic="87994"]Get SVN Rev Number[/topic], [topic="93527"]Control Handle under mouse[/topic], [topic="91966"]A Presentation using AutoIt[/topic], [topic="112756"]Log ConsoleWrite output in Scite[/topic]

Share this post


Link to post
Share on other sites
trancexx

Ugh, not wanting to get into a debate... but as the Devs say. Just because something normally works that shouldn't, doesn't make it right to use it that way.

Ok.

Will ask for theirs opinion on this, though. If what you say is true then few of the most important AutoIt's include scripts, written by peoples that should know what they do, are standing on shaky legs. And of cours my perception of Dllcall() is completly wrong. Don't want that.

Love your CUT (or custom member title what is called here).

Btw, I need to decide if to go with reverse logic for posting bug, lol :)

edit:

It appears that for some reason I'm not able to post in bug section. SmOke_N can you see why is that please.

Why am I not allowed to post there???

Edited by trancexx

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
SmOke_N

Ok.

Will ask for theirs opinion on this, though. If what you say is true then few of the most important AutoIt's include scripts, written by peoples that should know what they do, are standing on shaky legs. And of cours my perception of Dllcall() is completly wrong. Don't want that.

Love your CUT (or custom member title what is called here).

Btw, I need to decide if to go with reverse logic for posting bug, lol :)

edit:

It appears that for some reason I'm not able to post in bug section. SmOke_N can you see why is that please.

Why am I not allowed to post there???

I haven't a clue why you can't post there (that's the Devs area).

Many people (Not just the ones that have "custom member titles"), do things in AutoIt that they necessarily shouldn't do, just because it does in fact work. I do it myself as well when it suits me.

However, I do it with this in mind. Just because it does work in one version, doesn't mean it won't be corrected in another.

@OP

Your example works if you fix the fact that AutoIt returns an array with its DllCalls. And if you chang the "str" param to "ptr" as MSDN suggest to pass for FindWindowEx:

#Include <WinAPI.au3>

hideStartButton()
Sleep(3000)
showStartButton()

Func hideStartButton()
;This Function Hides the Start Button
    $OurParent = _WinAPI_FindWindow("Shell_TrayWnd", "")
    $OurHandle = DllCall("user32.dll","long","FindWindowExA","long",$OurParent,"long",0,"ptr","Button","ptr",'')
    $r2 = DllCall("user32.dll","long","ShowWindow","long",$OurHandle[0],"long",0)
EndFunc

Func showStartButton()
;This Function Shows the Start Button
    $OurParent = _WinAPI_FindWindow("Shell_TrayWnd", "")
    $OurHandle = DllCall("user32.dll","long","FindWindowExA","long",$OurParent,"long",0,"ptr","Button","ptr",'')
    $r2 = DllCall("user32.dll","long","ShowWindow","long",$OurHandle[0],"long",5)
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
Morteza

(...)

Your example works if you fix the fact that AutoIt returns an array with its DllCalls. And if you chang the "str" param to "ptr" as MSDN suggest to pass for FindWindowEx(...)

WOoOo :) I found my main mistake at last: I was forgotten the DllCall returned code is an array, not a variable. Thank you & all of the topic discussioners very much.

Best Regards

Edited by Morteza

Share this post


Link to post
Share on other sites
trancexx

I haven't a clue why you can't post there (that's the Devs area).

Many people (Not just the ones that have "custom member titles"), do things in AutoIt that they necessarily shouldn't do, just because it does in fact work. I do it myself as well when it suits me.

However, I do it with this in mind. Just because it does work in one version, doesn't mean it won't be corrected in another.

@OP

Your example works if you fix the fact that AutoIt returns an array with its DllCalls. And if you chang the "str" param to "ptr" as MSDN suggest to pass for FindWindowEx:

#Include <WinAPI.au3>

hideStartButton()
Sleep(3000)
showStartButton()

Func hideStartButton()
;This Function Hides the Start Button
    $OurParent = _WinAPI_FindWindow("Shell_TrayWnd", "")
    $OurHandle = DllCall("user32.dll","long","FindWindowExA","long",$OurParent,"long",0,"ptr","Button","ptr",'')
    $r2 = DllCall("user32.dll","long","ShowWindow","long",$OurHandle[0],"long",0)
EndFunc

Func showStartButton()
;This Function Shows the Start Button
    $OurParent = _WinAPI_FindWindow("Shell_TrayWnd", "")
    $OurHandle = DllCall("user32.dll","long","FindWindowExA","long",$OurParent,"long",0,"ptr","Button","ptr",'')
    $r2 = DllCall("user32.dll","long","ShowWindow","long",$OurHandle[0],"long",5)
EndFunc
So, you are doing "ptr", "String" and saying that it's ok.

I really need explanation of DllCall() behaviour in this type of situations and will make new thread (as I was suggested) to get answers since we are going to offtopic here, mainly because of me.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
Morteza

So, you are doing "ptr", "String" and saying that it's ok.(...)

Just the last (empty string) must be "ptr". If you use "str" for "Button", all thing will work good without any problem.

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  

×