Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

COM object Cleanup 1

Status
Not open for further replies.

Glenn9999

Programmer
Jun 19, 2004
2,312
US
I've been playing with COM objects in Delphi lately (actually related to something I intend to post as a FAQ, but as a learning experience, too), and I keep finding that I get access violations if I try to clean up the COM objects I use (e.g. ComObject._Release), along with COM itself.

I figured out that Delphi initializes COM if I use the relevant unit, so I haven't been too worried about that not working from the Microsoft documentation, but I am worried about not cleaning up after things and leaving memory leaks.

Any ideas on why I keep running into access violations when I try to clean up after things in the program?

I'm waiting for the white paper entitled "Finding Employment in the Era of Occupational Irrelevancy
 
just out of interest, if you dont clean up, do you actually get leaks?

what does fastmm say?

Aaron
 
Glenn,

ActiveX COM objects are interfaces, no need to cleanup.

_Release will be called from the moment the COM object goes out of scope.

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
ActiveX COM objects are interfaces, no need to cleanup.

Okay that helped tremendously. Got some specific things to hammer out (namely an access violation trying to change a ISWbemProperty field), but hopefully everything else will be straight-forward enough.

I'm waiting for the white paper entitled "Finding Employment in the Era of Occupational Irrelevancy
 
Got some specific things to hammer out

post more if you think we can help :)

Cheers,
Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
post more if you think we can help :)

Okay, this one will be a lot harder though (the access violation I referred to above). From the examples of what I'm looking to do, I'm gathering that to call a particular COM method, I need to pass a property set which contains all the parameters specified as OleVariants. Now unfortunately, Delphi would have to be different and consequently tougher than these examples, since it seems to put everything into methods instead of simple assignments. This is what I figured out.

Code:
{1}StartUpObject := WbemServices.Get('Win32_Process', 0, nil);
{2}ConfigObject := StartUpObject.SpawnInstance_(0);
{3}InParmObject := ConfigObject.Methods_.Item('Create', 0).InParameters.SpawnInstance_(0);
{4}command := 'NOTEPAD.EXE';
{5}writeln(string(InParmObject.Properties_.Item('CommandLine', 0).Get_Name));
{6}//  InParmObject.Properties_.Item('CommandLine', 0).Set_Value(command);

Now I'm sure I might have gotten something turned around in the process of doing this, since there's so many steps compared to the examples.

Statement #1 accesses a WMI object, and #2 creates a local instance of it. I'm not sure #2 is necessary since it seems duplicative, but that's another issue for another time. #3 should return the property set for the Win32_Process.Create method. #4 assigns to an OleVariant, and #6 should set the specific property in the list brought back. #6 results in an access violation, though. I coded #5 as a check and it returns the proper property name ("CommandLine"), so I'm not sure where the source of the access violation is coming from...



I'm waiting for the white paper entitled "Finding Employment in the Era of Occupational Irrelevancy
 
this code does not generate an AV:

Code:
procedure TForm1.Button1Click(Sender: TObject);

var StartUpObject, ConfigObject,InParmObject : ISWbemObject;
    Command : OleVariant;
    WbemService : ISWbemServicesEx;
    Locator : ISWbemLocator;

begin
 Locator := CoSWbemLocator.Create;
 WbemService := Locator.ConnectServer('localhost', 'root\cimv2', '', '', '', '', 0, nil) as ISWbemServicesEx;
 StartUpObject := WbemService.Get('Win32_Process', 0, nil);
 ConfigObject := StartUpObject.SpawnInstance_(0);
 InParmObject := ConfigObject.Methods_.Item('Create', 0).InParameters.SpawnInstance_(0);
 command := 'NOTEPAD.EXE';
 Memo1.Lines.Add(string(InParmObject.Properties_.Item('CommandLine', 0).Get_Name));
 InParmObject.Properties_.Item('CommandLine', 0).Set_Value(command);
end;

I assume you imported the Microsoft WMI scripting library.

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
I'm still getting an AV...

I'm waiting for the white paper entitled "Finding Employment in the Era of Occupational Irrelevancy
 
mmm, weird.
I'm using windows 7 enterpise with Delphi 2006.

can you elaborate on what you are trying to achieve here?
there are maybe other options than WMI.

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Windows XP Delphi 2006

can you elaborate on what you are trying to achieve here?there are maybe other options than WMI.

Demonstrate for my own self (maybe others) how to call WMI methods. A learning thing.

I'm waiting for the white paper entitled "Finding Employment in the Era of Occupational Irrelevancy
 
Still getting access violations on the Set_Value method. I went ahead and posted this to MSDN, since it's beginning to sound like there's something incongruent with the API as installed on Windows XP SP3. I've tried so many different examples (the process create method along with a couple of other things) that it seems that's probably the only option, because I'm sure that many people wouldn't be posting bad code...

Anyhow, even if it doesn't get solved, it didn't turn out all bad, especially since I now know how to get information out of WMI and have what seems to be very good code to do that. I'll probably try to post it as a FAQ after I work out a couple of (hopefully) small error conditions. Then on to the other things I'm trying to work out.

Thanks for your help!

I'm waiting for the white paper entitled "Finding Employment in the Era of Occupational Irrelevancy
 
question, what version of the WMI scripting library do you have? (should be v1.2)

/Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
My import file is marked "Microsoft WMI Scripting Library version 1.2".

I'm waiting for the white paper entitled "Finding Employment in the Era of Occupational Irrelevancy
 
I finally figured this out. It turns out that Delphi (version 3, the one that seems to support it instead of Turbo Delphi 2006) incorrectly converted the SetValue portion of ISWbemProperty. It expected a pointer value and Delphi made it a regular value.

So I copied the type library, renamed it, modified the proper code and what I have works perfectly now.

So I'll probably end up putting a WMI methods FAQ and WMI registry FAQ out soon. Thanks again for your help, whosrdaddy!

I'm waiting for the white paper entitled "Finding Employment in the Era of Occupational Irrelevancy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top