Using API JDB_InitEnvOvr and JDB_InitUser correctly

johndanter

johndanter

Legendary Poster
Hi folks,

A while ago I enquired about running a UBE under a different USER ID (huser2 below) and we all seemed to crack it by using these 2 cracking APIs

However I came across a few problems and I think I may have fixed them at the time but I'm just revisiting them now.
When you open a connection, you have to close the connection, right?
I think you do that with the JDB_FreeUser JDB_FreeEnv etc

Can someone cleverer than I am take a look at my code here and let me know if I am opening and closing things correctly please?

Thanks

John

The code
#include <jde.h>

#define b570001_c


/*****************************************************************************
* Source File: b570001
*
* Description: GTS EDI Interoperability, Launch UBE Specific User Source File

* Notes:
*
/**************************************************************************
* Notes:
* Purpose
* -------
* This function is designed to launch the Interoperability Vendor-Specific
UBE specified in the Input Parameter's UBE Name and Version under a specific USER ID.
It will also pass 6 additional parameters to the Vendor-Specific UBE, namely EDI
Key Co(EKCO), EDI Doc Number(EDOC), EDI Doc Type (EDCT), Export
Mode Flat File(FFEM), Export Mode Database(EDEM) and Export Mode API(EAEM).
After the Vendor-Specific UBE returns, it will copy the UBE Error Code
parameter from the Vendor-Specific UBE's report interconnect data structure
to the Output Error Code parameter.

The main aim of this BSFN is to open a user session for the user/password/environment passed in.
All WorkCentre messages for the UBE to be called will then fall into a WC of choice

*
* Special Logic
* -------------
* 1. All required fields must be passed in.
* 2. The Vendor-Specific UBE called by this business function must
have the following Report Interconnect data structure.
The order of the parameters in the UBE data structure must
be maintained as follows:
3. * EDI Key Co(EKCO) (INPUT)
4. * EDI Doc Number (EDOC) (INPUT)
5. * EDI Doc Type (EDTY) (INPUT)
6. * Export Mode Flat File (FFEM) (INPUT)
7. * Export Mode Database (EDEM) (INPUT)
8. * Export Mode API (EAEM) (INPUT)
9. * UBE Error Code (ERRC) (OUTPUT)
**************************************************************************/

#include <b570001.h>


/**************************************************************************
* Business Function: GTSEDIInteropLaunchSpecificUSER
*
* Description: GTS EDI Interoperability, Launch UBE Specific User
*
* Parameters:
* LPBHVRCOM lpBhvrCom Business Function Communications
* LPVOID lpVoid Void Parameter - DO NOT USE!
* LPDSD570 lpDS Parameter Data Structure Pointer
*
*************************************************************************/

JDEBFRTN (ID) JDEBFWINAPI GTSEDIInteropLaunchSpecificUSER (LPBHVRCOM lpBhvrCom, LPVOID lpVoid, LPDSD570001 lpDS)

{
/************************************************************************
* Variable declarations
************************************************************************/
ID idReturnCode = ER_SUCCESS;
ID idJDBReturn = JDEDB_PASSED;
/************************************************************************
* Declare structures
************************************************************************/

DSRI570001UBEDS dsReportInterconnect= { 0 };
struct tagUBEVAR dsUbeStructure = { 0 };


/************************************************************************
* Declare pointers
************************************************************************/

HUSER hUser = (HUSER)NULL;
PUBEVAR pUBEVar = (PUBEVAR)NULL;
HENV hEnv = NULL;
HUSER hUser2 = NULL;
LPBHVRCOM lpBhvrCom2 = NULL;
LPVOID lpVoid2 = NULL;
/*
JCHAR szEnv[11] = {0};
JCHAR szUser[11] = {0};
JCHAR szPwd[30] = {0};

PJSTR pEnv = NULL;
*/

/************************************************************************
* Check for NULL pointers
************************************************************************/
if ((lpBhvrCom == (LPBHVRCOM) NULL) ||
(lpVoid == (LPVOID) NULL) ||
(lpDS == (LPDSD570001) NULL))
{
jdeErrorSet (lpBhvrCom, lpVoid, (ID) 0, _J("4363"), (LPVOID) NULL);
return ER_ERROR;
}
/* Initialize Behaviour DS */
idJDBReturn = JDB_InitBhvr(lpBhvrCom, &hUser,
(JCHAR *) NULL, JDEDB_COMMIT_AUTO);

if (idJDBReturn != JDEDB_PASSED)
{
jdeErrorSet (lpBhvrCom, lpVoid, (ID) 0, _J("4363"), (LPVOID) NULL);

lpDS->cErrorCode = _J('1');
return ER_ERROR;
}
/************************************************************************
* Set pointers
************************************************************************/

/************************************************************************
* Main Processing
************************************************************************/

/* Spawn new user session using API JDB_InitEnvOvr
JDB_InitEnvOvr() initializes a JDB environment, usually on the servers.
This API passes the environment name, user name, and password for signing on to the environment.
In addition, without an initialized environment, no JDB API will work.
The environment handle initialized here is needed for the call to JDB_InitUser.
*/



if (JDB_InitEnvOvr( &hEnv, lpDS->szEnvironment, lpDS->szScheduledUserID, lpDS->szScheduledPassword) != JDEDB_PASSED)
{
JDB_FreeBhvr(hUser);
return (ER_ERROR);
}

if (JDB_InitUser(hEnv, &hUser2, lpDS->szScheduledUserID, JDEDB_COMMIT_AUTO) != JDEDB_PASSED)
{
JDB_FreeBhvr(hUser);
return (ER_ERROR);
}

jdeCreateBusinessFunctionParms(hUser2, &lpBhvrCom2, &lpVoid2);


/* Initialize Output Variables */
lpDS->cErrorCode = _J('0');

/* Set up PUBEVar data structure */
dsUbeStructure.bPreview = FALSE;
jdeStrcpy((JCHAR *)dsUbeStructure.szReport,(const JCHAR *)lpDS->szVendorSpecificUBEName);
jdeStrcpy((JCHAR *)dsUbeStructure.szVersion, (const JCHAR *)lpDS->szVersionToCall);
if((jdeGetHostName(dsUbeStructure.szMachineKey, 16, 0) == 1) ||
(!GetLocalEnvironmentName(dsUbeStructure.szEnhv, 11)))
{
lpDS->cErrorCode = _J('1');
lpDS->cUBEErrorCode = _J('1');
idReturnCode = ER_ERROR;
}
if(lpDS->cErrorCode != _J('1'))
{
/* Assign Input Parameters to Report Interconnect Values */
jdeStrcpy((JCHAR *)dsReportInterconnect.szEDIKeyCompany,
(const JCHAR *)lpDS->szEDIKeyCompany);
MathCopy(&dsReportInterconnect.mnEDIDocumentNumber, &lpDS->mnEDIDocumentNumber);
jdeStrcpy((JCHAR *)dsReportInterconnect.szEDIDocumentType,
(const JCHAR *)lpDS->szEDIDocumentType);
dsReportInterconnect.cExportModeFlatFile = lpDS->cExportModeFlatFile;
dsReportInterconnect.cExportModeExternalDatabase = lpDS->cExportModeExternalDatabase;
dsReportInterconnect.cExportModeExternalAPI = lpDS->cExportModeExternalAPI;

dsUbeStructure.idRunTime = (GLRTID)lpBhvrCom->hDlg << 16;

/* Run the UBE Synchronously with this BsFn (i.e. wait until UBE
is complete before executing next line of code).
*/
dsUbeStructure.bSynchFlag = TRUE;

/* Run the UBE Without Any Prompting (i.e. Batch Mode). */
dsUbeStructure.bBatchFlag = TRUE;

/* Call Launch UBE API */
idJDBReturn = jdeLaunchUBEEx((HUSER)hUser2,
(PUBEVAR)&dsUbeStructure,
(LPVOID)&dsReportInterconnect,
(LPBHVRCOM) lpBhvrCom);

/* When UBE Managed to Launch and Return, Get Error Code back */
if(idJDBReturn != JDEDB_PASSED)
{
lpDS->cErrorCode = _J('1');
lpDS->cUBEErrorCode = _J('1');
idReturnCode = ER_ERROR;
}
else
{
lpDS->cUBEErrorCode = dsReportInterconnect.cErrorCode;
}

jdeFreeBusinessFunctionParms(lpBhvrCom2, lpVoid2);
JDB_FreeUser(hUser2);
JDB_FreeEnv(hEnv);
jdeFree(pUBEVar);

}

/************************************************************************
* Function Clean Up
************************************************************************/

return (ER_SUCCESS);
}

/* Internal function comment block */
/**************************************************************************
* Function: Ixxxxxxx_a // Replace "xxxxxxx" with source file number
* // and "a" with the function name
* Notes:
*
* Returns:
*
* Parameters:
**************************************************************************/
 
John,

Remove jdeFree(pUBEVar);

pUBEVar is never allocated.

Craig
 
Cheers Craig

Are the other resources being cleared down ok?
I'm not sure if you remember, but the gist of this is a subsystem goes on to wake up a UBE. the Subsystem is running under User A but I may invoke code to call this BSFN which then launches the UBE under User B

I think it's all right, but I'm just checking
 
You need a JDB_FreeBhvr(hUser) at the end of the function. That's the user handle being allocated with

/* Initialize Behaviour DS */
idJDBReturn = JDB_InitBhvr(lpBhvrCom, &hUser,(JCHAR *) NULL, JDEDB_COMMIT_AUTO);

The other releases look fine for the secondary env.

Craig
 
Back
Top