Home > Back-end >  C Builder for a serial port resource problems
C Builder for a serial port resource problems

Time:09-18

#include
Void __fastcall TForm1: : Button1Click (TObject * Sender)
{
TRegistry * reg=new TRegistry;

Reg - & gt; RootKey=HKEY_LOCAL_MACHINE;
Reg - & gt; OpenKey (" HARDWARE \ \ DEVICEMAP \ \ SERIALCOMM ", true);
CbxComlist - & gt; Sorted=false;
Reg - & gt; GetValueNames (cbxComlist - & gt; The Items);

CbxComlist - & gt; The Items - & gt; BeginUpdate ();
for (int i=0; IItems - & gt; The Count. I++)
CbxComlist - & gt; The Items - & gt; Strings [I]=
Reg - & gt; ReadString (cbxComlist - & gt; The Items - & gt; Strings [I]);
CbxComlist - & gt; The Items - & gt; EndUpdate ();

The delete reg.

CbxComlist - & gt; Sorted=true;
}
Do not spray me, this is the COPY of demon elder brother a piece of code, my environment is XE8 c + + Builder, but after GetValueNames cbxComlist - & gt; IItems - & gt; Count
0, a great god to reassure? PS, don't, please understand

CodePudding user response:

 # include & lt; Registry. Hpp> 
Void __fastcall TForm1: : Button1Click (TObject * Sender)
{
TRegistry * reg=new TRegistry;

Reg - & gt; RootKey=HKEY_LOCAL_MACHINE;
Reg - & gt; OpenKey (" HARDWARE \ \ DEVICEMAP \ \ SERIALCOMM ", true);
CbxComlist - & gt; Sorted=false;
Reg - & gt; GetValueNames (cbxComlist - & gt; The Items);

CbxComlist - & gt; The Items - & gt; BeginUpdate ();
for (int i=0; iCbxComlist - & gt; The Items - & gt; Strings [I]=
Reg - & gt; ReadString (cbxComlist - & gt; The Items - & gt; Strings [I]);
CbxComlist - & gt; The Items - & gt; EndUpdate ();

The delete reg.

CbxComlist - & gt; Sorted=true;
}


Code test no problem, you check ~ ~

CodePudding user response:

... Do not spray me, this is COPY a piece of code of demon elder brother...
Black beauty! A,

With API: EnumPorts to make right, look, from the view of now before enumeration registry way is a bit LOW,

CodePudding user response:

Key point of this problem is not a c + + Buidler version problem, is the registry permissions, after doing the system of "the HARDWARE \ \ DEVICEMAP \ \ SERIALCOMM" for the average user is read-only, if OpenKey the second parameter is the true need for write access, it will cause the failure of OpenKey execution, the back of the code is invalid, only need to change OpenKey second parameter to false will succeed, so don't need to get write access,

Reg - & gt; OpenKey (_T (" HARDWARE \ \ DEVICEMAP \ \ SERIALCOMM "), false);

CodePudding user response:

refer to the second floor ccrun response:
... Do not spray me, this is COPY a piece of code of demon elder brother...
Black beauty! A,

With API: EnumPorts to make right, look, from the view of now before enumeration registry way is a bit LOW,



Demon elder brother, I use the following code, will miss two serial ports, it has pulled out of the usb serial port "back", feel still no reliable registry,

 const LPTSTR pName=NULL;//here is empty, it is the native 
Const DWORD Level=2;//specified port type structure, namely PORT_INFO_2
LPBYTE port=0;//receiving port information structure array pointer
DWORD pcbNeeded=0;//buffer pointer (required)
DWORD pcReturned=0;//return the result PORT_INFO_ * structure buffer number

If (EnumPorts (NULL, Level, port, 0, & amp; PcbNeeded, & amp; PcReturned))
{
return ;//the same function, the first execution to obtain the required buffer size pcbNeeded
}
Port=new BYTE [pcbNeeded];
if (! EnumPorts (pName, Level, port, pcbNeeded, & amp; PcbNeeded, & amp; PcReturned))
{
return ;//the same function, the second to perform access port number pcReturned
}

PORT_INFO_2 ArrPort [100]={0};
Memcpy (ArrPort, port, pcbNeeded);//remove the port information into the array

for(int i=0; i{
//printf (" % s \ r \ n ", ArrPort [I] pPortName);//return port name
Memo1 - & gt; Lines - & gt; Add (ArrPort [I] pPortName);
}
Delete the port;
  • Related