Results 1 to 17 of 17

Thread: [RESOLVED] copy text to another application

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    57

    Resolved [RESOLVED] copy text to another application

    hello guys

    i have a application ( non vb application )

    and the application has a text box and a button...



    then i have my VB6 project

    in my project i have

    text1.text
    command1

    i want when i click the command1 button copy the text1 text to ( non vb application ) applications textbox and click the button....

    can anybody help me in this..

    best regs....

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: copy text to another application

    You need to get the hWnd handle of the textbox in the other application, using FindWindows and FindWindowEx, after which you can send a WM_SETTEXT message.

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    57

    Re: copy text to another application

    Joacim Andersson... thanks for replying

    can u give me a example soruce.....

  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: copy text to another application

    Well, first you need to know the class name of the window in the other application and the class name of the textbox (which might be "Edit" but it depends on what language the other program was written in). You can use Spy++ to get these.

    This example shows how to set the text in Notepad. The class name of the Notepad main window happens to be Notepad, and the first text box (which is also the only textbox) have the class name "Edit".
    Code:
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
        ByVal lpClassName As String, _
        ByVal lpWindowName As String _
    ) As Long
    
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" ( _
        ByVal hWnd1 As Long, _
        ByVal hWnd2 As Long, _
        ByVal lpsz1 As String, _
        ByVal lpsz2 As String _
    ) As Long
    
    Private Declare Function SendMessageText Lib "user32" Alias "SendMessageA" ( _
        ByVal hWnd As Long, _
        ByVal wMsg As Long, _
        ByVal wParam As Long, _
        ByVal lParam As String _
    ) As Long
    
    Private Const WM_SETTEXT As Long = &HC
    
    Private Sub Command1_Click()
        Dim hWndParent As Long
        Dim hWndText As Long
        'Find the Notepad window
        hWndParent = FindWindow("Notepad", vbNullString)
        If hWndParent Then
            'Find the first text box in Notepad
            hWndText = FindWindowEx(hWndParent, 0, "Edit", vbNullString)
            If hWndText Then
                'set the text
                Call SendMessageText(hWndText, WM_SETTEXT, 0, Text1.Text)
            End If
        End If
    End Sub

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    57

    Re: copy text to another application

    Joacim Andersson thanks again bro

    ur code works with notepad

    bt with That application it dnt work.. i dnt knw why

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: copy text to another application

    As I mentioned, you need to get the correct class names. You can use either Spy++ for that or my own Finder tool which you can download from here. In that thread you'll also find more information about handles and sendmessage.

  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    57

    Re: copy text to another application

    Joacim bro thanks again... bt im writing correct class names then also it don't get the text

    bt i can click the command button (Non vb application )

    so im uploading that application + source

    again thanks for helpine me Joacim ...
    Attached Files Attached Files

  8. #8
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: copy text to another application

    Hmmm.... Yes... Sorry, I will not run an exe file on my computer that I have no idea where it comes from.

  9. #9

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    57

    Re: copy text to another application

    ok... Joacim Andersson... thanks for ur help... and thanks for ur kindness
    thanks

    is there anybody who can solve this problem....

  10. #10
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: copy text to another application

    Quote Originally Posted by manmad View Post
    Joacim Andersson thanks again bro

    ur code works with notepad
    bt with That application it dnt work.. i dnt knw why
    That's because Notepad is a MS Windows application. None MS applications may or may not work.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  11. #11
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: copy text to another application

    Quote Originally Posted by jmsrickland View Post
    That's because Notepad is a MS Windows application. None MS applications may or may not work.
    It has nothing to do with MS per se, what it has to do with is if the textbox is based on the regular Windows edit box or not. I have not runned the attached exe but I did look at the source code manmad used and according to the class name he used it looks like the other application is a .Net app in which case sending WM_SETTEXT should work.

  12. #12
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: copy text to another application

    It has to do with MS per se because all MS applications are designed to 'talk' (or communicate) with one another via API's whereas an application that was not made by MS may or may not be able to 'talk' with other applications. Anyway that is what I was told and read. So if that is not so then I was told wrong and I read wrong.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: copy text to another application

    That is not true I'm afraid, and there is a huge amount of documentation (particularly on MSDN) to disprove it.

    It isn't about who made the apps, it is about what types of controls they use - because the messages (and functionality) are specific to the control type rather than the application. If a different type of control is used, you will probably need to use different messages (and may not be able to do the same things).

    Several types of controls (such as textboxes, listboxes, and scrollbars) are used by many applications simply because they are readily available - languages like VB (and even non MS ones, probably including Delphi) use them in many cases, simply because they don't need to be re-created.


    If you use a tool like Spy++ you can find the Class of the control you want to work with, and can then find the documentation for that class to see what messages to use with it.

  14. #14
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: copy text to another application

    Quote Originally Posted by si_the_geek View Post
    If you use a tool like Spy++ you can find the Class of the control you want to work with, and can then find the documentation for that class to see what messages to use with it.
    Well, you can't just go on the class name since that can easily be changed when you create the window (it's one of the parameters you pass to CreateWindowEx). For example a TextBox in VB6 has the class name ThunderTextBox (if you run the application from the IDE) or ThunderRT6TextBox (if you run a compiled application). But it still just an implementation of the regular Edit window which means that you can send any of the EM_xxx messages to it (depending a little bit if its a multiline textbox or not). It also response to many of the WM_xxx messages, such as WM_SETTEXT.

    In Delphi the class name for a single line textbox is TEdit and for a multiline textbox its called TMemo, but they are still based on the Edit window and as such you may send them the messages that an Edit window expects.

    However if you would create a new textbox from scratch and not base it at all on the built in Windows standard control it will of course then not accept any of the standard messages unless you build in support for them. But building a textbox from scratch is a lot of work and why would you want to do that instead of basing it on something that is already there?

  15. #15
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: copy text to another application

    I took a chance and ran the EXE file manmad attached and I then noticed that he used the wrong class name and also the calls where slightly wrong. Also the call to FindWindowsEx finds the first window with the specified class name and/or text. Now that might not be the one you expect. On that particular Form there are two frames (or group boxes) which both contains two textboxes each. One editable textbox and one that is marked as read-only (however even though you can't type in it directly it still accepts a WM_SETTEXT message and changes its text.

    The Frames both have the same class name, in fact they have the same class names as the window itself has. So to find one of the edit boxes you must first call FindWindow to find the actual Form window. Then call FindWindowEx to find the first frame, now the "first" frame that is returned is actually the one placed on the right hand side of the Form which you might expect to be the second frame. A new FindWindowEx call let you find one of the textboxes inside this frame.

    Now another "funny" thing, the first TextBox in the frame to the right is the top-most textbox, but in the other frame the first textbox is the textbox on the bottom (the read-only one).

    So it all depends on which textbox you want to change the text for. But I assume you want the top-left-most textbox in which case this is the code (please note that I pass in the text as well as the class name to find the "frame" window).
    vb Code:
    1. Dim window As Long
    2. Dim frame As Long
    3. Dim textbox As Long
    4.  
    5. window = FindWindow("windowsforms10.Window.8.app.0.378734a", vbNullString)
    6. 'note that I pass the text for the frame here as well as the class name
    7. frame = FindWindowEx(window, 0&, "windowsforms10.Window.8.app.0.378734a", "PPC")
    8. textbox = FindWindowEx(frame, 0&, "WindowsForms10.EDIT.app.0.378734a", vbNullString)
    9. 'now we have the first textbox in this frame, but that is actually the bottom text box
    10. 'so we need to make a second call
    11. textbox = FindWindowEx(frame, textbox, "WindowsForms10.EDIT.app.0.378734a", vbNullString)
    12. Call SendMessageByString(textbox, WM_SETTEXT, 0&, "Some new text")
    This works just fine, even though I doubt very much that the KeyGen application was written by Microsoft.
    Last edited by Joacim Andersson; Mar 24th, 2009 at 12:25 PM.

  16. #16
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: copy text to another application

    Quote Originally Posted by Joacim Andersson View Post
    Well, you can't just go on the class name since that can easily be changed when you create the window...
    Very true, but in the vast majority of cases the class name will at least give you a good starting point to find the relevant info - even if it is just to send you back to the standard control types.

  17. #17

    Thread Starter
    Member
    Join Date
    Jul 2008
    Posts
    57

    Re: copy text to another application

    Joacim Andersson bro.. thanks again for sloving the Problem For me... n thanks u too si_the_geek... keep helping.. like that... have a grt day Bros

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