I was asked to make an application (in pascal/delphi 2007) that can be used to reboot remote machines (running XP) based on user input. I've made a similar piece of software in C#/.NET2.0. I am new to Pascal/Delphi, however I managed to translate my code for the most part. The systems that this application will run on do not always have .NET unfortunately.
To determine if the remote machine is active, I attempt a ping. The ping uses WMI to connect to the computer, which requires a username/password.
Is there a better way to test if a remote machine is on the network? I've done research and pinging seems to be the best bet in this scenario. Unfortunately the remote machines I am rebooting have their drives write-protected so I cannot create a server/client scenario.
If the remote machine is not connected on the network, the application throws an exception for Access Denied.
In C#, I could do something along the lines of
try {
//WMI code to connect to remote machine
}
catch (Exception Except)
{
WrteExceptionToLog(Except);
}
In Delphi, I attempted the following:
try
//WMI code to connect to remote machine
except
on E: Exception do
WrteExceptionToLog(E);
end;
Although that type of try/except works in most other parts of the code it doesn't seem to handle WMI calls. It does not catch the exception and it goes straight to the compiler catching it as a EOleException. Being desperate, I tried
try
//WMI code to connect to remote machine
except
on E: EOleException do
WrteExceptionToLog(E);
end;
This resulted the same as the exception.
I did attempt to implement a universal 'catch-all' exception handler. This worked, but after logging the error, it would crash the application. I did not pursue this any further. The logging function works fine outside of the universal exception handler.
function pingMachine(const Address: string; Retries, BufferSize: Word): Integer;
var
FSWbemLocator : OLEVariant;
FWMIService : OLEVariant;
FWbemObjectSet: OLEVariant;
FWbemObject : OLEVariant;
oEnum : IEnumvariant;
iValue : LongWord;
i : Integer;
mResult : Integer;
PacketsReceived: Integer;
Minimum : Integer;
Maximum : Integer;
Average : Integer;
test: Integer;
begin
PacketsReceived := 0;
Minimum := 0;
Maximum := 0;
Average := 0;
mResult := -1;
try
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
//WMI exception below when remote machine is offline
FWMIService := FSWbemLocator.ConnectServer(Address, 'root\CIMV2',
'USERNAME', 'PASSWORD');
//code to do the actual ping...
except
on E: EOleException do
begin
LogFiles.NewException(E);
end;
on E: Exception do
begin
LogFiles.NewException(E);
end;
end;
try/except
is the correct solution. It handles COM/OLE errors just fine (in your examples, you are missing theexcept
keyword, though). If you are having problems, it has to be in code you have not shown. Please provide a Minimal, Complete, and Verifiable example.except
in his code, I added except where necessary. That was not causing the problem.