FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour passing parameters between 2 exe
Posts: 946
Joined: Thu Oct 06, 2005 07:05 PM
passing parameters between 2 exe
Posted: Thu Apr 25, 2013 07:52 PM

Hello

How can we pass a parameter between 2 exe and how the second will retreive it ?

i know shellexecute(....) can pass parameter

in program1.exe shellexecute(,"open""program2.exe",param)

is there another way to do it and how to retreive the param in program2

Thanks for help,

Richard

http://www.cbati.com

Uestudio
Fwh 13.05 Harbour 3.2 MSVC 2013
Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: passing parameters between 2 exe
Posted: Thu Apr 25, 2013 08:02 PM

You can use a file to pass and receive parameters.

EMG

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: passing parameters between 2 exe
Posted: Thu Apr 25, 2013 08:03 PM
Richard Chidiak wrote:how to retreive the param in program2


Code (fw): Select all Collapse
FUNCTION MAIN( cParam1, cParam2, etc. )


EMG
Posts: 946
Joined: Thu Oct 06, 2005 07:05 PM
Re: passing parameters between 2 exe
Posted: Thu Apr 25, 2013 08:05 PM

Exactly what i am looking for

Thank you Enrico

Richard

http://www.cbati.com

Uestudio
Fwh 13.05 Harbour 3.2 MSVC 2013
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: passing parameters between 2 exe
Posted: Thu Apr 25, 2013 08:37 PM
According to the Windows API this is the way to do it (of course other systems work fine as well). This way is to send data to an already running app:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms649011(v=vs.85).aspx

A working example:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms649009(v=vs.85).aspx
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 392
Joined: Tue Jul 29, 2008 01:55 PM
Re: passing parameters between 2 exe
Posted: Fri Apr 26, 2013 01:06 AM

Hi Antonio.

For those who do not know C + +

It is possible to have an example of this code to operate in xHarbour and Harbour?

regards

Visite Chiapas, el paraiso de México.
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: passing parameters between 2 exe
Posted: Fri Apr 26, 2013 03:42 AM
Here are two sample programs.
wndsend.prg
wndget.prg

Code (fw): Select all Collapse
// wndsend.prg

#include "fivewin.ch"
#include "xbrowse.ch"

REQUEST DBFCDX

//----------------------------------------------------------------------------//

function Main()

   local oWnd, oBar, oBrw

   USE CUSTOMER NEW VIA "DBFCDX"

   DEFINE WINDOW oWnd TITLE "SendWnd"
   DEFINE BUTTONBAR oBar OF oWnd SIZE 100,32 2007
   DEFINE BUTTON OF oBar PROMPT "SendMsg" ACTION SendData()
   SET MESSAGE OF oWnd TO '' 2007

   @ 0,0 XBROWSE oBrw OF oWnd ALIAS "CUSTOMER" ;
      AUTOCOLS CELL LINES NOBORDER

   oBrw:CreateFromCode()
   oWnd:oClient   := oBrw

   ACTIVATE WINDOW oWnd
   CLOSE DATA

return 0

//----------------------------------------------------------------------------//

static function SendData()

   local hDstWnd  := FindWindow( "TCDWnd", "GetWnd" )

   if Empty( hDstWnd )
      ? "window not found"
   else
      SendDataToApp( hDstWnd, 1, ;
         FW_ValToExp( { CUSTOMER->FIRST, CUSTOMER->LAST, CUSTOMER->CITY } ), ;
         WndMain():hWnd )
   endif

return nil

//----------------------------------------------------------------------------//

//----------------------------------------------------------------------------//

#pragma BEGINDUMP

#include <windows.h>
#include <hbapi.h>

HB_FUNC (SENDDATATOAPP) // ( hDestWnd, nMsgID, cData, hFromWnd )
{
   COPYDATASTRUCT cds;

   cds.dwData     = hb_parnl( 2 );
   cds.cbData     = hb_parclen( 3 );
   cds.lpData     = (PVOID) hb_parcx( 3 );

   SendMessage( (HWND) hb_parnl( 1 ), WM_COPYDATA, (WPARAM) (HWND) hb_parnl( 4 ), (LPARAM) (LPVOID) &cds );
   hb_ret();
}

HB_FUNC (PARSECOPYDATA)
{
   PCOPYDATASTRUCT pCDS;

   pCDS           = (PCOPYDATASTRUCT) hb_parnl( 1 );
   hb_stornl( pCDS->dwData, 2 );
   hb_retclen( pCDS->lpData, pCDS->cbData );

}

#pragma ENDDUMP

//============================================================================//


wndget.prg:
Code (fw): Select all Collapse
// wndget.prg

#include "fivewin.ch"
#include "xbrowse.ch"

REQUEST DBFCDX

//----------------------------------------------------------------------------//

function Main()

   local oWnd, oBar, oBrw

   oWnd  := TCDWnd():New( nil, nil, nil, nil, "GetWnd" )
   DEFINE BUTTONBAR oBar OF oWnd SIZE 100,32 2007
   SET MESSAGE OF oWnd TO ''

   oWnd:bOnGetData   := { |oWnd, oFrom, nID, cData| ;
      OnGetData( oWnd, oFrom, nID, cData, oBrw ) }

   @ 0,0 XBROWSE oBrw OF oWnd DATASOURCE {} ;
      COLUMNS 1, 2, 3 ;
      HEADERS "First", "Last", "City" ;
      COLSIZES 80, 80, 80 ;
      CELL LINES NOBORDER

   oBrw:CreateFromCode()
   oWnd:oClient   := oBrw

   ACTIVATE WINDOW oWnd
   CLOSE DATA

return 0

//----------------------------------------------------------------------------//

static function OnGetData( oWnd, oFrom, nMsgID, cData, oBrw )

   AAdd( oBrw:aArrayData, &cData )
   oBrw:GoBottom()
   oBrw:Refresh()
   oBrw:SetFocus()

return nil

//----------------------------------------------------------------------------//

//============================================================================//

#define WM_COPYDATA                     0x004A

CLASS TCDWnd FROM TWindow STATIC

   DATA bOnGetData

   CLASSDATA lRegistered AS LOGICAL

   METHOD HandleEvent( nMsg, nWParam, nLParam )

ENDCLASS

//----------------------------------------------------------------------------//

METHOD HandleEvent( nMsg, nWParam, nLParam ) CLASS TCDWnd

   local nDataID := 0, cData

   if nMsg == WM_COPYDATA
      if ::bOnGetData != nil
         cData    := ParseCopyData( nLParam, @nDataID )
         Eval( ::bOnGetData, Self, nWParam, nDataID, cData )
         return 0
      endif
   endif

return ::Super:HandleEvent( nMsg, nWParam, nLParam )

//----------------------------------------------------------------------------//

//----------------------------------------------------------------------------//

#pragma BEGINDUMP

#include <windows.h>
#include <hbapi.h>

HB_FUNC (SENDDATATOAPP) // ( hDestWnd, nMsgID, cData, hFromWnd )
{
   COPYDATASTRUCT cds;

   cds.dwData     = hb_parnl( 2 );
   cds.cbData     = hb_parclen( 3 );
   cds.lpData     = (PVOID) hb_parcx( 3 );

   SendMessage( (HWND) hb_parnl( 1 ), WM_COPYDATA, (WPARAM) (HWND) hb_parnl( 4 ), (LPARAM) (LPVOID) &cds );
   hb_ret();
}

HB_FUNC (PARSECOPYDATA)
{
   PCOPYDATASTRUCT pCDS;

   pCDS           = (PCOPYDATASTRUCT) hb_parnl( 1 );
   hb_stornl( pCDS->dwData, 2 );
   hb_retclen( pCDS->lpData, pCDS->cbData );

}

#pragma ENDDUMP

//============================================================================//


Independently compile and build the two exes wndsend.exe and wndget.exe, either in Harbour or xHarbour.

wndsend.prg opens and shows browse of customer.dbf in fwh\samples folder. You may need the exe files located in fwh\samples folder or have the customer.dbf in the same folder where the wndsend.exe resides.

Launch both programs and move the windows so that you can see both windows without overlapping each other.

wndget.exe shows browse of an empty array with three columns.

wndsend.exe shows browse of customer.dbf. Move to any row of the browse and click "SendMsg" button on the buttonbar.

Now this program sends an array of CUSTOMER->FIRST, CUSTOMER->LAST, CUSTOMER-CITY to WndGet.Exe through WM_COPYDATA message.
WndGet.exe responds to WM_COPYDATA message and retrieves the data, appends to the array and displays the data in the browse.

Keep moving to different rows in the customer browse of WndSend.Exe and press "SendMsg" button. The information of the row is received by WndGet.Exe and shown in its array browse.

Notes:
I have used SendMessage() function to send the messages. Those who are aware the difference between SendMessage() and PostMessage() may be tempted to use PostMessage(), but that does not work here.

The disadvantage of SendMessage() is that the sending program waits till the responding application completes processing the message ( or ignores ). In the case of our example, till the HandleEvent method returns 0.

Therefore the responding program should retrieve and store the message and signal handling of the event fast.
Regards



G. N. Rao.

Hyderabad, India
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: passing parameters between 2 exe
Posted: Fri Apr 26, 2013 07:36 AM

Mr Rao
Thanks for such a detailed explanation
Regards

Cristobal Navarro

Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo

El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
Posts: 392
Joined: Tue Jul 29, 2008 01:55 PM
Re: passing parameters between 2 exe
Posted: Fri Apr 26, 2013 07:30 PM

Hello nages

Thanks for the example

I'll try

Thanks again

Visite Chiapas, el paraiso de México.

Continue the discussion