Ideas:
The serial port is used, and other programs cannot use the serial port;
Enable serial port: Disable the enabled serial port so that other programs can use the serial port.
First, you must obtain the number of serial ports in the device.
Void enumcom () <br/>{< br/> hkey; <br/> long lresult; <br/> tchar szkeyname [64]; <br/> regopenkeyex (HKEY_LOCAL_MACHINE, text ("drivers // builtin"), 0, 0, & hkey); <br/> int I = 0; <br/> DO <br/> {<br/> DWORD dwkeynamelength = max_path; <br/> lresult = regenumkeyex (hkey, I, szkeyname, & dwkeynamelength, 0, null, null, null); <br/> If (lresult! = Error_success) <br/>{< br/> break; <br/>}< br/> If (! Wcsncmp (text ("serial"), szkeyname, 6 )) <br/>{< br/> tchar szcomkey [max_path] = text ("drivers // builtin //"); <br/> wcscat (szcomkey, szkeyname ); </P> <p> hkey hcomkey; <br/> regopenkeyex (HKEY_LOCAL_MACHINE, szcomkey, 0, 0, & hcomkey); </P> <p> DWORD keytype; <br/> DWORD dwlength = 20; <br/> tchar Buf [64]; <br/> regqueryvalueex (hcomkey, text ("Port"), 0, & keytype, (byte *) BUF, & dwlength); <br/> // comarray [comnum] = wide2short (Bu F); <br/> strcpy (comarray [ncomcouont], wide2short (BUF); <br/> // listbox_addstring (getdlgitem (hdlg, idc_list1), Buf ); <br/> regclosekey (hcomkey); <br/> ncomcouont ++; <br/>}< br/> I ++; </P> <p >}while (lresult! = Error_no_more_items); <br/> regclosekey (hkey); <br/>}
Disable Serial Port:
Void ccomctrldlg: onbnclickedforbidcom () <br/>{< br/> // todo: add the control notification handler Code <br/> // enumeration serial port <br/> If (ncomcouont = 0) <br/> enumcom (); <br/> If (ncomcouont> 0) <br/>{< br/> hcomlist = new handle [ncomcouont]; <br/> zeromemory (hcomlist, (sizeof (handle) * ncomcouont); <br/>}< br/> int nopencom = 0; <br/> for (INT I = 0; I <ncomcouont; I ++) <br/>{< br/> hcomlist [I] = createfile (short2wide (comarray [I]), generic_read | Generic_write, 0, null, open_existing, 0, null); <br/> mtchar MSG = {0}; <br/> wcscpy (MSG, l "Disable serial port :"); <br/> wchar_t * wname = short2wide (comarray [I]); <br/> wcscat (MSG, wname ); <br/> If (hcomlist [I] = invalid_handle_value) <br/>{< br/> wcscat (MSG, l "failed! "); <Br/>}< br/> else <br/>{< br/> nopencom ++; <br/> wcscat (MSG, l" successful! "); <Br/>}< br/>: MessageBox (null, MSG, l" notice ", mb_ OK ); <br/>}< br/> If (nopencom = ncomcouont) <br/>: MessageBox (null, l "All serial ports are disabled successfully", l "notice ", mb_ OK); </P> <p >}< br/>
Enable serial port: Disable the open serial port
Void ccomctrldlg: onbnclickedrestartcom () <br/>{< br/> // todo: add the control notification handler code here <br/> If (ncomcouont = 0) <br/> enumcom (); <br/> int nclose = 0; <br/> for (INT I = 0; I <ncomcouont; I ++) <br/>{< br/> mtchar MSG = {0}; <br/> wcscpy (MSG, l "enable serial port :"); <br/> wchar_t * wname = short2wide (comarray [I]); <br/> wcscat (MSG, wname); <br/> If (hcomlist [I]! = Invalid_handle_value) <br/>{< br/> closehandle (hcomlist [I]); <br/> hcomlist [I] = NULL; <br/> nclose ++; <br/> wcscat (MSG, l "successful! "); <Br/>}< br/> else <br/>{< br/> wcscat (MSG, l" failed! "); <Br/>}< br/>: MessageBox (null, MSG, l" notice ", mb_ OK ); <br/>}< br/> If (nclose = ncomcouont) <br/>: MessageBox (null, l "start all serial ports successfully", l "notice ", mb_ OK); <br/>}