Subject Re: [firebird-support] Command line options
Author Andrew Gough
Hi Mahesh,

----- Original Message -----
From: "Mahesh Ishwar" <meghanshic@...>


Thanx for the reply,Dimitry.
Actually what my requirement is that I want to start fbserver whenever my
client application starts and stop it when the user quits the client
application. It should be very much transparent to the client. Therefore,
fbserver -n would be the very much suitable option for me. But if I try to
use this, fbserver shuts down automatically after 4-5 seconds. It is only
when I use fbserver -a, that it behaves like a service. Other than that it
shuts down automatically. Can u tell me how to control this behaviour?

[....]

I was doing exactly the same thing at one stage. I created the server
process from my client application as follows:

static PROCESS_INFORMATION s_pi;
static STARTUPINFO s_si;
static bool s_bInit = false;

void Init( bool bStartDB )
{
if( !s_bInit )
{
if( bStartDB )
{
ZeroMemory( & s_si, sizeof( s_si ) );
s_si.cb = sizeof( s_si );
ZeroMemory( &s_pi, sizeof( s_pi ) );

//1. Look for a running Firebird server
//2. If not going, attempt to start it...
HWND hFB = FindWindow( "FB_Server", NULL );
if( hFB == NULL )
hFB = FindWindow( "IB_Server", NULL ); //try and find the server
by the old name

if( hFB == NULL ) //its not running, so try starting it...
{
//Attempt to determine the path to the server
LRegistry reg( HKEY_LOCAL_MACHINE,

"SOFTWARE\\FirebirdSQL\\Firebird\\CurrentVersion",
true, false );
LString sPath;
reg.Read( "ServerDirectory", sPath, "" );
reg.Close();

if( sPath.length() > 0 )
{
char buf[ _MAX_PATH ];
buf[ 0 ] = '\0';

//NB: Put quotes around the EXE file/path to prevent hacking
as per
// MSDN info for CreateProcess()....
strcat( buf, "\"" );
strcat( buf, sPath.c_str() );
strcat( buf, "\\fbserver.exe\" -a -n" ); //generate the
command line

BOOL bOK = CreateProcess( NULL, buf,
NULL, NULL, FALSE,
DETACHED_PROCESS |
NORMAL_PRIORITY_CLASS,
NULL, NULL, & s_si, & s_pi );
}
}
}
s_bInit = true;
}
}

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

void Shutdown()
{
//If we started the process, lets attempt to terminate it as well....
if( s_pi.hProcess != NULL && s_pi.hThread != NULL )
{
TerminateProcess( s_pi.hProcess, 0 );
CloseHandle( s_pi.hThread );
CloseHandle( s_pi.hProcess );
}
}


which seemed to work OK for me.

I have since changed to using the embedded server directly in the client
application process as we dont mind the local DB being exclusively
accessed - in fact its an advantage in our situation.

Andrew Gough
LiveNote Technologies Inc.