- #include <WINDOWS.H>
- #include <TCHAR.H>
- #include <SETUPAPI.H>
-
- #pragma comment(lib, "setupapi.lib")
-
- HDEVINFO hDevInfo = NULL;
-
- void FormatMSG(DWORD dwError, LPTSTR * o)
- {
- LPTSTR oo=(LPTSTR)o;
- BOOL bOk = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, dwError,
- MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), oo, 0, NULL);
- if(!bOk)
- {
- HMODULE hDll = LoadLibraryEx(_T("netmsg.dll"),NULL,DONT_RESOLVE_DLL_REFERENCES);
- if(NULL != hDll)
- {
- FormatMessage(FORMAT_MESSAGE_FROM_HMODULE |
- FORMAT_MESSAGE_FROM_SYSTEM,hDll,
- dwError,
- MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED),
- (LPTSTR)o,0, NULL);
- FreeLibrary(hDll);
- }
- }
- }
- BOOL ChangeStatus(DWORD NewStatus, DWORD SelectedItem, HDEVINFO hDevInfo)
- {
- LPTSTR lpszMsg = NULL;
- HCURSOR hCursor = NULL;
- try
- {
- SP_PROPCHANGE_PARAMS PropChangeParams = {sizeof(SP_CLASSINSTALL_HEADER)};
- SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
- hCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
-
- if (!SetupDiEnumDeviceInfo(hDevInfo, SelectedItem, &DeviceInfoData))
- {
- FormatMSG(GetLastError(), &lpszMsg);
- throw lpszMsg;
- }
-
- PropChangeParams.ClassInstallHeader.InstallFunction = DIF_PROPERTYCHANGE;
- PropChangeParams.Scope = DICS_FLAG_GLOBAL;
- PropChangeParams.StateChange = NewStatus;
- if (!SetupDiSetClassInstallParams(hDevInfo, &DeviceInfoData, (SP_CLASSINSTALL_HEADER *)&PropChangeParams,
- sizeof(PropChangeParams)))
- {
- FormatMSG(GetLastError(), &lpszMsg);
- throw lpszMsg;
- }
-
-
- if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,hDevInfo,&DeviceInfoData))
- {
- FormatMSG(GetLastError(), &lpszMsg);
- throw lpszMsg;
- }
- SetCursor(hCursor);
- return TRUE;
- }
- catch (TCHAR * pszError)
- {
- SetCursor(hCursor);
- ::MessageBox(NULL,pszError,_T("提示"),MB_OK);
- if (NULL != lpszMsg)
- {
- LocalFree((HLOCAL)lpszMsg);
- }
- return FALSE;
- }
- }
-
-
-
-
-
-
-
-
-
- BOOL ControlDisk(int nStatus, int nIndex)
- {
- if (-1 == nStatus)
- {
- return FALSE;
- }
- LPTSTR lpszMsg = NULL;
- try
- {
- TCHAR * GUIDString = NULL;
- GUID guid;
- ZeroMemory(&guid, sizeof(GUID));
- switch(nIndex)
- {
- case 0:
- GUIDString = _T("4D36E980-E325-11CE-BFC1-08002BE10318");
- UuidFromString((unsigned char *)GUIDString, &guid);
- break;
- case 1:
- GUIDString = _T("4D36E965-E325-11CE-BFC1-08002BE10318");
- UuidFromString((unsigned char *)GUIDString, &guid);
- break;
- case 2:
- GUIDString = _T("36FC9E60-C465-11CF-8056-444553540000");
- UuidFromString((unsigned char *)GUIDString, &guid);
- break;
- }
- hDevInfo = SetupDiGetClassDevs(&guid,NULL,NULL,DIGCF_PRESENT);
- if (INVALID_HANDLE_VALUE != hDevInfo)
- {lpszMsg="hahazx";
- FormatMSG(GetLastError(), &lpszMsg);
-
- throw lpszMsg;
- }
- DWORD i;
- SP_DEVINFO_DATA DeviceInfoData;
- ZeroMemory(&DeviceInfoData, sizeof(SP_DEVINFO_DATA));
- DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
- for (i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); ++i)
- {
- if (1 == nStatus)
- {::MessageBox(NULL,"启用","correct",MB_OK);
- ChangeStatus(DICS_ENABLE, i, hDevInfo);
- }
- else if (0 == nStatus)
- {::MessageBox(NULL,"禁用","error",MB_OK);
- ChangeStatus(DICS_DISABLE, i, hDevInfo);
- }
- }
-
- return SetupDiDestroyDeviceInfoList(hDevInfo);
- }
- catch (TCHAR * pszError)
- {
- ::MessageBox(NULL,pszError,_T("提示"),MB_OK);
- if (NULL != lpszMsg)
- {
- LocalFree((HLOCAL)lpszMsg);
- }
- return FALSE;
- }
- return FALSE;
- }
- void main()
- {
- ControlDisk(1, 2);
- }
posted on
2009-06-05 14:15
Yincheng
阅读(
332)
评论()
编辑
收藏
举报