// AdapterPropertyMgr.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <UserUtilLib.hpp>
#include <atlconv.h>
#include <Sddl.h>
#include <iostream>
using namespace std;
BOOL EnableChangeAdapterProperty(BOOL bEnable);
BOOL EnableChangeAdapterPropertyForXP(BOOL bEnable);
BOOL EnableChangeAdapterPropertyForVista(BOOL bEnable);
BOOL GetCurrentUserSID(TCHAR* szSID);
BOOL EnableChangeAdapterProperty(BOOL bEnable)
{
int nWinVer = UserUtilLib::Win32Helper::GetWindowsVersion();
BOOL bDataAvailable = FALSE;
if (nWinVer <= WINDOWS_VERSION_XP)
bDataAvailable = EnableChangeAdapterPropertyForXP(bEnable);
else
bDataAvailable = EnableChangeAdapterPropertyForVista(bEnable);
return bDataAvailable;
}
BOOL EnableChangeAdapterPropertyForXP(BOOL bEnable)
{
HKEY hKey;
DWORD dwLen = 0, dwDisposition = 0;
DWORD dwValue = 0;
TCHAR szCurrentUserSID[MAX_PATH] = {0};
TCHAR szKey[MAX_PATH] = {0};
BOOL bDataAvailable = FALSE;
if (!GetCurrentUserSID(szCurrentUserSID))
{
return FALSE;
}
_tcscat_s(szKey, _countof(szKey), szCurrentUserSID);
_tcscat_s(szKey, _countof(szKey)- _tcslen(szCurrentUserSID), _T("\\SOFTWARE\\Policies\\Microsoft\\Windows\\Network Connections"));
LONG lRes = RegCreateKeyEx(HKEY_USERS, szKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_READ | KEY_WRITE | KEY_WOW64_64KEY, NULL, &hKey, &dwDisposition);
if (lRes != ERROR_SUCCESS)
{
return FALSE;
}
dwValue = 1;
lRes = RegSetValueEx(hKey, _T("NC_EnableAdminProhibits"), 0, REG_DWORD, (LPBYTE)&dwValue, sizeof(dwValue));
if (lRes != ERROR_SUCCESS)
{
return FALSE;
}
dwLen = sizeof(DWORD);
lRes = RegQueryValueEx(hKey, _T("NC_LanProperties"), NULL, NULL, (LPBYTE)&dwValue, &dwLen);
if (lRes == ERROR_SUCCESS)
{
if (dwValue == 1 && !bEnable)
{
bDataAvailable = TRUE;
dwValue = 0;
RegSetValueEx(hKey, _T("NC_LanProperties"), 0, REG_DWORD, (LPBYTE)&dwValue, sizeof(dwValue));
}
else if (dwValue == 0 && bEnable)
{
bDataAvailable = TRUE;
dwValue = 1;
RegSetValueEx(hKey, _T("NC_LanProperties"), 0, REG_DWORD, (LPBYTE)&dwValue, sizeof(dwValue));
}
}
else
{
if (!bEnable)
bDataAvailable = TRUE;
dwValue = bEnable ? 1 : 0;
RegSetValueEx(hKey, _T("NC_LanProperties"), 0, REG_DWORD, (LPBYTE)&dwValue, sizeof(dwValue));
}
if (bDataAvailable)
{
dwValue = bEnable ? 1 : 0;
RegSetValueEx(hKey, _T("NC_LanConnect"), 0, REG_DWORD, (LPBYTE)&dwValue, sizeof(dwValue));
RegSetValueEx(hKey, _T("NC_RenameConnection"), 0, REG_DWORD, (LPBYTE)&dwValue, sizeof(dwValue));
RegSetValueEx(hKey, _T("NC_LanChangeProperties"), 0, REG_DWORD, (LPBYTE)&dwValue, sizeof(dwValue));
}
RegCloseKey(hKey);
return bDataAvailable;
}
BOOL EnableChangeAdapterPropertyForVista(BOOL bEnable)
{
HKEY hKey, hSubKey;
DWORD dwIndex = 0, dwType;
TCHAR szBuf[512] = {0};
TCHAR szSubKey[512] = {0};
TCHAR szValue[512] = {0};
DWORD dwLen = sizeof(szBuf);
FILETIME ftLastWriteTime;
BOOL bDataAvailable = FALSE;
TCHAR* szKey[] = {_T("SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}"),
_T("SYSTEM\\ControlSet001\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}")};
for (int i = 0; i < 2; i++)
{
LONG lRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKey[i], 0, KEY_ENUMERATE_SUB_KEYS | KEY_READ | KEY_WRITE, &hKey);
if (lRes != ERROR_SUCCESS)
{
return FALSE;
}
dwIndex = 0;
do
{
ZeroMemory(szBuf, sizeof(szBuf));
dwLen = sizeof(szBuf);
lRes = RegEnumKeyEx(hKey, dwIndex, szBuf, &dwLen, NULL, NULL, NULL,&ftLastWriteTime);
dwIndex++;
if (lRes != ERROR_SUCCESS)
break;
if (_tcsicmp(szBuf, _T("Descriptions")) == 0)
continue;
ZeroMemory(szSubKey, sizeof(szSubKey));
_snwprintf_s(szSubKey, 512, 511, L"%s%s", szBuf, _T("\\Connection"));
if (ERROR_SUCCESS != RegOpenKeyEx(hKey, szSubKey, 0, KEY_READ | KEY_WRITE, &hSubKey))
continue;
if (bEnable)
{
dwLen = sizeof(szValue);
ZeroMemory(szValue, sizeof(szValue));
lRes = RegQueryValueEx(hSubKey, _T("Name"), NULL, &dwType, (LPBYTE)szValue, &dwLen);
if (lRes != ERROR_SUCCESS)
{
dwLen = sizeof(szValue);
ZeroMemory(szValue, sizeof(szValue));
lRes = RegQueryValueEx(hSubKey, _T("NameCopy"), NULL, &dwType, (LPBYTE)szValue, &dwLen);
if (ERROR_SUCCESS == lRes)
{
bDataAvailable = TRUE;
RegSetValueEx(hSubKey, _T("Name"), NULL, REG_SZ, (LPBYTE)szValue, dwLen);
}
else
{
}
}
}
else
{
dwLen = sizeof(szValue);
ZeroMemory(szValue, sizeof(szValue));
lRes = RegQueryValueEx(hSubKey, _T("Name"), NULL, &dwType, (LPBYTE)szValue, &dwLen);
if (lRes == ERROR_SUCCESS)
{
RegSetValueEx(hSubKey, _T("NameCopy"), NULL, REG_SZ, (LPBYTE)szValue, dwLen);
RegDeleteValue(hSubKey, _T("Name"));
bDataAvailable = TRUE;
}
}
RegCloseKey(hSubKey);
} while (true);
RegCloseKey(hKey);
}
return bDataAvailable;
}
BOOL GetCurrentUserSID(TCHAR* szSID)
{
USES_CONVERSION;
TCHAR szSSID[MAX_PATH] = {0};
std::vector<DWORD> vecProcessID;
DWORD dwProcessID = UserUtilLib::Process::GetProcessIDFromName("explorer.exe", vecProcessID);//GetProcessIDFromName("explorer.exe");
if( dwProcessID == 0 )
{
return FALSE;
}
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwProcessID);
if( hProcess == NULL )
{
return FALSE;
}
HANDLE hToken;
if( !OpenProcessToken(hProcess, TOKEN_ALL_ACCESS, &hToken) )
{
CloseHandle(hProcess);
return FALSE;
}
DWORD dwLength;
TOKEN_USER* account = NULL;
if (GetTokenInformation(hToken, TokenUser, NULL, 0, &dwLength) == FALSE &&
GetLastError() != ERROR_INSUFFICIENT_BUFFER)
{
CloseHandle(hToken);
CloseHandle(hProcess);
return FALSE;
}
account = (TOKEN_USER*)malloc(dwLength);
if (NULL == account)
{
CloseHandle(hToken);
CloseHandle(hProcess);
return FALSE;
}
if (GetTokenInformation(hToken, TokenUser, (LPVOID)account, dwLength, &dwLength))
{
TCHAR* szSIDTemp= NULL;
if (ConvertSidToStringSid(account->User.Sid, &szSIDTemp))
{
//strcpy(szSSID, lpszSid);
_tcscpy(szSID, szSIDTemp);
LocalFree(szSIDTemp);
}
}
CloseHandle(hToken);
CloseHandle(hProcess);
free(account);
return (0 == szSID[0] ? FALSE : TRUE);
}
int _tmain(int argc, _TCHAR* argv[])
{
std::cout<<"\n请输入 0 或 1 设置网卡修改属性(按Enter确认):\n\n0 - 禁用修改网卡属性 \n\n1 - 启用修改网卡属性"<<std::endl;
std::cout<<"\n>>";
char c;
std::cin>>c;
BOOL bEnable = FALSE;
if (c=='1')
{
bEnable = TRUE;
}
else if (c=='0')
{
bEnable = FALSE;
}
EnableChangeAdapterProperty(bEnable);
std::cout<<"\n设置成功!"<<std::endl;
std::cin>>c;
//Sleep(5000);
return 0;
}
设置网卡修改属性
最新推荐文章于 2024-08-22 09:47:16 发布