Subject Re: How to ShutDown Firebird Server running as a Application
Author jwdongcn
Peter,Thank You.
I am learning your code. :-)

Regards
jwdong
--- In ib-support@yahoogroups.com, <peter@c...> wrote:
> Re shutdown
>
> I possibly think this is a sledgehammer to crack a nut, but first I
would investigate if the firebird guardian has an option if not then
maybe the firebird server has a command line option.
> If all else fails then....
> if you have any programming tools, then the windows api provides a
facility via the WM_CLOSE message.
>
> Here is an example I got from the net for VB and also one in C ,
this program could then be called from the command line, or if you
are familiar with console applications it would be easy to write one
in your favourite application.
>
> As I said though I would be surprised if there is not a command
line you can call the server with to close it down.
>
> Rgds
>
> Peter
>
>
> Abstract
> From within a Visual Basic?application, you can terminate another
Windows?based application that is currently running. To do this, you
send a WM_CLOSE command to the running application. This terminates
the program just as if you had clicked on the Close command in the
application's control menu. This article explains how you can
terminate a program from within a Visual Basic application.
>
> Terminating Running Applications
> There are several steps you need to perform in your Visual Basic?
program before you can successfully terminate a running application.
>
> First, you need to determine the running application's window
handle. This can be done by calling theWindows?application
programming interface (API) FindWindow function. Next, you must use
the GetWindow function to make sure that you are not trying to
terminate your own Visual Basic program. Second, you must be certain
the window handle does not refer to a window that is disabled or
otherwise not able to be terminated. As long as the above-mentioned
conditions have been met, you can call the PostMessage function to
terminate the running application.
>
> The Windows API PostMessage function is the key to terminating a
program running in Windows. After you have determined the
application's handle, you simply execute the PostMessage function
with the WM_CLOSE command as an argument.
>
> To declare this function within your program, include the following
Declare statement in the Global Module or General Declarations
section of your form:
>
> Declare Function PostMessage Lib "User" (ByVal Hwnd As Integer,
ByVal wMsg
> As Integer, ByVal wParam As Integer, ByVal lParam As Long) As
Integer
> Note that this Declare statement must be typed as one single line
of text.
>
> The PostMessage function requires four arguments to be passed to
it. These arguments are as follows:
>
> hWnd An integer value set to the window's handle
> wMsg An integer value set to the message ID that you want to
send to the window
> wParam An integer value set to a 16-bit parameter (depends on
wMsg)
> lParam A string or long value (depends on wMsg)
>
> After calling the PostMessage function, an integer value will be
returned. If this value is set to TRUE (nonzero), the function was
successful and the target application was terminated.
>
> Example Program
> The following Visual Basic program shows how you can terminate an
application currently running in Windows. This example assumes that
the application you want to terminate is the Windows Solitaire game
and that it is currently running in memory.
>
> When you execute this program, it will display a message box
telling you either that Solitaire is not running (in which case the
program simply ends) or that Solitaire is running. Click the OK
command button and Solitaire will be immediately terminated.
>
> 1.. Start a new project in Visual Basic. Form1 is created by
default.
> 2.. Add the following Constants and Declare statements to the
General Declarations section of Form1 (note that each Declare
statement must be typed as a single line of text):
> Declare Function IsWindow Lib "User" (ByVal Hwnd As Integer) As
Integer
> Declare Function GetWindow Lib "User" (ByVal Hwnd As Integer, ByVal
wCmd
> As Integer) As Integer
> Declare Function GetWindowLong Lib "User" (ByVal Hwnd As Integer,
ByVal nIndex
> As Integer) As Long
> Declare Function PostMessage Lib "User" (ByVal Hwnd As Integer,
ByVal wMsg
> As Integer, ByVal wParam As Integer, ByVal lParam As Long) As
Integer
> Declare Function FindWindow Lib "User" (ByVal lpClassName As Any,
ByVal
> lpWindowName As String) As Integer
> Const GW_OWNER = 4
> Const GWL_STYLE = -16
> Const WS_DISABLED = &H8000000
> Const WS_CANCELMODE = &H1F
> Const WM_CLOSE = &H10
> 3.. Add the following code to the Form_Load event for Form1:
> Sub Form_Load()
> Dim Hwnd As Integer
> Dim Y As Integer
> Hwnd = FindWindow(0&, "Solitaire")
> If Hwnd = 0 Then
> MsgBox "SOLITAIRE is not running"
> Exit Sub
> Else
> MsgBox "Click to quit SOLITAIRE"
> End If
> Y = EndTask(Hwnd)
> If Y <> 0 Then
> MsgBox "SOLITAIRE terminated"
> Else
> MsgBox "Error - Cannot terminate SOLITAIRE"
> End If
> End Sub
> 4.. Create a new function called EndTask. Type the following code
for this function:
> Function EndTask(TargetHwnd As Integer) As Integer
> Dim X As Integer
> Dim ReturnVal As Integer
> If TargetHwnd = hWndMe% Or GetWindow(TargetHwnd, GW_OWNER) =
hWndMe% Then
> End
> End If
> If IsWindow(TargetHwnd) = False Then GoTo EndTaskFail
> If (GetWindowLong(TargetHwnd, GWL_STYLE) And WS_DISABLED) Then
GoTo EndTaskSucceed
>
> If IsWindow(TargetHwnd) Then
> If Not (GetWindowLong(TargetHwnd, GWL_STYLE) And
WS_DISABLED) Then
> X = PostMessage(TargetHwnd, WM_CANCELMODE, 0, 0&)
> X = PostMessage(TargetHwnd, WM_CLOSE, 0, 0&)
> DoEvents
> End If
> End If
> GoTo EndTaskSucceed
>
> EndTaskFail:
> ReturnVal = False
> GoTo EndTaskEndSub
> EndTaskSucceed:
> ReturnVal = True
> EndTaskEndSub:
> EndTask% = ReturnVal
> End Function
>
**********************************************************************
********************************************
>
> C Version
>
> Use function
> BOOL TerminateProcess(
> HANDLE hProcess, // handle to the process
> UINT uExitCode // exit code for the process
> );
>
> Example for Win95:
> ------------------------------------------------------------
> #include <windows.h>
> #include <tlhelp32.h>
>
> struct PINFO
> {
> DWORD dwPriorityClass;
> LONG pcPriClassBase;
> DWORD pid;
> DWORD cntThreads;
> char szFullPath[256];
> char szModName[64];
> };
>
>
> BOOL KillProcess(char *);
> BOOL GetProcessModule (DWORD, DWORD, LPMODULEENTRY32, DWORD);
>
> void main(){
>
>
> KillProcess("CALC.EXE");
> }
>
>
>
>
> BOOL KillProcess(char* ModName)
> {
> HANDLE hProcessX;
> HANDLE hSnapshot = NULL;
> BOOL bRet = FALSE;
> PROCESSENTRY32 pe32 = {0};
> HANDLE hProcessSnap;
> // Take a snapshot of all processes currently in the system.
> //
> hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
> if (hProcessSnap == (HANDLE)-1)
> return FALSE;
>
> // Fill in the size of the structure before using it.
> pe32.dwSize = sizeof(PROCESSENTRY32);
>
> // Walk the snapshot of the processes, and for each process, get
>
> // information to display.
> if (Process32First(hProcessSnap, &pe32)) {
> BOOL bGotModule = FALSE;
> MODULEENTRY32 me32 = {0};
> PINFO pi = {0};
> bRet = FALSE;
> do {
> bGotModule = GetProcessModule(pe32.th32ProcessID,
> pe32.th32ModuleID, &me32, sizeof(MODULEENTRY32));
> if (bGotModule) {
> HANDLE hProcess;
>
> // Get the actual priority class.
>
> hProcess = OpenProcess (PROCESS_ALL_ACCESS,
> FALSE, pe32.th32ProcessID);
>
> pi.dwPriorityClass = GetPriorityClass (hProcess);
>
> if( strcmp(me32.szModule,ModName)==NULL)
> {
> TerminateProcess(hProcess,0);
> }
> CloseHandle (hProcess);
> }
> }
> while (Process32Next(hProcessSnap, &pe32));
> }
> else
> bRet=FALSE; // could not walk the list of processes
>
> // Do not forget to clean up the snapshot object.
> CloseHandle (hProcessSnap);
> return bRet;
> }
>
> BOOL GetProcessModule (DWORD dwPID, DWORD dwModuleID,
> LPMODULEENTRY32 lpMe32, DWORD cbMe32)
> {
> BOOL bRet = FALSE;
> BOOL bFound = FALSE;
> HANDLE hModuleSnap = NULL;
> MODULEENTRY32 me32 = {0};
>
> // Take a snapshot of all modules in the specified process.
> hModuleSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID);
> if (hModuleSnap == (HANDLE)-1)
>
> return (FALSE);
>
> // Fill the size of the structure before using it.
> me32.dwSize = sizeof(MODULEENTRY32);
>
> // Walk the module list of the process, and find the module of
> // interest. Then copy the information to the buffer pointed
> // to by lpMe32 so that it can be returned to the caller.
> if (Module32First(hModuleSnap, &me32)) {
> do {
> if (me32.th32ModuleID == dwModuleID) {
> CopyMemory (lpMe32, &me32, cbMe32);
> bFound = TRUE;
> }
> }
> while (!bFound && Module32Next(hModuleSnap, &me32));
>
> bRet = bFound; // if this sets bRet to FALSE, dwModuleID
> // no longer exists in specified process
> }
> else
> bRet = FALSE; // could not walk module list
>
> // Do not forget to clean up the snapshot object.
> CloseHandle (hModuleSnap);
>
> return (bRet);
> }
>
>
>
>
>
> ----- Original Message -----
> From: jwdongcn
> To: ib-support@yahoogroups.com
> Sent: Friday, June 06, 2003 3:40 AM
> Subject: [ib-support] How to ShutDown Firebird Server running as
a Application
>
>
> Hello!
> I need to shutdown firebird before uninstall.
> When the Firebird server runs as a service, I can use "instsvc
stop"
> However, if the Firebird server run as a Application, how can I
> shutdown the server in a command line? (I don't want to ask user
to
> do it manually)
>
> I run the server in windows 98.
>
> thanks.
>
>
>
> Yahoo! Groups Sponsor
>
>
>
>
>
> To unsubscribe from this group, send an email to:
> ib-support-unsubscribe@egroups.com
>
>
>
> Your use of Yahoo! Groups is subject to the Yahoo! Terms of
Service.
>
>
> [Non-text portions of this message have been removed]