[C++] 纯文本查看 复制代码
// UProtector.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stdlib.h>
#include <windows.h>
#include <winreg.h>
int protecter(DWORD dw, HKEY hKey)
{
HKEY hSubKey;
RegCreateKeyEx(hKey, _T("StorageDevicePolicies"), 0, NULL, NULL, KEY_READ | KEY_WRITE, NULL, &hSubKey, NULL);
if (ERROR_SUCCESS != RegSetValueEx(hSubKey, _T("WriteProtect"), NULL, REG_DWORD, (const byte *)&dw, sizeof(DWORD)))
return 0;
else
return 1;
}
int _tmain(int argc, _TCHAR* argv[])
{
int flag = 0;
DWORD lck;
if (argc != 2 || (_tstoi(argv[1]) != 0 && _tstoi(argv[1]) != 1) ) {
_tprintf(_T("Usage: DiskLock.exe 0 | 1\n"));
exit (EXIT_FAILURE);
}
HKEY hSDP;
if ( RegOpenKeyEx ( HKEY_LOCAL_MACHINE,
_T("SYSTEM\\CurrentControlSet\\Control"),
0, KEY_READ | KEY_WRITE, &hSDP
) == ERROR_SUCCESS )
{
lck = _tstoi(argv[1]);
flag = protecter(lck, hSDP);
if(flag && lck == 1) {
_tprintf(_T("Lock disk Successful!\n"));
exit(EXIT_SUCCESS);
}
else if (flag && lck == 0) {
_tprintf(_T("Unlock disk Successful!\n"));
exit(EXIT_SUCCESS);
}
if (flag == 0 && lck == 1) {
_tprintf(_T("Lock disk Failure!\n"));
exit(EXIT_FAILURE);
}
else if (flag == 0 && lck == 0) {
_tprintf (_T("Unlock disk failure!\n"));
exit(EXIT_FAILURE);
}
}
else {
_tprintf(_T("Can not access to registry key\n"));
exit(EXIT_FAILURE);
}
return 0;
}