home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource1
/
chint
/
useful06.hnt
< prev
next >
Wrap
Text File
|
1992-06-10
|
6KB
|
206 lines
/***************************************************************************
Example of how to pass parameter from one program to the next.
Includes : i) Modulr "GlobalV" defining and setting up global vars.
ii) Test1, set values and passes to Test2
iii) Test2, display the parameters pass to it by Test1
***************************************************************************/
1) The header file GLOBALV.H
/***************************************************************************
* Module - globalv.h, Global Variables Between Programs module header *
***************************************************************************/
typedef struct {
byte variable1; /* List all bits of info that you want shared */
int variable2; /* between programs. Use whatever variable */
long variable3; /* names and types you like. */
double variable4;
string variable5;
} globallyKnown; /* Type defining Inter Program Global Variable */
extern grecptr globallyKnownH; /* Pointer to global recognition header */
extern globallyKnown *globalVPtr; /* Pointer to Global Variable */
extern bool calledBy; /* Was this program called by a another */
/* that also uses this unit and passed */
/* the inter program variable address. */
void globalv_init(void);
/***************************************************************************
* END OF HEADER FILE globalv.h *
***************************************************************************/
2) The module GLOBALV.C
/**************************************************************************
* Module - globalv.c, Global Variables Between Programs source *
**************************************************************************/
#ifdef __TURBOC__
#include <conio.h>
#else
#include <graph.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "db_lsc.h"
#include "db_types.h"
#include "db_conio.h"
#include "db_dos.h"
#include "db_dmem.h"
#include "db_file.h"
#include "db_str.h"
#include "db_gvar.h"
#include "globalv.h"
uchar unitIdentifier[] = "_GLOBAL1";
grecptr globallyKnownH; /* Pointer to global recognition header */
globallyKnown *globalVPtr; /* Pointer to Global Variable */
bool calledBy; /* Was this program called by a another */
static bool initialized = False;
void initGlobals(grecptr dgv) /* Set up inter program global variables */
{
globalVPtr = (globallyKnown *) &(dgv->gdata);
if (i_alloc) {
globalVPtr->variable1 = 0;
globalVPtr->variable2 = 0;
globalVPtr->variable3 = 0;
globalVPtr->variable4 = 0.0;
strcpy(globalVPtr->variable5,"");
calledBy = False;
}
else
calledBy = True;
}
void gvar_Exit(void)
/*
If this module allocated some of memory then it should free up that memory
block as the unit closes.
*/
{
if (i_alloc)
dosfree(globalVPtr);
}
void globalv_init(void)
{
if (!initialized) {
initialized = True;
if (!(initgvar(unitIdentifier,sizeof(globallyKnown),&globallyKnownH,&i_alloc))) {
cwrite(_NoGlobalSpace);
cwrite("\n\r");
exit(0);
}
else {
initGlobals(globallyKnownH);
atexit(gvar_Exit);
}
}
}
/***************************************************************************
* END OF MODULE SOURCE globalv.c *
***************************************************************************/
3) The first test program TEST1.C
/***************************************************************************
* Test Module TEST1.C *
***************************************************************************/
#ifdef __TURBOC__
#include <conio.h>
#else
#include <graph.h>
#endif
#include <stdio.h>
#include <process.h>
#include <stdlib.h>
#include <string.h>
#include "db_lsc.h"
#include "db_types.h"
#include "db_conio.h"
#include "db_dos.h"
#include "db_dmem.h"
#include "db_file.h"
#include "db_str.h"
#include "db_gvar.h"
#include "globalv.h"
string commandLine, ts;
int i;
void main(void)
{
db_gvar_init();
globalv_init();
globalVPtr->variable1 = 1;
globalVPtr->variable2 = 2;
globalVPtr->variable3 = 3;
globalVPtr->variable4 = 4.0;
strcpy(globalVPtr->variable5,"Pass this string to the next program");
strconcat(commandLine," ",recog,pointer2string(ts,dgvar),NULL);
/* Standard inter program globals provided by the DB_GVar unit. */
strconcat(commandLine,commandLine," ",recog,pointer2string(ts,globallyKnownH),NULL);
/* Inter program globals provided by your GlobalV unit. */
i = spawnlp(P_WAIT,"TEST2.EXE","TEST2.EXE",commandLine,NULL);
}
/***************************************************************************
* END Test Module TEST1.C *
***************************************************************************/
4) The second test program TEST2.C
/***************************************************************************
* Test Module TEST2.C *
***************************************************************************/
#ifdef __TURBOC__
#include <conio.h>
#else
#include <graph.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "db_types.h"
#include "db_conio.h"
#include "db_dos.h"
#include "db_dmem.h"
#include "db_file.h"
#include "db_str.h"
#include "db_gvar.h"
#include "globalv.h"
string ts;
void main(void)
{
db_gvar_init();
globalv_init();
printf("%6i\n\r",globalVPtr->variable1);
printf("%6i\n\r",globalVPtr->variable2);
printf("%6i\n\r",globalVPtr->variable3);
printf("%6.2f\n\r",globalVPtr->variable4);
printf("%s\n\r",globalVPtr->variable5);
getch();
}