I'm using Delphi 2007 to create an application that will reboot a network attached computer.
I have a unit named rebootMachine that fails when attempting to connect a machine that is not communicating. The software needs to be able to handle this condition in the event a user is attempting to reboot a machine that may be offline for some reason. When I run the code below, it halts at
WMIService := SWbemLocator.ConnectServer(host, 'root\CIMV2', username, password);
I attempt to use a try/catch block, but it is not catching the failed connection and I am not sure why. The error I am presented with states an EOleException with the message "The RPC server is unavailable". This is true as the machine is not online. After this, I get an access violation message with read of address 000000000. To note, the same code works great if the computer IS online.
function rebootMachine(host: string; username: string; password: string)
: Integer;
const
wbemFlagForwardOnly = $00000020;
var
WMIService : OLEVariant;
WbemObjectSet: OLEVariant;
WbemObject : OLEVariant;
SWbemLocator : OLEVariant;
oEnum : IEnumvariant;
iValue : LongWord;
mResult : Integer;
begin
try
CoInitialize(nil); // Initializes the COM library on the current thread
mResult := -1;
// create our wmi object
SWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
// connect remotely to the machine
WMIService := SWbemLocator.ConnectServer(host, 'root\CIMV2', username, password);
....
except
on E: EOleException do
begin
LogFiles.NewException(E);
mResult := E.ErrorCode;
result := mResult;
end;
end;
end;
So the question is, how can I catch the EOleException correctly?
ConnectServer
but you are seeing a first chance exception that will not appear if you are not debugging. Your description is not complete enough to be sure, though.