Hi Katja....
Just one question
Why using the registry for this is not reliable? can you give some
examples please
Thanks
Vahan
-----Original Message-----
From: [email protected] [mailto:[EMAIL PROTECTED] Behalf
Of Katja Bergman
Sent: Monday, August 08, 2005 10:41 AM
To: [email protected]
Subject: [delphi-en] Re: Finding All Serial Ports - Help Please
Hey, why not use WMI instead?
Okay, where to start with WMI? Well, here's a simple function that
will give all the device ID's, sorted:
function EnumPorts: TStringList;
function NextItem(var Enum: IEnumVARIANT; const riid: TGUID; out
ppObject): Boolean;
var
OleProperty: OleVariant;
NumProp: LongWord;
begin
try
Result := Succeeded(Enum.Next(1, OleProperty, NumProp)) and
(NumProp > 0) and
Succeeded(IDispatch(OleProperty).QueryInterface(riid, ppObject));
except
Result := False;
IUnknown(ppObject) := nil;
end;
end;
var
Enum: IEnumVariant;
Prop: SWbemProperty;
PropEnum: IEnumVariant;
Item: SWbemObject;
ObjectSet: ISWbemObjectSet;
begin
Result := TStringList.Create;
ObjectSet := CoSWbemLocator.Create.ConnectServer('', 'root\cimv2',
'', '', '', '', 0, nil).ExecQuery('select DeviceID from
Win32_SerialPort', 'WQL', wbemFlagBidirectional, nil);
Enum := ObjectSet._NewEnum as IEnumVariant;
while NextItem(Enum, SWBemObject, Item) do begin
PropEnum := Item.Properties_._NewEnum as IEnumVariant;
while NextItem(PropEnum, SWBemProperty, Prop) do begin
try
Result.Add(Trim(VarToStr(Prop.Get_Value)));
except on E: Exception do {Do something with this error.};
end;
end;
end;
Result.Sort;
end;
This function will need the units Windows, SysUtils, Classes, ActiveX,
Variants and WbemScripting_TLB. All of them, except for the last one,
are standard Delphi units. You will have to generate the unit
WbemScripting_TLB yourself by importing the type library
C:\WINNT\system32\wbem\wbemdisp.tlb into your Delphi application.
(Menu option Project/Import type library.)
Of course, if you replace the DeviceID in above code into a * (select
* from Win32_SerialPort) you would get a lot more information about
these COM-ports, of course. But if you're only interested in the
names, this is the most udeful way to do so.
And forget reading the registry for this kind of information. That's
never going to be a reliable solution.
Of course, you could also query Win32_SerialPortConfiguration instead
of Win32_SerialPort. Then you get to know the current settings for
these ports too.
Then again, the amount of system information that you can collect
simply by using WMI is enormous. :D It is complex at first, I'll admit
that. But once you're used to it, it's extremely useful too!
With kind regards,
X Katja Bergman.
-----------------------------------------------------
Home page: http://groups.yahoo.com/group/delphi-en/
To unsubscribe: [EMAIL PROTECTED]
SPONSORED LINKS C programming language Computer programming languages The
c programming language
C programming language List of programming languages Delphi
programmer
----------------------------------------------------------------------------
----
YAHOO! GROUPS LINKS
a.. Visit your group "delphi-en" on the web.
b.. To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
c.. Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
----------------------------------------------------------------------------
----
[Non-text portions of this message have been removed]
------------------------ Yahoo! Groups Sponsor --------------------~-->
<font face=arial size=-1><a
href="http://us.ard.yahoo.com/SIG=12hds95hq/M=362131.6882499.7825260.1510227/D=groups/S=1705115362:TM/Y=YAHOO/EXP=1123498517/A=2889191/R=0/SIG=10r90krvo/*http://www.thebeehive.org
">Get Bzzzy! (real tools to help you find a job) Welcome to the Sweet Life
- brought to you by One Economy</a>.</font>
--------------------------------------------------------------------~->
-----------------------------------------------------
Home page: http://groups.yahoo.com/group/delphi-en/
To unsubscribe: [EMAIL PROTECTED]
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/delphi-en/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/