Jump to content

Win API doubt


Sidharth
 Share

Recommended Posts

Hi All

I have question regarding Windows API.

I am trying to build small automation of copying one file from open folder to two different folders. I am doing this using mouse and keyboard events (basically searching windows by title and then copy).

My code should execute first copy command and then should start working for second copy, since file is quite large it is stuck there and control is not coming back to the code.

i tried putting in delay. Is multi threading a solution ? Any help or direction to any guide will be much appreciated.

Background: I am using VBA to do this, Posting on this forum as autoit source will have same functionality.

Regards

Sidharth Rai

Link to comment
Share on other sites

not able to post in developer sections. some restriction or something.

Can you tell me how does autoit resolve such dependencies, does it give delay or something and how does it handle controls..

and also if you can post my problem in developed section..

Link to comment
Share on other sites

If you want to perform copy operations in the background then take a look There is an example in the first post for copying multiple files.

An alternative is to use more than one process. Multithreading is not possible in AutoIt, but if you run the cmd command (which I assume you can do from most lanaguages) then you can run it as a seperate process. To do that in AutoIt look at the Run function and the @ComSpec macro (there is a section in the Run docs about using it). If you store the PID then you can use the ProcessExists function to check if it is still in progress.

Link to comment
Share on other sites

Hi mat

Thanks for your help.

Basically i want to implement this in Windows API, First i will open folder "AB" using title search and then copy folder on location (600,400). Now i seach "CD" and paste at (400,600).Now i search for folder "FG" and copy folder at (600,400) and then come back/paste at "CD".

Problem i am facing is - i reach "AB" i copy the folder and then i goto "CD" and run paste command, everything works perfect till here. Now my source code execution is not taking place.

I tried to debug, debugger stops after first paste operation.I beleive we need to exchange controls using PIDs or somthing.

I am posting this question here because i believe Autoit Source code will be implementing this functionality (can you just give me hint or somehing).

Regards

Sidharth Rai

Link to comment
Share on other sites

This discussion is going nowhere without the code you're trying. Post something we can help debug. But don't expect anyone to implement your idea for you, there's plenty of examples (and probably a UDF or two) to do exactly what you want.

Edited by wraithdu
Link to comment
Share on other sites

  • 2 weeks later...

I am sorry for late reply , following is my code- can you please let me know why is my code not returning for the exceution of second call

main fucntion is Right_Down() and I am trying to operate the requried opertions on two windows named "Critical" and "CriticalAll".

'**Win32 API Declarations

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

Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long

Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long

Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)

Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long

Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long

Private Declare Function IsIconic Lib "user32" (ByVal hWnd As Long) As Long

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

Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long

Private Declare Function GetForegroundWindow Lib "user32" () As Long

Private Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long

Private Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hWnd As Long, ByVal wFlag As Long) As Long

Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Const KEYEVENTF_EXTENDEDKEY = &H1

Const KEYEVENTF_KEYUP = &H2

'**Win32 API User Defined Types

Private Type RECT

Left As Long

Top As Long

Right As Long

Bottom As Long

End Type

Private Const SW_RESTORE = 9

Private Const SW_SHOW = 5

Const MOUSEEVENTF_RIGHTDOWN = &H8

Const MOUSEEVENTF_RIGHTUP = &H10

Const MOUSEEVENTF_MOVE = &H1 ' mouse move

Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down

Const MOUSEEVENTF_LEFTUP = &H4 ' left button up

'Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down

'Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up

Const MOUSEEVENTF_MIDcDLEDOWN = &H20 ' middle button down

Const MOUSEEVENTF_MIDDLEUP = &H40 ' middle button up

Const MOUSEEVENTF_ABSOLUTE = &H8000 ' absolute move

Const MOUSEEVENTF_WHEEL = &H800 ' wheel button rolled

Private Sub test()

Dim Rec As RECT

'Get Left, Right, Top and Bottom of Form1

GetWindowRect GetWindowHandle, Rec

'Set Cursor position on X

SetCursorPos Rec.Right - 600, Rec.Top + 400

End Sub

Private Function GetWindowHandle() As Long

Const CLASSNAME_MSExcel = "XLMAIN"

'Gets the Apps window handle, since you can't use App.hInstance in VBA (VB Only)

GetWindowHandle = FindWindow(vbNullString, "test1.xlsx")

End Function

Public Sub RightDown()

FnSetForegroundWindow "critical"

FnSetForegroundWindow "critical1"

End Sub

Public Function FnSetForegroundWindow(strWindowTitle As String) As Boolean

Dim MyAppHWnd As Long

Dim CurrentForegroundThreadID As Long

Dim NewForegroundThreadID As Long

Dim lngRetVal As Long

Dim text1 As String

Dim textlen As Long

Dim blnSuccessful As Boolean

MyAppHWnd = FindWin(strWindowTitle)

'MyAppHWnd = FnFindWindowLike(strWindowTitle)

If MyAppHWnd <> 0 Then

'We've found the application window by the caption

CurrentForegroundThreadID = GetWindowThreadProcessId(GetForegroundWindow(), ByVal 0&)

NewForegroundThreadID = GetWindowThreadProcessId(MyAppHWnd, ByVal 0&)

'AttachThreadInput is used to ensure SetForegroundWindow will work

'even if our application isn't currently the foreground window

'(e.g. an automated app running in the background)

Call AttachThreadInput(CurrentForegroundThreadID, NewForegroundThreadID, True)

lngRetVal = SetForegroundWindow(MyAppHWnd)

Call AttachThreadInput(CurrentForegroundThreadID, NewForegroundThreadID, False)

If lngRetVal <> 0 Then

'Now that the window is active, let's restore it from the taskbar

If IsIconic(MyAppHWnd) Then

' MsgBox "runing fast"

Call ShowWindow(MyAppHWnd, SW_RESTORE)

SetCursorPos 550, 200

mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0

mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0

keybd_event vbKeyControl, 0, 0, 0

keybd_event vbKeyC, 0, 0, 0

keybd_event vbKeyControl, 0, KEYEVENTF_KEYUP, 0

SetCursorPos 550, 700

keybd_event vbKeyControl, 0, 0, 0

keybd_event vbKeyV, 0, 0, 0

keybd_event vbKeyControl, 0, KEYEVENTF_KEYUP, 0

Else

Call ShowWindow(MyAppHWnd, SW_SHOW)

SetCursorPos 550, 200

mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0

mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0

keybd_event vbKeyControl, 0, 0, 0

keybd_event vbKeyC, 0, 0, 0

keybd_event vbKeyControl, 0, KEYEVENTF_KEYUP, 0

SetCursorPos 850, 700

keybd_event vbKeyControl, 0, 0, 0

keybd_event vbKeyV, 0, 0, 0

keybd_event vbKeyControl, 0, KEYEVENTF_KEYUP, 0

End If

blnSuccessful = True

Else

MsgBox "Found the window, but failed to bring it to the foreground!"

End If

Else

MsgBox "Application Window '" + strWindowTitle + "' not found!"

End If

FnSetForegroundWindow = blnSuccessful

End Function

Function FindWin(WinTitle As String) As Long

Dim lhWnd As Long, sAns As String

Dim sTitle As String

Dim hWnd As Long

lhWnd = GetForegroundWindow()

sTitle = LCase(WinTitle)

Do

DoEvents

If lhWnd = 0 Then Exit Do

sAns = LCase$(GetCaption(lhWnd))

' MsgBox sAns

If InStr(sAns, sTitle) Then

'MsgBox sAns

FindWin = lhWnd

Exit Do

Else

FindWin = 0

End If

lhWnd = GetNextWindow(lhWnd, 2)

Loop

End Function

Private Function GetCaption(lhWnd As Long) As String

Dim sAns As String, lLen As Long

lLen& = GetWindowTextLength(lhWnd)

sAns = String(lLen, 0)

Call GetWindowText(lhWnd, sAns, lLen + 1)

GetCaption = sAns

End Function

Link to comment
Share on other sites

This is an AutoIt forum so we will help with AutoIt code. Assuming the code you posted is VB then you need to ask on a VB forum.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
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...