Recently learned Visual Studio C + + software development, do a serial tool, using the combo box drop-down option to select the serial number, there are two ways, the first automatic add the string number to the tool code, such as some common serial tools can choose the COM1~COM9 string number, You only need to add the serial number sequence when the code is initialized, or you can include "COM1" in the combo box property menu data. COM2; COM3; COM9; ", this method in the actual use of time, will be more troublesome, because there is only 2 serial port, but to choose one in 10 sequences.
So there is another kind of more common processing, this method is to automatically detect the current string of the system when the program starts, and then add its string value to the combo box control, so it is more convenient, but the code to write more relevant content, through Baidu, find the response code. Its operating effect is as follows, my computer currently only two virtual serial port COM5 and COM6
The specific code is as follows
1BOOL Cmfc_uart20160921dlg::checkcomport (void)2 {3 LongLreg;4 HKEY HKEY;5 DWORD maxvaluelength;6 DWORD Dwvaluenumber;7Lreg = RegOpenKeyEx (HKEY_LOCAL_MACHINE, TEXT ("Hardware\\devicemap\\serialcomm"),8 0, Key_query_value, &HKey);9 if(Lreg! = ERROR_SUCCESS)//when successful, return to Error_success,Ten { OneAfxMessageBox (TEXT ("Open Registry error!\n")); A returnFALSE; - } - theLreg =RegQueryInfoKey (HKey, NULL, NULL, NULL , NULL, NULL, NULL, -&dwvaluenumber, &maxvaluelength, NULL, NULL, NULL); - - if(Lreg! = ERROR_SUCCESS)//not successful + { -AfxMessageBox (TEXT ("Getting Info error!\n")); + returnFALSE; A } at -TCHAR *pvaluename, *Pcomnumber; -DWORD Cchvaluename, dwvaluesize =Ten; - - for(inti =0; i < Dwvaluenumber; i++) - { inCchvaluename = Maxvaluelength +1; -Dwvaluesize =Ten; toPvaluename = (tchar*) VirtualAlloc (NULL, Cchvaluename, Mem_commit, page_readwrite); +Lreg =RegEnumValue (HKey, I, Pvaluename, -&cchvaluename, null , NULL, NULL, or NULL); the * if((Lreg! = ERROR_SUCCESS) && (Lreg! =error_no_more_items)) $ {Panax NotoginsengAfxMessageBox (TEXT ("Enum Registry Error or No more items!\n")); - returnFALSE; the } + APcomnumber = (tchar*) VirtualAlloc (NULL,6, Mem_commit, page_readwrite); theLreg =RegQueryValueEx (HKey, Pvaluename, NULL, +NULL, (LPBYTE) Pcomnumber, &dwvaluesize); - $ if(Lreg! =error_success) $ { -AfxMessageBox (TEXT ("Can not get the name of the the port")); - returnFALSE; the } - Wuyi CString str (pcomnumber); the //m_combocomcontrol.addstring (str);//add the obtained value to the Combox control - WuVirtualFree (Pvaluename,0, mem_release); -VirtualFree (Pcomnumber,0, mem_release); About } $ - returnTRUE; - -}
Finally we just execute the above function in the initialization code and then let combo box choose the first serial port by default.
Checkcomport ();
M_combocomcontrol.setcursel (0);//Let the control control of combo box select the first serial number
The first option is to have combo box associate a CString variable m_combocom, and then add the following code to the Initialize function OnInitDialog ()
1M_combocom.addstring (_t ("COM1"));2M_combocom.addstring (_t ("COM2"));3M_combocom.addstring (_t ("COM3"));4M_combocom.addstring (_t ("COM4"));5M_combocom.addstring (_t ("COM5"));6M_combocom.addstring (_t ("COM6"));7M_combocom.addstring (_t ("COM7"));8M_combocom.addstring (_t ("COM8"));9M_combocom.addstring (_t ("COM9"));TenM_combocom.addstring (_t ("COM10")); One //m_combocom.addstring (_t ("COM11")); AM_combocom.setcursel (0);
Or use the property features of the control, as shown in
Visual Studio C + + MFC application automatically probes for string numbers