Saturday, December 22, 2007

Connect to Firebird 1.5 database on Windows Vista using Local Protocol

Using Local Protocol to connect to Firebird database no longer works in Windows Vista when we install firebird server using setup file's default configuration. Using local connection is convenient and fast if we perform GBAK, GFIX, QLI operation compare to TCP/IP connection. To configure Firebird 1.5 works with windows vista for local connection, install the firebird server to run as application instead of service: I haven't try using local connection for Firebird 2.x install on windows vista. Some suggest that changing IPCName in firebird.conf works even running firebird as service.

Saturday, December 08, 2007

Create a I/O redirection console application

Typical DOS application that may perform great function are GREP, MORE, SORT. These functions chain together via command redirection operators may perform complex operations. Here is a simple application written by Delphi to show how to deal with I/O redirection:
program pipe;

{$APPTYPE CONSOLE}

var S: string;
   F: TextFile;

begin
 AssignFile(F, '');
 Reset(F);
 while not Eof(F) do begin
   Readln(F, s);
   Writeln(s);
   Flush(F);
 end;
 CloseFile(F);
end.

Scrolling TMemo and TRichEdit control at runtime

Here is the code to scroll TMemo control to bottom at runtime:
var M: TWMVScroll;
begin
 M.Msg := WM_VSCROLL;
 M.ScrollCode := SB_BOTTOM;
 Memo1.Dispatch(M);
end;
As such, we may use the code above to perform various kind scrolling operation during runtime:
{ Scroll Bar Commands }
{$EXTERNALSYM SB_LINEUP}
SB_LINEUP = 0;
{$EXTERNALSYM SB_LINELEFT}
SB_LINELEFT = 0;
{$EXTERNALSYM SB_LINEDOWN}
SB_LINEDOWN = 1;
{$EXTERNALSYM SB_LINERIGHT}
SB_LINERIGHT = 1;
{$EXTERNALSYM SB_PAGEUP}
SB_PAGEUP = 2;
{$EXTERNALSYM SB_PAGELEFT}
SB_PAGELEFT = 2;
{$EXTERNALSYM SB_PAGEDOWN}
SB_PAGEDOWN = 3;
{$EXTERNALSYM SB_PAGERIGHT}
SB_PAGERIGHT = 3;
{$EXTERNALSYM SB_THUMBPOSITION}
SB_THUMBPOSITION = 4;
{$EXTERNALSYM SB_THUMBTRACK}
SB_THUMBTRACK = 5;
{$EXTERNALSYM SB_TOP}
SB_TOP = 6;
{$EXTERNALSYM SB_LEFT}
SB_LEFT = 6;
{$EXTERNALSYM SB_BOTTOM}
SB_BOTTOM = 7;
{$EXTERNALSYM SB_RIGHT}
SB_RIGHT = 7;
{$EXTERNALSYM SB_ENDSCROLL}
SB_ENDSCROLL = 8;

Friday, December 07, 2007

Capture the output of console process and display on your GUI application

I use GBAK.EXE to perform backup and restore operation of Firebird database in my GUI application design. The reason I use GBAK instead of firebird service manager is I can perform remote backup and restore operation via TCP/IP network without transferring the backup file to firebird server first. However, as GBAK is a console utility, It display the output to STDOUT or STDERR. There is no easy way to capture the output while the console process is running. We need to invoke windows API to perform the task. We may use CreatePipe and CreateProcess to redirect the STDxxx handles and use ReadFile to capture the output in our GUI application. Here are some references that may helps:
  1. Microsoft knowledge base: How to spawn console processes with redirected standard handles
  2. Borland newsgroup: Catch console output in real time
There is a drawback using windows native API aproach. As stated in microsoft knowledge base:
Child processes that use such C run-time functions as printf() and fprintf() can behave poorly when redirected. The C run-time functions maintain separate IO buffers. When redirected, these buffers might not be flushed immediately after each IO call. As a result, the output to the redirection pipe of a printf() call or the input from a getch() call is not flushed immediately and delays, sometimes-infinite delays occur. This problem is avoided if the child process flushes the IO buffers after each call to a C run-time IO function. Only the child process can flush its C run-time IO buffers. A process can flush its C run-time IO buffers by calling the fflush() function.
I face this problem if I use GBAK to perform a lengthy backup and restore operation via this approach. The GUI application always stop half way in unforeseen point although it will finish the operation at last. There isn't any response from the ReadFile when it hang some where. I suspect it was due the GBAK utility written didn't perform flush as described.