Subject Re: [ib-support] Is Interbase Server Running?
Author Robert F. Tulloch
Hi:

This is part of some stuff I use. It is for app running on server or
workstation.

//---------------------------------------------------------------------------
void __fastcall TDBLogonDialog::LogonOkBtnClick(TObject *Sender)
{
//Check if user name and/or password are blank
if ((Trim(UserNameEdit->Text) == "") || (Trim(PasswordEdit->Text)
== ""))
{
Application->MessageBox("You must enter a valid User Id and
Password to "
"logon to the Interbase Server.",
"Logon Server",
MB_OK | MB_ICONEXCLAMATION);
if (Trim(UserNameEdit->Text) == "")
{
UserNameEdit->SetFocus();
return;
}
else if (Trim(PasswordEdit->Text) == "")
{
PasswordEdit->SetFocus();
return;
}
}

//Set up variables
String IBServerStartCmd, Machine;
bool RemoteServer;
bool regSuccess;
String RemoteServerName;
TRegistry *Registry;

LogonOkBtn->Enabled = false;
LogonCancelBtn->Enabled = false;

//Check if using remote server and set flag
RemoteServer = (SuP.RemoteServer == "True");
MainForm->RemoteServer = RemoteServer;

Registry = new TRegistry();
try
{
Screen->Cursor = crHourGlass;
Registry->RootKey = HKEY_LOCAL_MACHINE;
if (RemoteServer)
{
Machine = SuP.ServerName;
/* Machine = SP.Data[FileData]->Strings[FServerDatabase];
RemoteServerName = Machine;
Machine.Delete(Machine.Pos(":"), (Machine.Length() + 1) -
Machine.Pos(":"));
Machine = "\\\\" + Machine;*/

regSuccess = Registry->RegistryConnect(Machine);
if (!regSuccess)
{
ShowMessage("Could not connect to the Registry on the
Network Server.");
delete Registry;
LogonOkBtn->Enabled = true;
LogonCancelBtn->Enabled = true;
Screen->Cursor = crDefault;
ModalResult = mrCancel;
return;
}
}
if
(!Registry->OpenKey("Software\\Borland\\InterBase\\CurrentVersion",
false))
{
if (RemoteServer)
ShowMessage("The InterBase Server is not installed on your
Network Server.");
else
ShowMessage("The InterBase Server is not installed on your
local system.");
delete Registry;
LogonOkBtn->Enabled = true;
LogonCancelBtn->Enabled = true;
Screen->Cursor = crDefault;
ModalResult = mrCancel;
return;
}
}
catch(ERegistryException &E)
{
ShowMessage(E.Message);
Registry->CloseKey();
delete Registry;
LogonOkBtn->Enabled = true;
LogonCancelBtn->Enabled = true;
Screen->Cursor = crDefault;
ModalResult = mrCancel;
return;
}
MainForm->InterBaseRootPath =
Registry->ReadString("RootDirectory");
if (RemoteServer)
{
//Set remote database name
String DataBaseName = SuP.ServerName;
DataBaseName.Delete(DataBaseName.Pos("\\"),2);
MainDM->LTIBDB->DatabaseName = DataBaseName + ":" +
SuP.RemotePath;
MainForm->InterBaseInstallPath = Machine + ":" +
MainForm->InterBaseRootPath;
IBServerStartCmd = "NETSVC \"InterBase Guardian\" " + Machine +
" /Start";

//
MainDM->CheckForRemoteCommonDataShare(MainDM->LTIBDB->DatabaseName);
}
else
{
//Set local database name
MainDM->LTIBDB->DatabaseName = SuP.ServerName + SuP.LocalPath;
MainForm->InterBaseInstallPath = MainForm->InterBaseRootPath;
IBServerStartCmd = Format("%s%s -a",
ARRAYOFCONST((Registry->ReadString("ServerDirectory"), "ibguard.exe",
1)));
}

Registry->CloseKey();
delete Registry;

//Load the entered password into the database object
MainDM->LTIBDB->Params->Values["user_name"] = UserNameEdit->Text;
MainDM->LTIBDB->Params->Values["password"] = PasswordEdit->Text;
if (Trim(UserNameEdit->Text) != "SYSDBA")
{
String User = Trim(UserNameEdit->Text);
String RoleName = IBControlDM->GetRoleName(User,
MainDM->LTIBDB->DatabaseName);
MainDM->LTIBDB->Params->Values["sql_role_name"] = RoleName;
}

//Try to open the database
try
{
MainDM->LTIBDB->Open();
ModalResult = mrOk;
}
catch(EIBError *E)
{
if (E->IBErrorCode == isc_network_error) //335544721)
{
if (MessageDlg("The server has not been started. Would you
like to start "
"it now? Click Yes to continue, No to cancel
and exit. ",
mtConfirmation,
TMsgDlgButtons() << mbYes << mbNo, 0) == mrYes)
{
if (!StartServer(IBServerStartCmd))
{
Screen->Cursor = crDefault;
ModalResult = mrCancel;
return;
}
else
{
LogonOkBtn->Enabled = true;
LogonCancelBtn->Enabled = true;
Screen->Cursor = crDefault;
LogonOkBtn->SetFocus();
}
}
else
{
ModalResult = mrCancel;
LogonOkBtn->Enabled = true;
LogonCancelBtn->Enabled = true;
Screen->Cursor = crDefault;
return;
}
}
if (E->IBErrorCode == isc_login) //335544472)
{
Application->MessageBox("Your UserName and/or Password are
not defined. "
"Click Cancel then contact your
System Administrator "
"to get the correct information or
set up new logon "
"parameters for you.",
"Logon Server",
MB_OK | MB_ICONEXCLAMATION);
Screen->Cursor = crDefault;
LogonOkBtn->Enabled = true;
LogonCancelBtn->Enabled = true;
UserNameEdit->SetFocus();
return;
}
else
{
MesgDlg->DisplayMsg(ERR_SERVER_LOGIN, E->Message);
if((E->IBErrorCode == isc_lost_db_connection) ||
(E->IBErrorCode == isc_unavailable) ||
(E->IBErrorCode == isc_network_error))
MainDM->SetIBErrorState();
Screen->Cursor = crDefault;
LogonOkBtn->Enabled = true;
LogonCancelBtn->Enabled = true;
}
}
}

//--------------------------------------------------------------------------
bool __fastcall TDBLogonDialog::StartServer(String IBServerStartCmd)
{
//Declare variables
DWORD cf; //Creation Flags
STARTUPINFO si;
PROCESS_INFORMATION pi;
SECURITY_ATTRIBUTES sa;

Screen->Cursor = crHourGlass;

//Initialize Creation Flags
cf = DETACHED_PROCESS;

//Initialize StartupInfo Struct
memset(&si, 0, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.lpReserved = NULL;
si.lpDesktop = NULL;
si.lpTitle = NULL;
si.cbReserved2 = 0;
si.lpReserved2 = NULL;
si.dwFlags = NORMAL_PRIORITY_CLASS |CREATE_DEFAULT_ERROR_MODE
|CREATE_NEW_CONSOLE;

//Initialize ProcessInfo structure
memset(&pi, 0, sizeof(PROCESS_INFORMATION));

//Initialize SecurityAttr Struct
memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES));
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = false;

//Create the process
if (!CreateProcess (
NULL,
IBServerStartCmd.c_str(),
&sa,
NULL,
false,
cf,
NULL,
NULL,
&si,
&pi
))
{
if (MainForm->RemoteServer)
{
ShowMessage("The local InterBase Server on the Network Server
could not be started.");
Screen->Cursor = crDefault;
LogonOkBtn->Enabled = true;
LogonCancelBtn->Enabled = true;
return false;
}
else
{
ShowMessage("The local InterBase Server could not be
started.");
Screen->Cursor = crDefault;
LogonOkBtn->Enabled = true;
LogonCancelBtn->Enabled = true;
return false;
}
}
else
{
if (MainForm->RemoteServer)
ShowMessage("The InterBase Server on the Network Server
started successfully.");
else
ShowMessage("The local InterBase Server started
successfully.");
}
//Wait for completion
WaitForInputIdle(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
Screen->Cursor = crDefault;
LogonOkBtn->Enabled = true;
LogonCancelBtn->Enabled = true;
return true;
}