Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 458
  • Last Modified:

call vb6 click from vb.net 22008

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
hainansyndrome
Asked:
hainansyndrome
  • 8
  • 7
1 Solution
 
Frosty555Commented:
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
 
hainansyndromeAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
hainansyndromeAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
hainansyndromeAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
hainansyndromeAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
*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
 
hainansyndromeAuthor Commented:
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
 
hainansyndromeAuthor Commented:
illplay tonight and let you know later
thanks for the stear in the right
directions
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
hainansyndromeAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
hainansyndromeAuthor Commented:
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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now