VC ++ enables or disables USB devices

Source: Internet
Author: User
  1. # Include <windows. h>
  2. # Include <tchar. h>
  3. # Include <setupapi. h>
  4. // # Include <shlwapi. h>
  5. # Pragma comment (Lib, "setupapi. lib ")
  6. // # Pragma comment (Lib, "shlwapi. lib ")
  7. Hdevinfo = NULL;
  8. // Error message
  9. VoidFormatmsg (DWORDDwerror,Lptstr* O)
  10. {
  11. LptstrOO = (Lptstr) O;
  12. BoolBok = formatmessage (format_message_from_system | format_message_allocate_buffer, null, dwerror,
  13. Makelangid (lang_chinese, sublang_chinese_simplified), Oo, 0, null );
  14. If(! Bok)// Change ** to * 2007/01/10
  15. {// The newly created string is directed by oo.
  16. HmoduleHdll = LoadLibraryEx (_ T ("Netmsg. dll"), Null, dont_resolve_dll_references );
  17. If(Null! = Hdll)
  18. {
  19. Formatmessage (format_message_from_hmodule |
  20. Format_message_from_system, hdll,
  21. Dwerror,
  22. Makelangid (lang_chinese, sublang_chinese_simplified ),
  23. (Lptstr) O, 0, null );
  24. Freelibrary (hdll );
  25. }
  26. }
  27. }
  28. BoolChangestatus (DWORDNewstatus,DWORDSelecteditem, hdevinfo)
  29. {
  30. LptstrLpszmsg = NULL;
  31. HcursorHcursor = NULL;
  32. Try
  33. {
  34. Sp_propchange_params propchangeparams = {Sizeof(Sp_classinstall_header )};
  35. Sp_devinfo_data deviceinfodata = {Sizeof(Sp_devinfo_data )};
  36. Hcursor = setcursor (loadcursor (null, idc_wait ));
  37. // Get a handle to the selected item.
  38. If(! Setupdienumdeviceinfo (hdevinfo, selecteditem, & deviceinfodata ))
  39. {
  40. Formatmsg (getlasterror (), & lpszmsg );
  41. ThrowLpszmsg;
  42. }
  43. // Set the propchangeparams structure.
  44. Propchangeparams. classinstallheader. installfunction = dif_propertychange;
  45. Propchangeparams. Scope = dics_flag_global;
  46. Propchangeparams. statechange = newstatus;
  47. If(! Setupdisetclassinstallparams (hdevinfo, & deviceinfodata, (sp_classinstall_header *) & propchangeparams,
  48. Sizeof(Propchangeparams )))
  49. {
  50. Formatmsg (getlasterror (), & lpszmsg );
  51. ThrowLpszmsg;
  52. }
  53. // Call the classinstaller and perform the change.
  54. If(! Setupdicallclassinstaller (dif_propertychange, hdevinfo, & deviceinfodata ))
  55. {
  56. Formatmsg (getlasterror (), & lpszmsg );
  57. ThrowLpszmsg;
  58. }
  59. Setcursor (hcursor );
  60. ReturnTrue;
  61. }
  62. Catch(Tchar* Pszerror)
  63. {
  64. Setcursor (hcursor );
  65. : MessageBox (null, pszerror, _ T ("Prompt"), Mb_ OK );
  66. If(Null! = Lpszmsg)
  67. {
  68. Localfree ((Hlocal) Lpszmsg );
  69. }
  70. ReturnFalse;
  71. }
  72. }
  73. // This function is mainly used to enable and disable these devices.
  74. // Parameter description, nstatus can have three values,-1: Nothing
  75. // 0 disable the device, 1 enable the device.
  76. // Nindex is used to control whether to enable or disable the optical drive or USB drive.
  77. // Three values are also recommended. 0 indicates the soft drive, and 1 indicates the optical drive.
  78. // 2 stands for USB
  79. // You can call this function to disable the optical drive.
  80. // Controldisk (0, 1 );
  81. BoolControldisk (IntNstatus,IntNindex)
  82. {
  83. If(-1 = nstatus)
  84. {
  85. ReturnFalse;
  86. }
  87. LptstrLpszmsg = NULL;
  88. Try
  89. {
  90. Tchar* Guidstring = NULL;
  91. Guid;
  92. Zeromemory (& guid,Sizeof(Guid ));
  93. Switch(Nindex)
  94. {
  95. Case0:// 0 indicates the soft drive
  96. Guidstring = _ T ("4d36e980-e325-11ce-bfc1-08002be10318");
  97. Uuidfromstring (unsignedChar*) Guidstring, & guid );
  98. Break;
  99. Case1:// 1 indicates the optical drive
  100. Guidstring = _ T ("4d36e965-e325-11ce-bfc1-08002be10318");
  101. Uuidfromstring (unsignedChar*) Guidstring, & guid );
  102. Break;
  103. Case2:// 2 stands for USB
  104. Guidstring = _ T ("36fc9e60-c465-11cf-8056-444553540000");
  105. Uuidfromstring (unsignedChar*) Guidstring, & guid );
  106. Break;
  107. }
  108. Hdevinfo = setupdigetclassdevs (& guid, null, null, digcf_present );
  109. If(Invalid_handle_value! = Hdevinfo)
  110. {Lpszmsg ="Hahazx";
  111. Formatmsg (getlasterror (), & lpszmsg );
  112. ThrowLpszmsg;
  113. }
  114. DWORDI;
  115. Sp_devinfo_data deviceinfodata;
  116. Zeromemory (& deviceinfodata,Sizeof(Sp_devinfo_data ));
  117. Deviceinfodata. cbsize =Sizeof(Sp_devinfo_data );
  118. For(I = 0; setupdienumdeviceinfo (hdevinfo, I, & deviceinfodata); ++ I)
  119. {
  120. If(1 = nstatus)
  121. {: MessageBox (null,"Enable","Correct", Mb_ OK );
  122. Changestatus (dics_enable, I, hdevinfo );
  123. }
  124. Else If(0 = nstatus)
  125. {: MessageBox (null,"Disable","Error", Mb_ OK );
  126. Changestatus (dics_disable, I, hdevinfo );
  127. }
  128. }
  129. // Release device information set
  130. ReturnSetupdidestroydeviceinfolist (hdevinfo );
  131. }
  132. Catch(Tchar* Pszerror)
  133. {
  134. : MessageBox (null, pszerror, _ T ("Prompt"), Mb_ OK );
  135. If(Null! = Lpszmsg)
  136. {
  137. Localfree ((Hlocal) Lpszmsg );
  138. }
  139. ReturnFalse;
  140. }
  141. ReturnFalse;
  142. }
  143. VoidMain ()
  144. {
  145. Controldisk (1, 2 );
  146. }

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.