其實透過WMI也能取得COM Port的相關資訊
![](http://delphi.ktop.com.tw/loadfile.php?TOPICID=20159717&CC=450863)
範例下載:
http://delphi.ktop.com.tw/loadfile.php?TOPICID=20160030&CC=450870
unit1.H
//--------------------------------------------------------------------------- #ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include
#include
#include
#include <Forms.hpp> #include
#include
#include
#include
#include //---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TListBox *ListBox1;
TLabel *Label1;
TButton *Button2;
TButton *Button3;
void __fastcall Button1Click(TObject *Sender);
void __fastcall ListBox1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
private: // User declarations
public: // User declarations
IWbemClassObject *pSerialDev;
IEnumWbemClassObject *pEnum;
IWbemServices *pSvc;
IWbemLocator *pLoc;
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif unit1.cpp
//--------------------------------------------------------------------------- #include
#pragma hdrstop #include "Unit1.h"
#include "Unit2.h"
#include "Unit3.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
pEnum=0;
pSvc=0;
pLoc=0;
pSerialDev=0;
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender)
{
Button1->Enabled=false;
HRESULT hres; hres = CoInitializeEx(0, COINIT_MULTITHREADED); //Initialize COM. if (FAILED(hres))
{ ShowMessage("Failed to initialize COM library. Error code = "+IntToHex((int)hres,8));
Application->Terminate();
}
hres = CoInitializeSecurity(
NULL, // security descriptor
-1, // use this simple setting
NULL, // use this simple setting
NULL, // reserved
RPC_C_AUTHN_LEVEL_CONNECT, // Windows NT 4/Windows 2000 authentication level
RPC_C_IMP_LEVEL_IMPERSONATE, // impersonation level
NULL, // use this simple setting
EOAC_NONE, // no special capabilities
0); // reserved if (FAILED(hres))
{
ShowMessage("Failed to initialize security. Error code = "+IntToHex((int)hres,8));
CoUninitialize();
Application->Terminate();
} //IWbemLocator interface for obtaining the initial namespace pointer to
//the Windows Management on a particular host computer.
hres = CoCreateInstance(CLSID_WbemLocator, 0,
CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc); if (FAILED(hres))
{
ShowMessage("Failed to create IWbemLocator object. Err code = "+IntToHex((int)hres,8));
CoUninitialize();
Application->Terminate();
} hres = pLoc->ConnectServer(
L"\\\\.\\root\\cimv2",
NULL, NULL, 0, NULL, 0, 0, &pSvc); if (FAILED(hres))
{
ShowMessage("Could not connect. Error code = "+IntToHex((int)hres,8));
CoUninitialize();
Application->Terminate();
} // Set the proxy so that impersonation of the client occurs.
hres = CoSetProxyBlanket(pSvc,
RPC_C_AUTHN_WINNT, // NTLM authentication service
RPC_C_AUTHZ_NONE, // default authorization service...
NULL, // no mutual authentication
RPC_C_AUTHN_LEVEL_CONNECT, // authentication level
RPC_C_IMP_LEVEL_IMPERSONATE, // impersonation level
NULL, // use current token
EOAC_NONE); // no special capabilities if (FAILED(hres))
{
ShowMessage("Could not set proxy blanket. Error code = "+IntToHex((int)hres,8));
pSvc->Release();
pLoc->Release();
CoUninitialize();
Application->Terminate();
} //Win32_SerialPort - WMI class that contains information about serial ports
BSTR className = SysAllocString(L"Win32_SerialPort"); hres = pSvc->GetObject(className, 0, NULL, &pSerialDev, NULL );
//Retrieves an class object. ULONG puReturned; //Create instance for enumeration ports hres=pSvc->CreateInstanceEnum(
className,
NULL,
NULL,
&pEnum
); //no need in variable className now.
SysFreeString(className); if (hres != 0)
{
ShowMessage("Error");
Application->Terminate();
} // Retrieve the objects in the result set.
for (;;)
{
VARIANT Val;
CIMTYPE pvtType;
ULONG uReturned = 0; VariantInit(&Val); hres = pEnum->Next(
WBEM_INFINITE, // Time out
1, // One object
&pSerialDev,
&uReturned
); if (uReturned == 0)
break; //get name for every port and add it in the list box...
BSTR Name = SysAllocString(L"Name");
pSerialDev->Get(Name,0,&Val,&pvtType,NULL);
LPSTR SS=WideToAnsi(Val.bstrVal);
ListBox1->Items->Add(SS);
SysFreeString(Name);
delete []SS;
//... and free it. VariantClear(&Val);
} }
//---------------------------------------------------------------------------
void __fastcall TForm1::ListBox1Click(TObject *Sender)
{
if(ListBox1->ItemIndex >= 0)Form2->ShowModal();
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender)
{
Close();
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Button3Click(TObject *Sender)
{
Form3->ShowModal();
}
//--------------------------------------------------------------------------- void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
// All done.
if(pEnum)pEnum->Release();
if(pSvc)pSvc->Release();
if(pLoc)pLoc->Release();
if(pSerialDev)pSerialDev->Release();
CoUninitialize();
}
//--------------------------------------------------------------------------- Unit2.H
//--------------------------------------------------------------------------- #ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include
#include
#include
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm2 : public TForm
{
__published: // IDE-managed Components
TLabel *Label1;
TEdit *Edit1;
TLabel *Label2;
TEdit *Edit2;
TLabel *Label5;
TLabel *Label6;
TLabel *Label7;
TLabel *Label8;
TLabel *Label9;
TLabel *Label10;
TEdit *Edit5;
TEdit *Edit6;
TLabel *Label16;
TEdit *Edit7;
TEdit *Edit8;
TEdit *Edit9;
TEdit *Edit10;
TEdit *Edit16;
TLabel *Label17;
TLabel *Label19;
TLabel *Label20;
TLabel *Label21;
TEdit *Edit17;
TEdit *Edit19;
TEdit *Edit20;
TEdit *Edit21;
TLabel *Label23;
TLabel *Label25;
TLabel *Label26;
TLabel *Label27;
TLabel *Label28;
TLabel *Label29;
TLabel *Label30;
TLabel *Label31;
TLabel *Label32;
TEdit *Edit23;
TEdit *Edit25;
TEdit *Edit26;
TEdit *Edit27;
TEdit *Edit28;
TEdit *Edit30;
TEdit *Edit29;
TEdit *Edit31;
TEdit *Edit32;
TLabel *Label33;
TLabel *Label34;
TLabel *Label35;
TLabel *Label36;
TLabel *Label37;
TLabel *Label38;
TLabel *Label39;
TLabel *Label40;
TLabel *Label41;
TLabel *Label42;
TLabel *Label43;
TLabel *Label44;
TLabel *Label45;
TLabel *Label46;
TEdit *Edit33;
TEdit *Edit34;
TEdit *Edit35;
TEdit *Edit36;
TEdit *Edit37;
TEdit *Edit38;
TEdit *Edit39;
TEdit *Edit41;
TEdit *Edit40;
TEdit *Edit42;
TEdit *Edit43;
TEdit *Edit44;
TEdit *Edit46;
TButton *Button2;
TEdit *Edit45;
void __fastcall Button2Click(TObject *Sender);
void __fastcall FormActivate(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm2(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm2 *Form2;
//---------------------------------------------------------------------------
#endif Unit2.CPP
//--------------------------------------------------------------------------- #include
#pragma hdrstop #include "Unit2.h"
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------- void __fastcall TForm2::Button2Click(TObject *Sender)
{
Close();
}
//--------------------------------------------------------------------------- void __fastcall TForm2::FormActivate(TObject *Sender)
{ VARIANT Val;
VariantInit(&Val);
HRESULT hres;
hres = Form1->pEnum->Reset(); for(int i=0;; ++i)
{
ULONG uReturned = 0; hres = Form1->pEnum->Next(
0, // Time out
1, // One object
&(Form1->pSerialDev),
&uReturned
); if (uReturned == 0)
break;
if(i != Form1->ListBox1->ItemIndex)continue; LPSTR TmpString; CIMTYPE pvtType;
VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"Availability",0,&Val,&pvtType,NULL);
switch(Val.iVal)
{
case 1: Edit1->Text="Other";break;
case 2: Edit1->Text="Unknown";break;
case 3: Edit1->Text="Running/Full Power";break;
case 4: Edit1->Text="Warning";break;
case 5: Edit1->Text="In Test";break;
case 6: Edit1->Text="Not Applicable";break;
case 7: Edit1->Text="Power Off";break;
case 8: Edit1->Text="Off Line";break;
case 9: Edit1->Text="Off Duty";break;
case 10: Edit1->Text="Degraded";break;
case 11: Edit1->Text="Not Installed";break;
case 12: Edit1->Text="Install Error";break;
case 13: Edit1->Text="Power Save - Unknown";break;
case 14: Edit1->Text="Power Save - Low Power Mode";break;
case 15: Edit1->Text="Power Save - Standby";break;
case 16: Edit1->Text="Power Cycle";break;
case 17: Edit1->Text="Power Save - Warning";break;
default: Edit1->Text="Unknown";
};
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"Binary",0,&Val,&pvtType,NULL);
if(Val.boolVal)Edit2->Text="True";
else Edit2->Text="False";
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"Caption",0,&Val,&pvtType,NULL);
TmpString=WideToAnsi(Val.bstrVal);
Edit5->Text=TmpString;
delete []TmpString;
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"ConfigManagerErrorCode",0,&Val,&pvtType,NULL);
Edit6->Text=IntToStr(Val.uintVal);
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"ConfigManagerUserConfig",0,&Val,&pvtType,NULL);
if(Val.boolVal)Edit7->Text="True";
else Edit7->Text="False";
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"CreationClassName",0,&Val,&pvtType,NULL);
TmpString=WideToAnsi(Val.bstrVal);
Edit8->Text=TmpString;
delete []TmpString;
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"Description",0,&Val,&pvtType,NULL);
TmpString=WideToAnsi(Val.bstrVal);
Edit9->Text=TmpString;
delete []TmpString;
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"DeviceID",0,&Val,&pvtType,NULL);
TmpString=WideToAnsi(Val.bstrVal);
Edit10->Text=TmpString;
delete []TmpString;
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"MaximumInputBufferSize",0,&Val,&pvtType,NULL);
Edit16->Text=IntToStr(Val.uintVal);
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"MaximumOutputBufferSize",0,&Val,&pvtType,NULL);
Edit17->Text=IntToStr(Val.uintVal);
VariantClear(&Val); VariantInit(&Val);
Form1->pSerialDev->Get(L"Name",0,&Val,&pvtType,NULL);
TmpString=WideToAnsi(Val.bstrVal);
Edit19->Text=TmpString;
delete []TmpString;
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"OSAutoDiscovered",0,&Val,&pvtType,NULL);
if(Val.boolVal)Edit20->Text="True";
else Edit20->Text="False";
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"PNPDeviceID",0,&Val,&pvtType,NULL);
TmpString=WideToAnsi(Val.bstrVal);
Edit21->Text=TmpString;
delete []TmpString;
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"PowerManagementSupported",0,&Val,&pvtType,NULL);
if(Val.boolVal)Edit23->Text="True";
else Edit23->Text="False";
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"ProviderType",0,&Val,&pvtType,NULL);
TmpString=WideToAnsi(Val.bstrVal);
Edit25->Text=TmpString;
delete []TmpString;
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"SettableBaudRate",0,&Val,&pvtType,NULL);
if(Val.boolVal)Edit26->Text="True";
else Edit26->Text="False";
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"SettableDataBits",0,&Val,&pvtType,NULL);
if(Val.boolVal)Edit27->Text="True";
else Edit27->Text="False";
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"SettableFlowControl",0,&Val,&pvtType,NULL);
if(Val.boolVal)Edit28->Text="True";
else Edit28->Text="False";
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"SettableParity",0,&Val,&pvtType,NULL);
if(Val.boolVal)Edit29->Text="True";
else Edit29->Text="False";
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"SettableParityCheck",0,&Val,&pvtType,NULL);
if(Val.boolVal)Edit30->Text="True";
else Edit30->Text="False";
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"SettableRLSD",0,&Val,&pvtType,NULL);
if(Val.boolVal)Edit31->Text="True";
else Edit31->Text="False";
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"SettableStopBits",0,&Val,&pvtType,NULL);
if(Val.boolVal)Edit32->Text="True";
else Edit32->Text="False";
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"Status",0,&Val,&pvtType,NULL);
TmpString=WideToAnsi(Val.bstrVal);
Edit33->Text=TmpString;
delete []TmpString;
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"StatusInfo",0,&Val,&pvtType,NULL);
switch(Val.iVal)
{
case 1: Edit34->Text="Other";break;
case 2: Edit34->Text="Unknown";break;
case 3: Edit34->Text="Enabled";break;
case 4: Edit34->Text="Disabled";break;
case 5: Edit34->Text="Not Applicable";break;
default: Edit34->Text="Unknown";
};
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"Supports16BitMode",0,&Val,&pvtType,NULL);
if(Val.boolVal)Edit35->Text="True";
else Edit35->Text="False";
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"SupportsDTRDSR",0,&Val,&pvtType,NULL);
if(Val.boolVal)Edit36->Text="True";
else Edit36->Text="False";
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"SupportsElapsedTimeouts",0,&Val,&pvtType,NULL);
if(Val.boolVal)Edit37->Text="True";
else Edit37->Text="False";
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"SupportsIntTimeouts",0,&Val,&pvtType,NULL);
if(Val.boolVal)Edit38->Text="True";
else Edit38->Text="False";
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"SupportsParityCheck",0,&Val,&pvtType,NULL);
if(Val.boolVal)Edit39->Text="True";
else Edit39->Text="False";
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"SupportsRLSD",0,&Val,&pvtType,NULL);
if(Val.boolVal)Edit40->Text="True";
else Edit40->Text="False";
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"SupportsRTSCTS",0,&Val,&pvtType,NULL);
if(Val.boolVal)Edit41->Text="True";
else Edit41->Text="False";
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"SupportsSpecialCharacters",0,&Val,&pvtType,NULL);
if(Val.boolVal)Edit42->Text="True";
else Edit42->Text="False";
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"SupportsXOnXOff",0,&Val,&pvtType,NULL);
if(Val.boolVal)Edit43->Text="True";
else Edit43->Text="False";
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"SupportsXOnXOffSet",0,&Val,&pvtType,NULL);
if(Val.boolVal)Edit44->Text="True";
else Edit44->Text="False";
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"SystemCreationClassName",0,&Val,&pvtType,NULL);
TmpString=WideToAnsi(Val.bstrVal);
Edit45->Text=TmpString;
delete []TmpString;
VariantClear(&Val); VariantInit(&Val);
hres=Form1->pSerialDev->Get(L"SystemName",0,&Val,&pvtType,NULL);
TmpString=WideToAnsi(Val.bstrVal);
Edit46->Text=TmpString;
delete []TmpString;
VariantClear(&Val); break;
}
}
//---------------------------------------------------------------------------
/*生活是一種藝術,用心生活享受生活*/