Results 1 to 9 of 9

Thread: Passing Args Between Two Exe ?

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    46

    Passing Args Between Two Exe ?

    ( LIKE ) COMMAND LINE ARGUMENT ... HOW?

    NOT COMMAND LINE ARGUMENTS
    Last edited by vb cub; Sep 20th, 2002 at 06:26 AM.

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    well command line args only get passed when a program starts.. so if you were going to shell an app from another one.. you could pass arguments that way...

    when your app starts up that is going to revieve arguments.. use the command$() function to get the passed in parameters.. you then have to parse them... thats why you notice programs that take command line parameters are usually like /s or -r

    using a character like / or - makes it easy to parse...

    there are other ways.. like using a database to share data.. or a text file..


    or check out this link
    http://www.thescarms.com/vbasic/PassString.asp

  3. #3
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    The Command$ variable in a VB program always has the command line parameters that were passed to the program.

    Eg

    If you call the program with

    RunThis.EXE A B C

    command$ will contain "A B C"

    So.. if you want to pass information from one program to another in this way you can SHELL the second program from the first program..

    eg; in program 1

    Shell "Program2.EXE A B C"

    in program 2

    If Command$="A B C" then
    ..do something
    end if
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    46
    LIKE CHAT PROGRAM THE TWO PROGRAM HAVE TO PASS VALUES
    -WHEN EVER IT NEEDS

  5. #5
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    Originally posted by vb cub
    LIKE CHAT PROGRAM THE TWO PROGRAM HAVE TO PASS VALUES
    -WHEN EVER IT NEEDS
    now your talking about the winsock control...

    http://216.26.168.92/vbworld/article.aspx?tag=winsock

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    46
    tHANK YOU

  7. #7

    Thread Starter
    Member
    Join Date
    Sep 2002
    Posts
    46
    Originally posted by kleinma
    well command line args only get passed when a program starts.. so if you were going to shell an app from another one.. you could pass arguments that way...

    when your app starts up that is going to revieve arguments.. use the command$() function to get the passed in parameters.. you then have to parse them... thats why you notice programs that take command line parameters are usually like /s or -r

    using a character like / or - makes it easy to parse...

    there are other ways.. like using a database to share data.. or a text file..


    or check out this link
    http://www.thescarms.com/vbasic/PassString.asp

    THANKS

  8. #8
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    This piece of Interprocess Communication was stolen from somewhere - probably from this forum, or from MS or MSDN.....

    1. Create a file called frmReceive.frm
    2. In Notepad paste the following into that file
    Code:
    VERSION 5.00
    Begin VB.Form frmReceive 
       BorderStyle     =   1  
       Caption         =   "Receive String"
       ClientHeight    =   1590
       ClientLeft      =   45
       ClientTop       =   330
       ClientWidth     =   3210
       LinkTopic       =   "Form1"
       LockControls    =   -1  'True
       MaxButton       =   0   'False
       ScaleHeight     =   1590
       ScaleWidth      =   3210
       Begin VB.CommandButton cmdQuit 
          Cancel          =   -1  'True
          Caption         =   "&Quit"
          Height          =   375
          Left            =   1943
          TabIndex        =   2
          Top             =   986
          Width           =   975
       End
       Begin VB.Label lblString 
          BackColor       =   &H00FFFFFF&
          BorderStyle     =   1  'Fest Einfach
          Height          =   285
          Left            =   263
          TabIndex        =   1
          Top             =   469
          Width           =   2685
       End
       Begin VB.Label Label1 
          Caption         =   "Here is the string that was sent:"
          Height          =   255
          Left            =   263
          TabIndex        =   0
          Top             =   229
          Width           =   2415
       End
    End
    Attribute VB_Name = "frmReceive"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    Option Explicit
    
    Private Sub cmdQuit_Click()
        Unload Me
    End Sub
    
    Private Sub Form_Load()
        ' Get this form's handle.
        ' Subclass this form to trap Windows messages.
        g_lnghWnd = Me.hwnd
        Call Hook
        Me.Left = Screen.Width / 5 * 4 - Me.Width
        Me.Top = Screen.Height / 5 * 4 - Me.Height
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        ' Un-subclass the form.
        Call Unhook
        Unload Me
        Set frmReceive = Nothing
    End Sub
    3. In VB create a project called ReceiveString
    4. Add frmReceive
    5. Add the following code in a module
    VB Code:
    1. Option Explicit
    2. Public g_lngPrevWndProc As Long
    3. Public g_lnghWnd As Long
    4. Public Const GWL_WNDPROC = (-4)
    5. Public Const WM_COPYDATA = &H4A
    6. Public Type COPYDATASTRUCT
    7.     dwData As Long
    8.     cbData As Long
    9.     lpData As Long
    10. End Type
    11. ' Copies a block of memory from one location to another.
    12. Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
    13. Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    14.  
    15. Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    16.  
    17. '
    18. ' Sub class the form to trap for Windows messages.
    19. '
    20. Public Sub Hook()
    21.     g_lngPrevWndProc = SetWindowLong(g_lnghWnd, GWL_WNDPROC, AddressOf WindowProc)
    22.     Debug.Print g_lngPrevWndProc
    23. End Sub
    24. Public Sub ReceiveMsg(lngParam As Long)
    25.     Dim strString As String
    26.     Dim cds As COPYDATASTRUCT
    27.     Dim bytBuffer(1 To 255) As Byte
    28.    ' Copy the data sent to this application into a local structure.
    29.     Call CopyMemory(cds, ByVal lngParam, Len(cds))
    30.    ' Copy the string that was passed into a byte array.
    31.     Call CopyMemory(bytBuffer(1), ByVal cds.lpData, cds.cbData)
    32.    ' Convert the ASCII byte array back to a Unicode string.
    33.     strString = StrConv(bytBuffer, vbUnicode)
    34.     strString = Left$(strString, InStr(1, strString, Chr$(0)) - 1)
    35.    ' Display the received string.
    36.     frmReceive.lblString = strString
    37. End Sub
    38. '
    39. ' Remove the subclassing.
    40. '
    41. Public Sub Unhook()
    42.     Call SetWindowLong(g_lnghWnd, GWL_WNDPROC, g_lngPrevWndProc)
    43. End Sub
    44.  
    45. Public Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    46.    ' This callback routine is called by Windows whenever a message is sent to this form. If it is
    47.    ' the copy message call our procedure to receive the message.
    48.     If uMsg = WM_COPYDATA Then Call ReceiveMsg(lParam)
    49.     ' Call the original window procedure associated with this form.
    50.     WindowProc = CallWindowProc(g_lngPrevWndProc, hw, uMsg, wParam, lParam)
    51. End Function
    6. Compile into an exe
    7. Create a file called frmSend.frm
    8. In Notepad paste the following into that file
    Code:
    VERSION 5.00
    Begin VB.Form frmSend 
       BorderStyle     =   1 
       Caption         =   "Send String"
       ClientHeight    =   1545
       ClientLeft      =   45
       ClientTop       =   330
       ClientWidth     =   3180
       LinkTopic       =   "Form1"
       LockControls    =   -1  'True
       MaxButton       =   0   'False
       ScaleHeight     =   1545
       ScaleWidth      =   3180
       Begin VB.CommandButton cmdQuit 
          Cancel          =   -1  'True
          Caption         =   "&Quit"
          Height          =   375
          Left            =   1943
          TabIndex        =   3
          Top             =   945
          Width           =   975
       End
       Begin VB.TextBox txtString 
          Height          =   285
          Left            =   263
          TabIndex        =   1
          Top             =   465
          Width           =   2655
       End
       Begin VB.CommandButton cmdSend 
          Caption         =   "&Send String"
          Height          =   375
          Left            =   263
          TabIndex        =   0
          Top             =   945
          Width           =   1095
       End
       Begin VB.Label Label1 
          Caption         =   "Enter the string to send:"
          Height          =   255
          Left            =   263
          TabIndex        =   2
          Top             =   225
          Width           =   2175
       End
    End
    Attribute VB_Name = "frmSend"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    
    ttribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    Option Explicit
    
    Private Const WM_COPYDATA = &H4A
    
    Private Type COPYDATASTRUCT
        dwData As Long
        cbData As Long
        lpData As Long
    End Type
    
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    
    ' Copies a block of memory from one location to another.
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
    
    Private Sub cmdQuit_Click()
        Unload Me
    End Sub
    
    Private Sub cmdSend_Click()
        Dim strString As String
        Dim lnghWnd As Long
        Dim cds As COPYDATASTRUCT
        Dim bytBuffer(1 To 255) As Byte
        
        strString = Trim$(txtString.Text)
        If strString = "" Then Exit Sub
        
        ' Get the handle of the target application.
        lnghWnd = FindWindow(vbNullString, "Receive String")
        
        ' Copy the string into a byte array, converting it to ASCII. Assign lpData the address of the
        ' byte array.
        Call CopyMemory(bytBuffer(1), ByVal strString, Len(strString))
        With cds
            .dwData = 3
            .cbData = Len(strString) + 1
            .lpData = VarPtr(bytBuffer(1))
        End With
        
        ' Send the string.
        Call SendMessage(lnghWnd, WM_COPYDATA, Me.hwnd, cds)
    End Sub
    
    Private Sub Form_Load()
        Me.Left = Screen.Width / 5
        Me.Top = Screen.Height / 5
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        Unload Me
        Set frmSend = Nothing
    End Sub
    9. Add this form to a new VB project called SendString and compile that
    10. Run them both and Bob's your uncle

  9. #9
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    if he wants a chat program then winsock is def the way to go

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