0

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?

5
  • Sigh. Show a complete MCVE. Sep 13, 2016 at 19:56
  • Just a thought but you are only catching EOleExceptions. What happens if you also catch any other exception after the EOleException ?
    – GuidoG
    Sep 14, 2016 at 6:46
  • Why don't you use the Windows API function "InitiateSystemShutdown" to restart the remote computer? If the function fails "GetLastError" should give you the reason, which might be that connecting to the remote computer failed.
    – Olaf Hess
    Sep 14, 2016 at 8:20
  • I suspect you will find if you step through the code that you will find WMIService is nil, giving you your second error code. The exception may already be being trapped and used to set the return value (to nil) in 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.
    – Dsm
    Sep 14, 2016 at 11:16
  • @Dsm, I think you're correct. I actually catch the exception in my log file outside of the debugger. However, I think my unit LogFiles.NewException(E); is blocking so my rebootMachine is never returning. Sep 14, 2016 at 14:15

2 Answers 2

0

I think that what you are seeing is a first chance exception that is being trapped by SWbemLocator.ConnectServer which is why you cannot trap it yourself. I think that under these circumstances the returned value is nil. If this is correct your code can be simply adjusted like this

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);
       if assigned( WMIService ) then
       begin
          ...
       end; 
    except
      // good idea to put this in anyway, but not relevant to action
    end;
end; 
1
  • Hi @DSM, unfortunately, the code isn't getting below setting the WMIService. It is failing at WMIService := SWbemLocator.ConnectServer(host, 'root\CIMV2', username, password);. Also, I cannot check if it is assigned due to the OLEvariant type. The compiler complains the types are not compatible. Sep 14, 2016 at 14:05
0

Issue is caused by the compiler, not the application. Running the application straight from the exe outside of the compiler suppresses the popup and catches it internally as expected.

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.