Windows 7 - About Clusters

Asked By AknCa on 31-Jul-07 06:42 AM
I want my applicaton  to detect whether it runs on a cluster or not , on
startup.
How can I do this using Cluster API ?

BEST REGARDS




Christoph Lindemann replied on 01-Aug-07 03:12 AM
The following is an example in Delphi:


const
CLUSTER_NOTINSTALLED = $00000000;
CLUSTER_INSTALLED    = $00000001;
CLUSTER_CONFIGURED   = $00000002;
CLUSTER_RUNNING      = $00000010;

type
_NODE_CLUSTER_STATE = DWORD;
NODE_CLUSTER_STATE = _NODE_CLUSTER_STATE;
TNodeClusterState = _NODE_CLUSTER_STATE;

const
ClusterStateNotInstalled  = NODE_CLUSTER_STATE(CLUSTER_NOTINSTALLED);
ClusterStateNotConfigured = NODE_CLUSTER_STATE(CLUSTER_INSTALLED);
ClusterStateNotRunning    = NODE_CLUSTER_STATE(CLUSTER_INSTALLED or
CLUSTER_CONFIGURED);
ClusterStateRunning       = NODE_CLUSTER_STATE(CLUSTER_INSTALLED or
CLUSTER_CONFIGURED or CLUSTER_RUNNING);

const
CLUSAPI = 'Clusapi.dll';

var
_GetNodeClusterState : function (lpszNodeName: LPWSTR;
var pdwClusterState: DWORD): DWORD; stdcall;

function GetClusterState : TNodeClusterState;
var
SvcManHandle, SvcHandle : SC_HANDLE;
ServiceStatus: TServiceStatus;
ServiceConfig : PQueryServiceConfig;
cb : cardinal;
StateError : DWORD;
begin
{$IFDEF DEBUG}Debug.Write(TL_DEBUG,'GetClusterState');{$ENDIF}
result := ClusterStateNotInstalled;

//dynamic linking, to make NT4 compatible
if not Assigned(_GetNodeClusterState) then
@_GetNodeClusterState := GetProcAddress(GetModuleHandle(CLUSAPI),
'GetNodeClusterState');

if Assigned(_GetNodeClusterState) then begin
//windows 2000
{$IFDEF DEBUG}Debug.Write(TL_DEBUG,'GetNodeClusterState.');{$ENDIF}
StateError := _GetNodeClusterState(nil,DWORD(result));
if StateError <> ERROR_SUCCESS then begin
SetLastError(StateError);
RaiseLastWin32Error;
end;
end else begin
//windows nt4
{$IFDEF DEBUG}Debug.Write(TL_DEBUG,'OpenSCManager for "Clussvc"
test.');{$ENDIF}
SvcManHandle := OpenSCManager(nil,nil,SC_MANAGER_CONNECT);
if SvcManHandle = 0 then
RaiseLastWin32Error;
try
SvcHandle := OpenService(SvcManHandle,'Clussvc',SERVICE_QUERY_STATUS
or SERVICE_QUERY_CONFIG);
if SvcHandle <> 0 then
try
cb := 0;
QueryServiceConfig(SvcHandle,nil,0,cb);
ServiceConfig := AllocMem(cb);
try
Win32Check(QueryServiceConfig(SvcHandle,ServiceConfig,cb,cb));
if ServiceConfig^.dwStartType <> SERVICE_DISABLED then begin
Win32Check(QueryServiceStatus(SvcHandle,ServiceStatus));
case ServiceStatus.dwCurrentState of
SERVICE_CONTINUE_PENDING,
SERVICE_PAUSED,
SERVICE_START_PENDING,
SERVICE_STOPPED :
result := ClusterStateNotRunning;
SERVICE_PAUSE_PENDING,
SERVICE_RUNNING,
SERVICE_STOP_PENDING :
result := ClusterStateRunning;
end;
end else
result := ClusterStateNotConfigured;
finally
FreeMem(ServiceConfig);
end;
finally
CloseServiceHandle(SvcManHandle);
end
else begin
Case GetLastError of
ERROR_ACCESS_DENIED :
result := ClusterStateNotConfigured;
ERROR_INVALID_NAME,
ERROR_SERVICE_DOES_NOT_EXIST :
result := ClusterStateNotInstalled
else
RaiseLastWin32Error;
end;
end;

finally
CloseServiceHandle(SvcManHandle);
end;
end;
end;




--
Christoph Lindemann
Undocumented Printing
http://www.undocprint.org/
AknCa replied on 01-Aug-07 05:14 AM
Thank you  very much for your answer.

But  i couldnt figure out exact code flow unfortunately,

What is the main method  for checking run on a cluster ?

Is it GetNodeClusterState or  SCM functions or both ?

GetNodeClusterState  returns state of a given node , so how can i find name
of node on my local machine programatically ?

BEST REGARDS