Subject | Code for calling GBAK and getting return code |
---|---|
Author | Joe Martinez |
Post date | 2002-05-09T15:08:39Z |
I did it!
I've finally figured out how to call GBAK from my application, and get
the return code that says whether it was successful or not. Thanks for
those who pointed me in the right direction. I am sharing my code here,
so that it can help someone else. This is C++ Builder code:
-----------------------------------------------------------------------------------------------------------------------
String MyServerPath;
String BackupString;
BOOL execresult;
// MY CODE TO FIGURE OUT THE PATH TO GBAK.EXE
MyServerPath =
GetRegistryString(HKEY_LOCAL_MACHINE,"SOFTWARE\\FirebirdSQL\\Firebird\\CurrentVersion","ServerDirectory");
if (MyServerPath == "***ERROR***")
{
MyServerPath =
GetRegistryString(HKEY_LOCAL_MACHINE,"SOFTWARE\\Borland\\Interbase\\CurrentVersion","ServerDirectory");
if (MyServerPath == "***ERROR***")
{
MyServerPath =
GetRegistryString(HKEY_LOCAL_MACHINE,"SOFTWARE\\InterBase
Corp\\Interbase\\CurrentVersion","ServerDirectory");
if (MyServerPath == "***ERROR***")
{
Application->MessageBox("Unable to find path to GBAK.EXE.
Aborting.","Error",ID_OK);
return;
}
}
}
String databasepath = DatabaseFilenameEdit->Text;
String backuppath = BackupFilenameEdit->Text;
DeleteFile("gbaklog.txt");
BackupString = MyServerPath + "\\gbak -B -T -V -Y gbaklog.txt -USER
SYSDBA -PAS " + PasswordEdit->Text + " " + databasepath + " " +
backuppath;
STARTUPINFO si;
PROCESS_INFORMATION pi;
memset(&si,0,sizeof(STARTUPINFO));
memset(&pi,0,sizeof(PROCESS_INFORMATION));
si.cb = sizeof(STARTUPINFO);
execresult = CreateProcess(
NULL,
BackupString.c_str(),
NULL,
NULL,
FALSE,
NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE,
NULL,
NULL,
&si,
&pi);
if (!execresult)
{
Application->MessageBox("Backup Failed. Unable to start
GBAK.EXE.","Error",ID_OK);
return;
}
DWORD ExitCode = STILL_ACTIVE;
while (ExitCode == STILL_ACTIVE)
{
WaitForSingleObject(pi.hProcess,INFINITE);
GetExitCodeProcess(pi.hProcess,&ExitCode);
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
if (ExitCode == 0)
Application->MessageBox("Backup completed
successfully.","Success!",MB_OK);
else
{
if (Application->MessageBox("Backup Failed. Do you want to view the
log file?","Warning",MB_YESNO|MB_ICONERROR) == IDYES)
WinExec("notepad gbaklog.txt",SW_SHOW);
}
I've finally figured out how to call GBAK from my application, and get
the return code that says whether it was successful or not. Thanks for
those who pointed me in the right direction. I am sharing my code here,
so that it can help someone else. This is C++ Builder code:
-----------------------------------------------------------------------------------------------------------------------
String MyServerPath;
String BackupString;
BOOL execresult;
// MY CODE TO FIGURE OUT THE PATH TO GBAK.EXE
MyServerPath =
GetRegistryString(HKEY_LOCAL_MACHINE,"SOFTWARE\\FirebirdSQL\\Firebird\\CurrentVersion","ServerDirectory");
if (MyServerPath == "***ERROR***")
{
MyServerPath =
GetRegistryString(HKEY_LOCAL_MACHINE,"SOFTWARE\\Borland\\Interbase\\CurrentVersion","ServerDirectory");
if (MyServerPath == "***ERROR***")
{
MyServerPath =
GetRegistryString(HKEY_LOCAL_MACHINE,"SOFTWARE\\InterBase
Corp\\Interbase\\CurrentVersion","ServerDirectory");
if (MyServerPath == "***ERROR***")
{
Application->MessageBox("Unable to find path to GBAK.EXE.
Aborting.","Error",ID_OK);
return;
}
}
}
String databasepath = DatabaseFilenameEdit->Text;
String backuppath = BackupFilenameEdit->Text;
DeleteFile("gbaklog.txt");
BackupString = MyServerPath + "\\gbak -B -T -V -Y gbaklog.txt -USER
SYSDBA -PAS " + PasswordEdit->Text + " " + databasepath + " " +
backuppath;
STARTUPINFO si;
PROCESS_INFORMATION pi;
memset(&si,0,sizeof(STARTUPINFO));
memset(&pi,0,sizeof(PROCESS_INFORMATION));
si.cb = sizeof(STARTUPINFO);
execresult = CreateProcess(
NULL,
BackupString.c_str(),
NULL,
NULL,
FALSE,
NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE,
NULL,
NULL,
&si,
&pi);
if (!execresult)
{
Application->MessageBox("Backup Failed. Unable to start
GBAK.EXE.","Error",ID_OK);
return;
}
DWORD ExitCode = STILL_ACTIVE;
while (ExitCode == STILL_ACTIVE)
{
WaitForSingleObject(pi.hProcess,INFINITE);
GetExitCodeProcess(pi.hProcess,&ExitCode);
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
if (ExitCode == 0)
Application->MessageBox("Backup completed
successfully.","Success!",MB_OK);
else
{
if (Application->MessageBox("Backup Failed. Do you want to view the
log file?","Warning",MB_YESNO|MB_ICONERROR) == IDYES)
WinExec("notepad gbaklog.txt",SW_SHOW);
}