Results 1 to 29 of 29

Thread: [RESOLVED] Loading .exe form - Brings up x2 not 1

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    147

    Resolved [RESOLVED] Loading .exe form - Brings up x2 not 1

    I have this code in the CmdSend_Click() button:

    Dim retval

    retval = Shell("C:\Program Files\Nokia\Nokia PC Suite 6\TextMessageEditor.exe", 1)

    SendKeys "abc", 1
    SendKeys "{TAB}", 1
    SendKeys "Your mesage here", 1


    It works BUT it opens the .exe file twice then sometimes it fills the fields in on just one form and other times it splits the message across the two. I've tried putting the code in CmdSend mousedown, mouseup, pressdown ect to see if that helps but even tho it does only bring up the form once it is minimized rather than on the creen with the focus set

    Any ideas on how to make the .exe file open just once?

  2. #2
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,675

    Re: Loading .exe form - Brings up x2 not 1

    You should check if the program windows has been launched before using the sendkeys.

    So after shell():

    Use the FindWindow() api to check if the window is lanuched and then try send keys.
    _____________________________________________________________________

    ----If this post has helped you. Please take time to Rate it.
    ----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.



  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    147

    Re: Loading .exe form - Brings up x2 not 1

    Ok I've come up with this code:

    Code:
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
    
    Private Sub CmdSend_Click()
    Dim retval
    Dim hWnd As Long
    
    retval = Shell("C:\Program Files\Nokia\Nokia PC Suite 6\TextMessageEditor.exe", 1)
        
        hWnd = FindWindow(vbNullString, "Nokia Text Message Editor - Connected to Nokia 6300")
        If hWnd > 0 Then
            SendKeys "abc", 1
        End If
    BUT... Even tho it s now bringing up just the one form like I want I'm getting the error: Type Mismatch on this line of code: hWnd = FindWindow(vbNullString, "Nokia Text Message Editor - Connected to Nokia 6300")

    The name i.e. "Nokia Text Message Editor - Connected to Nokia 6300" is what the form itself is called (says so at top of the form and on my menu bar)
    Any ideas?

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Loading .exe form - Brings up x2 not 1

    Hmmm, your API declaration is expecting LONG string pointers, try simply changing them to String parameter types
    If using Strings in the API call, change the delcaration parameters to be String not Long, else change your calls to use string pointers.
    Code:
    Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Last edited by LaVolpe; Mar 22nd, 2008 at 11:24 AM.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    147

    Re: Loading .exe form - Brings up x2 not 1

    Using VaLopes Line of code I no longer get the error BUT now it won't write to the form anymore

    If I put the code for SendKeys here:

    If hWnd > 0 Then
    'IFS IT OPEN
    SendKeys "abc", 1
    SendKeys "{TAB}", 1
    SendKeys "Your mesage here", 1
    End If

    It doesn't write at all but if I write the three SendKeys lines under the 'end if' then I have the same problem of it bringing up the form twice instead of once

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    147

    Re: Loading .exe form - Brings up x2 not 1

    Update the hWnd isn't working because when I change it to:

    if hWnd = 0 Then

    so equal to zero rather than more than it then goes back to writting in the forms but again bringing up two of them


    EDIT:
    I've done a step through with the code and it keeps saying hWnd = 0 even though at that point it is suppose to be reconising this line:
    hWnd = FindWindow(vbNullString, "Nokia Text Message Editor - Connected to Nokia 6300")

    I've tried changing from the form name to the path and TestMessageEditor.exe but it doesn't reconise any

    So now I'm even more confused than I was an hour ago

    When using the findwindow fuction am I suppose to be adding anything from the library, list of conponants or references??? What I mean is there is nothing extra to do is there apart from just writing the line of code to my existing code?
    Last edited by bubblegum_girl; Mar 22nd, 2008 at 12:08 PM.

  8. #8
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Loading .exe form - Brings up x2 not 1

    Good catch on your part regarding the hWnd>0 part. The shell command may be returning before the window is first displayed, which could explain why hWnd=0. Have you tried the link posted by MartinLiss?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    147

    Re: Loading .exe form - Brings up x2 not 1

    I did try the code from the website suggested by MartinLiss but I got it wrapped around my head

    I copied the private decs and put them at the top of the form. I then copied the fuction ShellandAll and then I done this:

    Private Sub CmdSend_Click()
    Dim retval
    retval = Shell("C:\Program Files\Nokia\Nokia PC Suite 6\TextMessageEditor.exe", vbNormalFocus)
    '********************************
    SendKeys "abc", 1
    SendKeys "{TAB}", 1
    SendKeys "Your mesage here", 1
    end sub

    Where the line of stars are is where I think I needed to call the ShellandAll function to perform the wait but no matter what I tried it wouldn't work

    I'm starting to pull my hair out because I keep getting both the different ways wrong and I can't seem to find the way forward

  10. #10
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,966

    Re: Loading .exe form - Brings up x2 not 1

    Quote Originally Posted by bubblegum_girl
    I did try the code from the website suggested by MartinLiss but I got it wrapped around my head

    I copied the private decs and put them at the top of the form. I then copied the fuction ShellandAll and then I done this:

    Private Sub CmdSend_Click()
    Dim retval
    retval = Shell("C:\Program Files\Nokia\Nokia PC Suite 6\TextMessageEditor.exe", vbNormalFocus)
    '********************************
    SendKeys "abc", 1
    SendKeys "{TAB}", 1
    SendKeys "Your mesage here", 1
    end sub

    Where the line of stars are is where I think I needed to call the ShellandAll function to perform the wait but no matter what I tried it wouldn't work

    I'm starting to pull my hair out because I keep getting both the different ways wrong and I can't seem to find the way forward
    i tested your code with a program that i have(priginal text editor program), and works fine. and only executes 1 time.
    other thing i need to know: when you execute the program who win the focus, is the textbox?

    heres my change:

    Code:
    Shell "C:\Documents and Settings\Joaquim\Os meus documentos\Visual Basic 6.0\Cambalinho\Cambalinho.exe", vbNormalFocus
    '********************************
    SendKeys "abc", 1
    SendKeys "{TAB}", 1
    SendKeys "Your mesage here", 1
    i put these code in command1_click sub...
    test the new changes and tell me if it works...
    Last edited by joaquim; Mar 22nd, 2008 at 02:10 PM.
    VB6 2D Sprite control

    To live is difficult, but we do it.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    147

    Re: Loading .exe form - Brings up x2 not 1

    The Nokia form has the focus but I'm still getting two forms up instead of one and the message is being written across both
    Last edited by bubblegum_girl; Mar 22nd, 2008 at 02:12 PM.

  12. #12
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,966

    Re: Loading .exe form - Brings up x2 not 1

    Quote Originally Posted by bubblegum_girl
    The fuction to cause the delay isn't being used because it isn't being called at any point so I'm still getting two forms up instead of one and the message is being written across both
    when you did the message i was editing the message.... see the message again..
    stay well
    Last edited by joaquim; Mar 22nd, 2008 at 02:15 PM.
    VB6 2D Sprite control

    To live is difficult, but we do it.

  13. #13
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,966

    Re: Loading .exe form - Brings up x2 not 1

    Quote Originally Posted by bubblegum_girl
    The Nokia form has the focus but I'm still getting two forms up instead of one and the message is being written across both
    and their caption are the same?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    147

    Re: Loading .exe form - Brings up x2 not 1

    I've took out the retval = and the brackets and I'm still getting the same problem (calling two forms and message splitting along the two)

  15. #15
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,966

    Re: Loading .exe form - Brings up x2 not 1

    Quote Originally Posted by bubblegum_girl
    I've took out the retval = and the brackets and I'm still getting the same problem (calling two forms and message splitting along the two)
    if you click again in button the program will be open again....
    for you block these problem you can use a timer(intead a wait api function). the timer is for catch(use the findwindow api function) if the nokia program is open or not and if it's you can disable the form(enable form propertie to false).
    Last edited by joaquim; Mar 22nd, 2008 at 02:30 PM.
    VB6 2D Sprite control

    To live is difficult, but we do it.

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    147

    Re: Loading .exe form - Brings up x2 not 1

    Going back to the link MartinLiss suggested this is my full code on a single form with just one button called CmdSend:

    Code:
    Option Explicit
    
    Private Declare Function OpenProcess Lib "kernel32" _
    (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
    ByVal dwProcessId As Long) As Long
    
    Private Declare Function GetExitCodeProcess Lib "kernel32" _
    (ByVal hProcess As Long, lpExitCode As Long) As Long
    
    Private Const STATUS_PENDING = &H103&
    Private Const PROCESS_QUERY_INFORMATION = &H400
    
    Public Function ShellandWait(ExeFullPath As String, _
    Optional TimeOutValue As Long = 0) As Boolean
        
        Dim lInst As Long
        Dim lStart As Long
        Dim lTimeToQuit As Long
        Dim sExeName As String
        Dim lProcessId As Long
        Dim lExitCode As Long
        Dim bPastMidnight As Boolean
        
        On Error GoTo ErrorHandler
    
        lStart = CLng(Timer)
        sExeName = ExeFullPath
    
        'Deal with timeout being reset at Midnight
        If TimeOutValue > 0 Then
            If lStart + TimeOutValue < 86400 Then
                lTimeToQuit = lStart + TimeOutValue
            Else
                lTimeToQuit = (lStart - 86400) + TimeOutValue
                bPastMidnight = True
            End If
        End If
    
        lInst = Shell(sExeName, vbMinimizedNoFocus)
        
    lProcessId = OpenProcess(PROCESS_QUERY_INFORMATION, False, lInst)
    
        Do
            Call GetExitCodeProcess(lProcessId, lExitCode)
            DoEvents
            If TimeOutValue And Timer > lTimeToQuit Then
                If bPastMidnight Then
                     If Timer < lStart Then Exit Do
                Else
                     Exit Do
                End If
        End If
        Loop While lExitCode = STATUS_PENDING
        
        ShellandWait = True
       
    ErrorHandler:
    ShellandWait = False
    Exit Function
    End Function
    
    Private Sub CmdSend_Click()
    Shell "C:\Program Files\Nokia\Nokia PC Suite 6\TextMessageEditor.exe", vbNormalFocus
    SendKeys "abc", 1
    SendKeys "{TAB}", 1
    SendKeys "Your mesage here", 1
    End Sub
    Is the ShellandAll function being called as standard because its public or do I need to somehow/somewhere call it in between loading the form and typing the text for the delay to happen?

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    147

    Re: Loading .exe form - Brings up x2 not 1

    Quote Originally Posted by joaquim
    but the forms have the same caption?
    yes exactly the same exe file but just two of them just like bringing up two calculators for example and putting half the numbers in one calculator and the other half of the numbers in to the other

  18. #18
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,966

    Re: Loading .exe form - Brings up x2 not 1

    Quote Originally Posted by bubblegum_girl
    yes exactly the same exe file but just two of them just like bringing up two calculators for example and putting half the numbers in one calculator and the other half of the numbers in to the other
    i'm sorry but try these code:

    Code:
    Option Explicit
    
    Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    
    Private Sub Command1_Click()
        
    Shell "C:\Documents and Settings\Joaquim\Os meus documentos\Visual Basic 6.0\Cambalinho\Cambalinho.exe", vbNormalFocus
    '********************************
    SendKeys "abc", 1
    SendKeys "{TAB}", 1
    SendKeys "Your mesage here", 1
    Timer1.Enabled = True
    End Sub
    
    Private Sub Form_Load()
        Timer1.Interval = 100
    End Sub
    
    Private Sub Timer1_Timer()
         If StatusProgram("Cambalinho") = False Then
            Command1.Enabled = True
            Timer1.Enabled = False
        Else
            Command1.Enabled = False
            
        End If
    End Sub
    
    Private Function StatusProgram(strProgram As String) As Boolean
        Dim s As Long
        s = FindWindow(vbNullString, strProgram)
        If s <> 0 Then
            StatusProgram = False
        Else
            StatusProgram = True
        End If
    End Function
    i edit again these message, because i found some errors... but now is ok..
    tell me something
    Last edited by joaquim; Mar 22nd, 2008 at 02:58 PM.
    VB6 2D Sprite control

    To live is difficult, but we do it.

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    147

    Re: Loading .exe form - Brings up x2 not 1

    I'm still getting two forms - The hWnd function doesn't work because when I get through it says it equals to zero even when it shoudn't be

  20. #20
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,966

    Re: Loading .exe form - Brings up x2 not 1

    Quote Originally Posted by bubblegum_girl
    I'm still getting two forms - The hWnd function doesn't work because when I get through it says it equals to zero even when it shoudn't be
    i'm sorry... don't forget change the strings for your program...
    yes i know, but see again the last message... i put more and diferent code, try test it...
    VB6 2D Sprite control

    To live is difficult, but we do it.

  21. #21

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    147

    Re: Loading .exe form - Brings up x2 not 1

    I'm thinking that the reason it can't find the hwnd is because the name in the brackets is wrong i.e.

    hwnd = FindWindow(vbNullString, "Nokia Text Message Editor - Connected to Nokia 6300")

    or

    hwnd = FindWindow(vbNullString, "C:\Program Files\Nokia\Nokia PC Suite 6\TextMessageEditor.exe")

    I there any way of finding out what it should be? I've tried both and nether work - Just in case its something completly different

  22. #22
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,966

    Re: Loading .exe form - Brings up x2 not 1

    Quote Originally Posted by bubblegum_girl
    I'm thinking that the reason it can't find the hwnd is because the name in the brackets is wrong i.e.

    hwnd = FindWindow(vbNullString, "Nokia Text Message Editor - Connected to Nokia 6300")

    or

    hwnd = FindWindow(vbNullString, "C:\Program Files\Nokia\Nokia PC Suite 6\TextMessageEditor.exe")

    I there any way of finding out what it should be? I've tried both and nether work - Just in case its something completly different
    no.... the findwindow just see if one form with the some caption is visible or open...

    my new code is working, but sometimes is ignored because the form minize automatic... if you execute the program outside of vb, the problem is resolved(at least i think)
    VB6 2D Sprite control

    To live is difficult, but we do it.

  23. #23

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    147

    Re: Loading .exe form - Brings up x2 not 1

    I've done a step by step run through

    I've used the code and strProgram IS coming up as: "Nokia Text Message Editor - Connected to Nokia 6300" so at least that line IS correct

    BUT when the form loads the command button is disabled so I can't even press it

  24. #24
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,966

    Re: Loading .exe form - Brings up x2 not 1

    Quote Originally Posted by bubblegum_girl
    I've done a step by step run through

    I've used the code and strProgram IS coming up as: "Nokia Text Message Editor - Connected to Nokia 6300" so at least that line IS correct

    BUT when the form loads the command button is disabled so I can't even press it
    no you can't... only after you close the nokia program
    VB6 2D Sprite control

    To live is difficult, but we do it.

  25. #25

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    147

    Re: Loading .exe form - Brings up x2 not 1

    Actually scrap that comment the only reason it said the nokia bit was because it was calling the name from the other function

    I'm still messing with the code but I can't seem to get it working YET

    EDIT:
    I don't think it likes the hWnd at all no matter what I seem to try so I think I might be better going and trying to work out the code from post 16 and somehow call the function that causes the delay

    What do you think?
    Last edited by bubblegum_girl; Mar 22nd, 2008 at 03:51 PM.

  26. #26
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,966

    Re: Loading .exe form - Brings up x2 not 1

    Quote Originally Posted by bubblegum_girl
    Actually scrap that comment the only reason it said the nokia bit was because it was calling the name from the other function

    I'm still messing with the code but I can't seem to get it working YET

    EDIT:
    I don't think it likes the hWnd at all no matter what I seem to try so I think I might be better going and trying to work out the code from post 16 and somehow call the function that causes the delay

    What do you think?
    heres the project that i did for you....
    test the exe... then tell me
    PS: (in portuguese)"nova pasta" is in inglish "new folder"
    Attached Files Attached Files
    VB6 2D Sprite control

    To live is difficult, but we do it.

  27. #27

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    147

    Re: Loading .exe form - Brings up x2 not 1

    I downloaded it and ran it and it brought up the text messaging editor form twice

    Does it do that on your pc or is it just me?

    I get two forms and the lines of text wrote twice (but incomplete because they start writing before the form is fully loaded)

    If I can get the code working from post 16 on here and cause the delay that way it may work but I don't know how to call the function StillandAll
    Last edited by bubblegum_girl; Mar 22nd, 2008 at 04:27 PM.

  28. #28
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,966

    Re: Loading .exe form - Brings up x2 not 1

    Quote Originally Posted by bubblegum_girl
    I downloaded it and ran it and it brought up the text messaging editor form twice

    Does it do that on your pc or is it just me?
    well i had a car accident today, but i'm ok too ... in my pc open only one time, and do the work ok(i test with other program, but i change the code for you)....
    now i don't know what hapen... i'm sorry...
    VB6 2D Sprite control

    To live is difficult, but we do it.

  29. #29

    Thread Starter
    Addicted Member
    Join Date
    Apr 2006
    Posts
    147

    Re: Loading .exe form - Brings up x2 not 1

    omg you are going to laugh but after all that I have got it to work using my first bit of code and the line: Sleep 5000

    It then waits for the form fully loading before writing and for some reason the text being writen before hand was interfering with the code and making it load another

    It doesn't do that now - I can't believe something so simple like setting a sleep would work after all the code we tried

    Thank you sooooooooooooo much for all your help


    p.s Your as bad as me I'm walking with a limp - I fell over on Friday last week and sprained my ankle - Thanks again for all your help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width