سؤال

one of my OleVariant variable was unassigned 2 times by mistake in my program (under Delphi 7).

Some end-users reported that the program may hang.

I was wondering if the unassignment of an OleVariant (already unassigned) could be the cause of the program to hang?

Please see below the 2 unassignments in function GetWin32_NTEventlogFileInfo where TempObj:=Unassigned; is written 2 times...

Thanks for help.

    program GetWMI_Info;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  ActiveX,
  ComObj,
  WbemScripting_TLB,
  Variants;



// CIM_DataFile est un type de fichiers logiques qui est un ensemble nommé de données ou un code exécutable.
// <B>Le comportement du fournisseur supportant cette classe sera modifié lors des prochaines sorties. Actuellement le fournisseur renvoie les fichiers sur des disques fixes ainsi que des fichiers mappés sur des disques logiques. À l’avenir, seules des instances de fichiers sur des disques fixes locaux seront renvoyés.<B>

procedure  GetWin32_NTEventlogFileInfo;
const
  WbemUser            ='';
  WbemPassword        ='';
  WbemComputer        ='localhost';
  wbemFlagForwardOnly = $00000020;
var
  FSWbemLocator : ISWbemLocator;
  FWMIService   : ISWbemServices;
  FWbemObjectSet: ISWbemObjectSet;
  FWbemObject   : ISWbemObject;
  FWbemPropertySet: ISWbemPropertySet; 
  TempObj         : OleVariant;  
  oEnum         : IEnumvariant;
  iValue        : Cardinal;
begin                
  FSWbemLocator := CoSWbemLocator.Create;
  FWMIService   := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser, WbemPassword, '', '', 0, nil);
  FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_NTEventlogFile','WQL', wbemFlagForwardOnly, nil);
  oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  while oEnum.Next(1, TempObj, iValue) = 0 do
  begin
    FWbemObject     := IUnknown(TempObj) as ISWBemObject;
    FWbemPropertySet:= FWbemObject.Properties_;

    Writeln(Format('CSName         %s',[String(FWbemPropertySet.Item('CSName', 0).Get_Value)]));// String
    Writeln(Format('Description    %s',[String(FWbemPropertySet.Item('Description', 0).Get_Value)]));// String

    Writeln('');
    TempObj:=Unassigned;
    TempObj:=Unassigned; // MY MISTAKE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  end;
end;


begin
 try
    CoInitialize(nil);
    try
      GetWin32_NTEventlogFileInfo;
    finally
      CoUninitialize;
    end;
 except
    on E:EOleException do
        Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode])); 
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
 Writeln('Press Enter to exit');
 Readln;      
end.
هل كانت مفيدة؟

المحلول

The line of code that sets that variable to Unassigned can safely be written 0, 1, 2, or more times. Something else is causing your problem.

Add madExcept into your process and when it hangs use the madTraceProcess tool to inspect the state of your program.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top