Results 1 to 7 of 7

Thread: Who can solve this problem?

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Location
    Sunnyvale CA
    Posts
    15

    Who can solve this problem?

    Here's the situation. I have an old program in VB6 which I intend to keep as it is. Upgrading it to .NET would be just too troublesome, so that's not an option. What I'd like to accomplish is to develop a program in VB.NET that will allow me to read strings from the old program. For example, I'd like the old VB6 program to send its version (in string type) to the VB.NET program, which in trun will show the version on the VB.NET form (or just store it in the memory).

    Is there any way of doing this? I know how to do this between two VB6 programs but I just can't figure it out for .NET. Does anyone here know anything about this? Any reference to a site or examples that I can follow would be just great!

    Thanks in advance!
    Eric.
    What is finite, but boundaryless?

  2. #2
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    The easiest way would be to just create a txt file with the old app and print it's version in it using App.Major & "." & App.Minor.
    Then just read from the text file with your VB .net version.

    The Harder way would be to use API. Namely SendMessagebystring, SendMessagelong,FindWindow,FindWindowex to find your VB .net project and send the string to it.
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Location
    Sunnyvale CA
    Posts
    15
    Originally posted by Arc
    The Harder way would be to use API. Namely SendMessagebystring, SendMessagelong,FindWindow,FindWindowex to find your VB .net project and send the string to it.
    Well, how do I exactly achieve this? Do you have any examples for it?

    I was thining of using TCP to send/receive within localhost since both will be run on the same machine.

    Thanks.
    What is finite, but boundaryless?

  4. #4
    PowerPoster Arc's Avatar
    Join Date
    Sep 2000
    Location
    Under my rock
    Posts
    2,336
    This example i made uses Notepad. But it's fairly simple to change it to any program. You just need an API spy program to the title of the program(which is usualy what the title bar says).

    All you need is 3 buttons and a textbox

    Put this in a module

    VB Code:
    1. 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
    2. Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    3. Public 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
    4. Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
    5. Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Long) As Long
    6. Declare Function SendMessageByNum& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long)
    7. Public Const WM_SETTEXT = &HC
    8. Public Const WM_CLOSE = &H10
    9. Public Const WM_GETTEXT = &HD
    10. Public Const WM_GETTEXTLENGTH = &HE
    11. Public Const GW_HWNDNEXT = 2
    12. Function GetText(Handle)
    13. GetTrim = SendMessageByNum(Handle, 14, 0&, 0&)
    14. TrimSpace$ = Space$(GetTrim)
    15. GetString = SendMessageByString(Handle, 13, GetTrim + 1, TrimSpace$)
    16. GetText = TrimSpace$
    17. End Function

    This goes in 3 different buttons.. 1 button sends text to Notepad, one gets text from Notepad and one closes Notepad.

    VB Code:
    1. Private Sub Close_Click()
    2. Dim notepad As Long, editx As Long
    3. notepad = FindWindow("notepad", vbNullString)
    4. editx = FindWindowEx(notepad, 0&, "edit", vbNullString)
    5. Call PostMessage(notepad, WM_CLOSE, 0, 0)
    6. End Sub
    7.  
    8. Private Sub Gettxt_Click()
    9. Dim notepad As Long, editx As Long
    10. notepad = FindWindow("notepad", vbNullString)
    11. editx = FindWindowEx(notepad, 0&, "edit", vbNullString)
    12. TheText = GetText(editx)
    13. Text1 = TheText
    14. End Sub
    15.  
    16. Private Sub SetTxt_Click()
    17. Dim notepad As Long, editx As Long
    18. notepad = FindWindow("notepad", vbNullString)
    19. editx = FindWindowEx(notepad, 0&, "edit", vbNullString)
    20. Call SendMessageByString(editx, WM_SETTEXT, 0, Text1)
    21. End Sub

    Just type text into your textbox and click the Settxt button and it will put the text into notepad etc...
    -We have enough youth. How about a fountain of "Smart"?
    -If you can read this, thank a teacher....and since it's in English, thank a soldier.


  5. #5
    Junior Member
    Join Date
    Feb 2003
    Location
    UK
    Posts
    23
    A lot depends on your exact needs. If you just want a few properties then there's an easy solution. If you want the two apps to be continuously talking to each other, there are other more complicated solutions.

    For the example of version info you could put a new (multiuse) class in your old VB6 project. Have this class initialise with the version information you're interested in.

    Get the .net application to create an instance of this class and read the properties.

    Gets round an awful lot of API messing around.

    If there's more info, then just put more properties on the class.

    If it's a bit more dynamic than that, you could have the class raise events.

    There's also the possibility of callbacks. Define an interface class in your VB6 project and get it to make calls through this interface. Have a .net class implement this interface and get it to pass a reference to your VB app.

    Dave
    Last edited by daughey; Apr 1st, 2003 at 02:30 AM.

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Location
    Sunnyvale CA
    Posts
    15

    Solved

    Actually, I used .NET's Remoting service to find the solution. I made a COM class for the vb6 application to access, in which an object is created virtually (referenced from the server). From the object, I made a simple call to change the variable's value, that sits on the server side. And voila, it's done.

    Much thanks to everyone who tried to help me. I really appreciated your help!

    You all are the best!
    Eric.
    What is finite, but boundaryless?

  7. #7
    New Member
    Join Date
    Sep 2007
    Posts
    1

    Post Re: Who can solve this problem?

    Sorry to resurrect such an ancient post, but I'm having trouble figuring out how to do this exact thing, but in VB.net (VS2005 specifically) instead of VB6. That is, I need to know how to put text into notepad and get text back from it and also how to close it would be nice as well. I already know how to launch it, I mostly just need to know how to control its text a bit.

    If anyone can help, I would be most appreciative
    Thanks in advance.

    Quote Originally Posted by Arc
    This example i made uses Notepad. But it's fairly simple to change it to any program. You just need an API spy program to the title of the program(which is usualy what the title bar says).

    All you need is 3 buttons and a textbox

    Put this in a module

    VB Code:
    1. 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
    2. Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    3. Public 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
    4. Declare Function SendMessageByString Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
    5. Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, ByVal lParam As Long) As Long
    6. Declare Function SendMessageByNum& Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long)
    7. Public Const WM_SETTEXT = &HC
    8. Public Const WM_CLOSE = &H10
    9. Public Const WM_GETTEXT = &HD
    10. Public Const WM_GETTEXTLENGTH = &HE
    11. Public Const GW_HWNDNEXT = 2
    12. Function GetText(Handle)
    13. GetTrim = SendMessageByNum(Handle, 14, 0&, 0&)
    14. TrimSpace$ = Space$(GetTrim)
    15. GetString = SendMessageByString(Handle, 13, GetTrim + 1, TrimSpace$)
    16. GetText = TrimSpace$
    17. End Function

    This goes in 3 different buttons.. 1 button sends text to Notepad, one gets text from Notepad and one closes Notepad.

    VB Code:
    1. Private Sub Close_Click()
    2. Dim notepad As Long, editx As Long
    3. notepad = FindWindow("notepad", vbNullString)
    4. editx = FindWindowEx(notepad, 0&, "edit", vbNullString)
    5. Call PostMessage(notepad, WM_CLOSE, 0, 0)
    6. End Sub
    7.  
    8. Private Sub Gettxt_Click()
    9. Dim notepad As Long, editx As Long
    10. notepad = FindWindow("notepad", vbNullString)
    11. editx = FindWindowEx(notepad, 0&, "edit", vbNullString)
    12. TheText = GetText(editx)
    13. Text1 = TheText
    14. End Sub
    15.  
    16. Private Sub SetTxt_Click()
    17. Dim notepad As Long, editx As Long
    18. notepad = FindWindow("notepad", vbNullString)
    19. editx = FindWindowEx(notepad, 0&, "edit", vbNullString)
    20. Call SendMessageByString(editx, WM_SETTEXT, 0, Text1)
    21. End Sub

    Just type text into your textbox and click the Settxt button and it will put the text into notepad etc...

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