Method for C++ to get system activity, available serial port and name
This article mainly explains "C++ how to get system activities, available serial ports and names". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how C++ can get system activities, available serial ports and names.
CreateFile method:
Void GetComList_256 (CComboBox * CCombox) / / get the available com ports to support up to 256
{
CString strCom,strComOpen
Int nCom = 0
Int count = 0
HANDLE hCom
CCombox- > ResetContent ()
Do
{
NCom++
StrCom.Format ("COM%d", nCom)
StrComOpen.Format ("\\.\\ COM%d", nCom)
HCom = CreateFile (strComOpen, GENERIC_READ | GENERIC_WRITE, 0, NULL
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0)
If (INVALID_HANDLE_VALUE = = hCom)
{
DWORD error=::GetLastError (); / / get the error message
}
Else
{
CCombox- > AddString (strCom)
Count++
}
CloseHandle (hCom)
} while (nComSetCurSel (0)
}
*
How to read the registry:
Int GetComList_Reg (CComboBox * CCombox)
{
HKEY hkey
Int result
Int I = 0
CString strComName;// serial port name
Detailed name of CString strDrName;// serial port
Result = RegOpenKeyEx (HKEY_LOCAL_MACHINE
_ T ("Hardware\\ DeviceMap\\ SerialComm")
NULL
KEY_READ
& hkey)
If (ERROR_SUCCESS = = result) / / Open the serial port registry
{
TCHAR portName [0x100], commName [0x100]
DWORD dwLong, dwSize
CCombox- > ResetContent ()
Do
{
DwSize = sizeof (portName) / sizeof (TCHAR)
DwLong = dwSize
Result = RegEnumValue (hkey, I, portName, & dwLong, NULL, NULL, (LPBYTE) commName, & dwSize)
If (ERROR_NO_MORE_ITEMS = = result)
{
/ / enumerate serial ports
Break; / / commName is the serial port name "COM2"
}
StrComName=commName
StrDrName = portName
/ / find the first'\ 'from right to left and get the length of the string on the left
Int len = strDrName.ReverseFind ('\')
/ / get the string to the left of'\'
CString strFilePath = strDrName.Left (len+1)
/ / get the string to the right of'\'
CString fileName = strDrName.Right (strDrName.GetLength ()-len-1)
FileName = strComName + _ T (":") + fileName
CCombox- > AddString (fileName)
ITunes +
} while (1)
RegCloseKey (hkey)
}
CCombox- > SetCurSel (0)
Return i
}
After reading, it can also be added to the combo box, and can be added to the data as needed.
At this point, I believe that "C++ system activities, available serial ports and names of the method" have a deeper understanding, you might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!