0

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;
14
  • 3
    try/except is the correct solution. It handles COM/OLE errors just fine (in your examples, you are missing the except 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. Jul 22, 2016 at 5:48
  • Thanks for the reply, I have updated the OP with a code snippet from the function. Jul 22, 2016 at 13:53
  • 2
    Did you test this outside the IDE? The IDE halts by default on exceptions, though you can make exceptions for this in the debugger's settings Jul 22, 2016 at 14:21
  • FWIW, since I assume Binary didn't really forget except in his code, I added except where necessary. That was not causing the problem. Jul 22, 2016 at 14:30
  • 1
    @Marco: you could be right. The fact that the IDE stops on an exception doesn't mean it is not handled, it simply means the IDE warns you before the exceptin handler is entered. This does not influence program flow. Just press [Continue] and the exception handler is invoked. Jul 22, 2016 at 14:31

1 Answer 1

0

(Posted answer on behalf of the OP).

The issue was not code related. Running the application outside the IDE allowed the exception to be handled properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.