Solved

call vb6 click from vb.net 22008

Posted on 2009-04-02
18
409 Views
Last Modified: 2013-12-26
There is a vb6 app called scold1.exe
I want to call the code from dot net to make it open with out having to touch the vb6 app

the vb6 code is, I am new to dotnet and programming in general so any help would be appreciated


Private Sub mnuSetup_Click()
    Timer1.Enabled = False
    bSetup = True
    frmWizard.Show vbModal
    If UCase(Trim(Command())) <> "/ADMIN" Then
        Me.Hide
    End If
    bSetup = False
    Timer1.Interval = 10000
    Timer1.Enabled = True
End Sub
0
Comment
Question by:hainansyndrome
  • 8
  • 7
18 Comments
 
LVL 31

Expert Comment

by:Frosty555
ID: 24053270
You can execute an external program in VB.NET using the System.Diagnostics.Process.Start() function.

     System.Diagnostics.Process.Start("C:\YourExe.exe")

You can also create a new process object and actually keep track of the process as it runs. Doing it that way will give you more detailed control over the process. See this example:

     http://www.freevbcode.com/ShowCode.asp?ID=5879
0
 

Author Comment

by:hainansyndrome
ID: 24053338
I am already opening it with this



            If (File.Exists("c:\scold1.exe")) Then
                System.Diagnostics.Process.Start("C:\scold1.exe", "/setup")
            Else
                MsgBox("Scold is Not Installed In C:\", , "Scoldis not here!")
            End If
   
What i want to do is call the info in my post if it is already running, we want to avoid closing scold1.exe


0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24053361
If you want to automatically click that button then your best bet is to get a handle to the main window:

    Dim P As Process = Process.Start("c:\some\path\scold1.exe")
    P.WaitForInputIdle()
    Dim pHandle As IntPtr = P.MainWindowHandle

Now you can use "pHandle" with the FindWindowEx() API to get a handle to the target button.  If there is more than one button then you may need to get a handle to the first one and then iterate until you find the correct one.  Once you have a handle to the button, you can use SendMessage() with BM_CLICK to click it.
http://www.experts-exchange.com/Programming/Languages/.NET/Q_23787272.html
0
 

Author Comment

by:hainansyndrome
ID: 24053395
What I am trying to do is have 2 buttons
1 opens the app if it is closed
the other will pass the above posted info to cause it to open to the admin page
Ill research what you posted Idle_mind but not sure it is what i am looking for
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24053454
You can check to see if it is open already with code like this:

        Dim FileName As String = "c:\some\path\scold1.exe"

        Dim ps() As Process = Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(FileName))

        If ps.Length = 0 Then

            Process.Start(FileName) ' not running...so start it up

        Else

            ' it is already running...

        End If

Open in new window

0
 

Author Comment

by:hainansyndrome
ID: 24053504
IT is close

Here is my button 1
What I want is to code a button 2 so if itis open it can pass in the code to make it open, accessing the vb6 exe directly, is that possible? someone sugested teh Shell_NotifyIcon



Dim T() As Process = Process.GetProcessesByName("Scold1") '
        If T.Length > 0 Then
            MsgBox("TScold already Running.", , "Can not Open Scold")
        Else
            If (File.Exists("c:\scold1.exe")) Then
                System.Diagnostics.Process.Start("c:\scold1.exe", "/setup")
            Else
                MsgBox("Scold is Not Installed In C:\", , "Scold is not here!")
            End If
        End If
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24053674
Does passing in "/setup" as a command line parameter make it do EVERYTHING that you want?
...or do you still need to simulate the user manually clicking on a button in Scold1?
0
 

Author Comment

by:hainansyndrome
ID: 24053702
i need to simulate the user clicking
if i launch the app with the /setup when the app is closed all goes correct
but i dont want to open it if it is already running so I need a way to simulate the clicks
as you put it
or call the running exe directly

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24054101
Gotcha.  Grab yourself WinSpector (Free):
http://www.windows-spy.com/

This will allow you to easily the hierarchy of controls along with their classnames in your target app.
See the image below.   It shows the buttons in Calculator.

Here is some code that can click the "C" (clear button) in Calculator.  Note that it is looking for a window, starting with the main window handle, that has a ClassName of "Button" AND a Value of "C":

    Dim cBtnHandle As IntPtr = FindWindowEx(P.MainWindowHandle, IntPtr.Zero, "Button", "C")

The code:
Public Class Form1
 

    Private Const BM_CLICK As Integer = &HF5

    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal handle As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer

    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As IntPtr, ByVal hWnd2 As IntPtr, ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim P As Process

        Dim ps() As Process = Process.GetProcessesByName("calc")

        If ps.Length = 0 Then

            P = Process.Start("calc")

            P.WaitForInputIdle()

        Else

            P = ps(0)

        End If
 

        Dim cBtnHandle As IntPtr = FindWindowEx(P.MainWindowHandle, IntPtr.Zero, "Button", "C")

        If Not cBtnHandle.Equals(IntPtr.Zero) Then

            SendMessage(cBtnHandle, BM_CLICK, 0, 0)

        End If

    End Sub
 

End Class

Open in new window

WinspectorWithCalc.jpg
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24054113
*In VB6, I believe all the controls start with "Thunder", so you'll have to find the correct classname for your VB6 button with Winspector and replace "Button" with "ThunderXXXXX"...whatever the XXX is....
0
 

Author Comment

by:hainansyndrome
ID: 24054366
ok I think I have it

maybe like i said I am a newbie
so ThunderRT6CommandButton is what i found
so

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim P As Process
        Dim ps() As Process = Process.GetProcessesByName("Scold1")
        If ps.Length = 0 Then
            P = Process.Start("Scold1")
            P.WaitForInputIdle()
        Else
            P = ps(0)
        End If
 
        Dim cBtnHandle As IntPtr = FindWindowEx(P.MainWindowHandle, IntPtr.Zero, "ThunderRT6CommandButton", "C")
        If Not cBtnHandle.Equals(IntPtr.Zero) Then
            SendMessage(cBtnHandle, BM_CLICK, 0, 0)
        End If
    End Sub
 
End Class
0
 

Author Comment

by:hainansyndrome
ID: 24054377
illplay tonight and let you know later
thanks for the stear in the right
directions
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24054553
In this line:

    Dim cBtnHandle As IntPtr = FindWindowEx(P.MainWindowHandle, IntPtr.Zero, "ThunderRT6CommandButton", "C")

Change the "C" to whatever the button says (the "caption" in VB6)...it might be "Setup"?
0
 

Author Comment

by:hainansyndrome
ID: 24055067
actually there is not button

when scoold is running
there is a icon in the tasktry
i have to right click it and choose setup from 3 options
this does the same as opening it with the command line
scold.exe /setup

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 24055127
Ack...  =\

I'm sorry...that is a completely different beast then!  It is NOT a trivial task to invoke a SystemTray icons menu and click on it.  =(

You'd probably be better off killing it and restarting it with your desired command-line parameters:

        For Each processOnDeathRow As Process In Process.GetProcessesByName("Scold1")
            processOnDeathRow.Kill()
        Next
        System.Threading.Thread.Sleep(2000) ' give windows a chance to do its thing...
        Process.Start("c:\scold1.exe", "/setup")
0
 

Author Comment

by:hainansyndrome
ID: 24056438
yes see here is the kicker Number #2
when scold is running it could be accessing a dll and doing some dat aback up
i am wanting to write the app to open it so we can see if it is running
if you open then it just pauses the data backup
I dont want to kill it, just pause it when i need to check something

but any ideas in the right direction would be nice
thanks for all the time and effort
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Creating and Managing Databases with phpMyAdmin in cPanel.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now