2016-09-13 2 views
0

저는 네트워크에 연결된 컴퓨터를 재부팅 할 응용 프로그램을 만들기 위해 Delphi 2007을 사용하고 있습니다.WMI를 사용하여 EOleException 잡기

나는 통신 할 수없는 기계를 연결할 때 실패하는 rebootMachine이라는 장치가 있습니다. 이 소프트웨어는 사용자가 어떤 이유로 오프라인 일 수있는 시스템을 재부팅하려고 할 때이 조건을 처리 할 수 ​​있어야합니다. 나는 아래의 코드를 실행하면, 내가 try/catch 블록을 사용하려고 시도하지만 실패한 연결을 잡기되지 않고, 내가 왜 확실하지 않다

WMIService := SWbemLocator.ConnectServer(host, 'root\CIMV2', username, password); 

에서 중단됩니다. 내가 제시 한 오류는 "RPC 서버를 사용할 수 없습니다"라는 메시지와 함께 EOleException을 나타냅니다. 기계가 온라인이 아니기 때문에 이것은 사실입니다. 이 후, 나는 주소 000000000의 읽기와 액세스 위반 메시지가 나타납니다. 컴퓨터가 온라인 상태 인 경우 동일한 코드가 작동합니다.

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; 

그럼 내가 어떻게 EOleException을 올바르게 잡을 수 있습니까?

+0

한숨. 완전한 MCVE를 보여줍니다. –

+0

그냥 생각하지만 EOleExceptions 만 잡으려고합니다. EOleException 후에 다른 예외를 잡으면 어떻게됩니까? – GuidoG

+0

왜 Windows API 함수 "InitiateSystemShutdown"을 사용하여 원격 컴퓨터를 다시 시작하지 않습니까? 함수가 실패하면 "GetLastError"는 원격 컴퓨터에 연결하는 데 실패한 이유를 알려줍니다. –

답변

0

나는 당신이보고있는 것이 SWbemLocator.ConnectServer에 의해 덫을 놓는 첫번째 기회의 예외라고 생각합니다. 그래서 당신 스스로 그것을 함정에 빠뜨릴 수 없습니다. 이러한 상황에서 반환 값은 0이라고 생각합니다. 이것이 맞으면이 코드를 간단히 조정할 수 있습니다.

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; 
+0

안녕하세요 @DSM, 불행히도, 코드는 WMIService를 설정하는 아래에지고 있지 않습니다. WMIService : = SWbemLocator.ConnectServer (host, 'root \ CIMV2', username, password);에 실패했습니다. 또한, OLEvariant 형식으로 인해 할당 된 경우 확인할 수 없습니다. 컴파일러는 형식이 호환되지 않는다고 불평합니다. – BinaryAssault

0

문제는 응용 프로그램이 아니라 컴파일러 때문에 발생합니다. 컴파일러 외부의 exe에서 응용 프로그램을 직접 실행하면 팝업이 표시되지 않고 예상대로 내부적으로 catch됩니다.

관련 문제