Obtain the serial number of the system

Source: Internet
Author: User

Method 1:
Int c Data Receiving Software DLG: getcom ()
{
Int reg;

// Cstringarray strcommarr;
Hkey;
Int RTN;
// M_cmbcomm.resetcontent ();
RTN = regopenkeyex (HKEY_LOCAL_MACHINE, "Hardware \ devicemap \ serialcomm ",
Null, key_read, & hkey); // open the serial port Registry
If (RTN = error_success)
{
Int I = 0;
Char portname [256], commname [256];
DWORD dwlong, dwsize;
While (1)
{
Dwsize = sizeof (portname );
Dwlong = dwsize;
RTN = regenumvalue (hkey, I, portname, & dwlong,
Null, null, (puchar) commname, & dwsize );

If (RTN = error_no_more_items) // enumeration serial port
{
Break;
}
// Strcommarr. Add (commname );

M_ctrlcomid.addstring (commname );
I ++;
}
Reg = I;
}
Regclosekey (hkey );
Return reg;
}

Use vc6.0 to automatically identify available serial ports and add them to combox.

 

Answer:

1. Create a commtest3 project, add a ComboBox to the interface, and set the ID to idc_combo.

(Note: When running a ComboBox, click the small arrow on the right of it to hide it (that is, the drop-down list is not displayed ). The following settings are required:
This is a problem that people who use VC ++ often encounter at first. In fact, the height of the drop-down list is not properly set. The height of a combo cannot be modified. It is determined by the system, but the height of the drop-down list (that is, the number of rows displayed in the drop-down list) can be set. In VB and Delphi, the default height is 8 rows, but in VC ++, the default height is 0. You need to click the drop-down arrow (this must be the position) at the design time. The box shown appears and the blue block below moves down, until a satisfactory height is reached .)

2. Add the following code to commtest3dlg. cpp.

Void ccommtest3dlg: getcom ()
{
// Obtain all available serial ports when the program starts
Handle hcom;
Int I, num, K;
Cstring STR;
Bool flag;

(Ccombobox *) getdlgitem (idc_combo)-> resetcontent ();
Flag = false;
Num = 0;
For (I = 1; I <= 16; I ++)
{// This program supports 16 serial ports
Str. Format ("\\\\\\\ Com % d", I );
Hcom = createfile (STR, 0, 0, 0,
Open_existing, file_attribute_normal, 0 );
If (invalid_handle_value! = Hcom)
{// If this serial port can be enabled, add this serial port
Closehandle (hcom );
STR = Str. mid (4 );
(Ccombobox *) getdlgitem (idc_combo)-> addstring (STR );
If (flag = false)
{
Flag = true;
Num = I;
}
}
}
I = (ccombobox *) getdlgitem (idc_combo)-> getcount ();
If (I = 0)
{// Disable "enable serial port" If no available serial port is found
(Ccombobox *) getdlgitem (idc_combo)-> enablewindow (false );
}
Else
{
K = (ccombobox *) getdlgitem (idc_combo)-> getcount ();
(Ccombobox *) getdlgitem (idc_combo)-> setcursel (k-1 );
// Mcom. bindcommp ORT (Num );
}
}

3. Add getcom () to oninitdialog ();

4. Add void getcom () to the header file ();

5. Execute the program to obtain available serial ports

 

 

  

Appendix: how to enable the serial port com10 or above in VC (reprinted)

Today, I used a previous serial port program and found that the serial port could not be opened. Because the serial port used is not a common port of COM1 and com2 but a port greater than com10, you can easily solve it by simply adding a few options. After the result is added, the test shows that initialization always fails, debugging found that the createfile always fails. Find msdn and check whether there is a difference here.

The Win32 API function createfile () can open devices in addition to common files. For example, it can be used to open a serial port to obtain a serial port handle. When using the createfile () function to open the serial port, set the file sharing mode to 0 (exclusive), set the creation parameter to open_existing, and set the template to null.

If it is from COM1 to com9, you can use "COM1"-"com9" as the file name to pass to the createfile () function. The function can return a successful result. However, if the operation object is a port of com10 or above, calling the createfile () function by naming the file name in this way will return invalid_handle_value, indicating that the port cannot be opened.

The reason for this strange phenomenon is that Microsoft's predefined standard device contains "COM1"-"com9 ". Therefore, when "COM1"-"com9" is passed as the file name to the function, the operating system automatically resolves it as the corresponding device. For serial ports such as com10 and above, file name systems such as com10 only refer to files in the general sense rather than serial devices.

To increase support for the com10 and above serial ports, Microsoft stipulates that if you want to access such a device, such a file name should be used (take com10 as an example): \. com10
Therefore, for com10 and later serial ports, the calling style of createfile () should be adjusted as follows:

Createfile (
"\\\\. \ Com10", // define the serial port name
Fdwaccess, // access mode (read/write)
0, // Shared Mode: must be set to 0, indicating that the device is exclusively used
Null, // confidentiality
Open_existing, // It must be set to open_existing
0, // File Attribute. for asynchronous mode, you can set it
Null // template, the serial port device must be set to null
);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.