cancel
Showing results for 
Search instead for 
Did you mean: 

API declaration in codes, how to do setting in object

EricLi
Level 6
If there are API declarations in codes, how to do setting in object. What to be set in Business Object Properties on Initiate Page and what to be write in code stage?

May I have your help based on the below codes:

Option Explicit
Private Declare Function PostMessage Lib "User32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long 

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

Private Const WM_CLOSE = &H10 

Private Sub Command1_Click()
    Dim winHwnd As Long
    Dim RetVal As Long
    winHwnd = FindWindow(vbNullString, 
    Debug.Print winHwnd 

    If winHwnd <> 0 Then 
        RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&) 
        If RetVal = 0 Then 
            MsgBox "错误!"
        Else
            MsgBox "成功关闭WINDOWS窗口!"
        End If

    Else
        MsgBox "WINDOWS窗口没有发现!"
    End If

End Sub

------------------------------
Eric Li
Operation Analysis
AIG
Asia/Hong_Kong
------------------------------
8 REPLIES 8

AndreyKudinov
Level 10
You can put declarations in global code, sub can go there too (and you just call it by name then in code stage) or you can also put its body in code stage itself.
Although, I wonder why would you need to use winapi, when blueprism can do the same natively - try attaching to a window and close it.

MsgBox (one of the MessageBox*) would need to be declared too and then bot would stop every time it pops up and wait for result.. do you really want that? There is no debug.print either.

p.s. code is not complete here: winHwnd = FindWindow(vbNullString, ???

------------------------------
Andrey Kudinov
Project Manager
MobileTelesystems PJSC
Europe/Moscow
------------------------------

Thank you, Andrey

 

The target window cannot be attached on whatever ways I have tried. So want to use code to close it.

 

The MsgBox I will revised it later.

 

Yes, winHwnd is not complete, I missed something when doing copy.

 

Eric Li

AIG

 

Automation Delivery Team | China Service Center

 

9F, The Centerpoint (Shuion), 374-2 Beijing Road, Guangzhou, China 510030

 

Tel: (+86 20) 2882 5022

 

Eric-F.Li@aig.comwww.aig.com

 



I had to tweak declarations a bit, but it works.
Check attachment

------------------------------
Andrey Kudinov
Project Manager
MobileTelesystems PJSC
Europe/Moscow
------------------------------

Thanks, Andrey.  Using check code function, system indicate the compiler error as attached screen.

 

21733.jpg

 

 

Eric Li

AIG

 

Automation Delivery Team | China Service Center

 

9F, The Centerpoint (Shuion), 374-2 Beijing Road, Guangzhou, China 510030

 

Tel: (+86 20) 2882 5022

 

Eric-F.Li@aig.comwww.aig.com

 



Works for me either way, but you can try replacing VbNullString with Nothing.

------------------------------
Andrey Kudinov
Project Manager
MobileTelesystems PJSC
Europe/Moscow
------------------------------

Many thanks! Andrey. It works after replaced with Nothing.

 

Eric Li

AIG

 

Automation Delivery Team | China Service Center

 

9F, The Centerpoint (Shuion), 374-2 Beijing Road, Guangzhou, China 510030

 

Tel: (+86 20) 2882 5022

 

Eric-F.Li@aig.comwww.aig.com

 



One more question, is it possible to use wildcard on the windows name. Because the windows I am suffering have different name, but they have the same prefix name.

 

Eric Li

AIG

 

Automation Delivery Team | China Service Center

 

9F, The Centerpoint (Shuion), 374-2 Beijing Road, Guangzhou, China 510030

 

Tel: (+86 20) 2882 5022

 

Eric-F.Li@aig.comwww.aig.com

 



I'd try window class name first. If class name is unique, it might work.
Otherwise, you'd have to use EnumWindows API to get all windows and look for the one you need afaik.
https://www.pinvoke.net/default.aspx/user32.EnumWindows


------------------------------
Andrey Kudinov
Project Manager
MobileTelesystems PJSC
Europe/Moscow
------------------------------