使用Win Api操纵注册表,查询,添加,删除,修改,读取mac地址
1. 打开注册表
RegOpenKey(key, subkey, reserved,sam)
RegOpenKeyEx(key,subkey, reserved,sam)
两个函数的参数一样。参数含义如下:
Key:必须为表1中列出的项。
SubKey:要打开的子项。
Reserved:必须为0。
lSam:对打开的子项进行的操作,包括win32con.KEY_ALL_ACCESS、win32con.KEY_READ、win32con.KEY_WRITE等
2. 关闭注册表
RegCloseKey(key)
其参数只有一个,其含义如下:
lKey:已经打开的注册表项的句柄。
3. 读取项值
RegQueryValue(key,subKey) 读取项的默认值
RegQueryValueEx(key,valueName) 读取某一项值
对于RegQueryValue,其参数含义如下:
Key:已打开的注册表项的句柄。
subKey:要操作的子项。
对于RegQueryValueEx,其参数含义如下:
Key:已经打开的注册表项的句柄。
valueName:要读取的项值名称。
4. 设置项值
RegSetValueEx(key,valueName,reserved,type,value) 要修改或重新设置注册表某一项的项值。如果项值存在,则修改该项值,如果不存在,则添加该项值。
RegSetValue(key,subKey,type,value) 设置项的默认值
对于RegSetValueEx,其参数含义如下:
Key:要设置的项的句柄。
valueName:要设置的项值名称。
Reserved:保留,可以设为0。
Type:项值的类型。
lValue:所要设置的值。
对于RegSetValue,其参数含义如下:
Key:已经打开的项的句柄。
subKey:所要设置的子项。
Type:项值的类型,必须为win32con.REG_SZ。
Value:项值数据,为字符串。
5. 添加、删除项
RegCreateKey(key,subKey) 向注册表中添加项
RegDeleteKey(key,subKey) 删除注册表中的项
其参数含义相同,参数含义分别如下:
Key:已经打开的注册表项的句柄。
subKey:所要操作(添加或删除)的子项。
6. 获取本机主机MAC地址
本地网卡的MAC地址可以通过ARP协议获得,也可以通过API查询获得,IP Helper API中还有一组管理网络适配器的API,其中有一个可以获得网卡相关信息。
DWOED GetAdaptersInfo(
PIP_ADAPTER_INFO pAdapterInfo;
PULONG pOutBufLen;
);
两个参数分别是:存储网卡信息的缓冲区指针和缓冲区大小。
创建注册表项实例:
#include <windows.h>
#include <stdio.h>
void showErrorText(DWORD error_num);
int main()
{
HKEY hKey;
HKEY subKey;
DWORD result;
char sname[] = "我是最棒的"; // 写入想写的内容
//打开注册表, HKEY_CURRENT_USER\Software
result = RegOpenKeyEx(
HKEY_CURRENT_USER, "Software", // 打开
0, // 保留参数必须填 0
KEY_WRITE, // 打开权限,写入
&hKey // 打开之后的句柄
);
if (result == ERROR_SUCCESS)
{
printf("open success!\n");
}
else
{
printf("open failed!\n");
showErrorText(result);
}
// 添加注册表项, 即 HKEY_CURRENT_USER\Software\AnywayCompany
RegCreateKey(hKey, "AnywayCompany", &subKey);
// 设置 HKEY_CURRENT_USER\Software\AnywayCompany\Name 的值
result = RegSetValueEx(
subKey,
"Name", // Name字段
0, // 保留参数必须填 0
REG_SZ, // 键值类型为字符串
(const unsigned char*)sname, // 字符串首地址
sizeof(sname) // 字符串长度
);
if (result == ERROR_SUCCESS)
{
printf("set success!\n");
}
else
{
printf("set failed!\n");
showErrorText(result);
}
//关闭注册表:
RegCloseKey(hKey);
// 暂停
system("pause");
return 0;
}
/*
* 根据错误码输出错误信息
*/
void showErrorText(DWORD error_num)
{
char* msg = NULL;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
error_num,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // 使用默认语言
(LPSTR)&msg,
0,
NULL
);
printf("Error code %d: ", error_num);
if (msg == NULL)
printf("%s\n", "Unknown error");
else
printf("%s\n", msg);
}
结果:
在这一环节忘记截图了(要在注册表编辑器里找,路径:HKEY_CURRENT_USER\Software\AnywayCompany)
删除注册表项值的实例:
#include <windows.h>
#include <stdio.h>
void showErrorText(DWORD error_num);
int main()
{
HKEY hKey;
HKEY subKey;
DWORD result;
char sname[] = " 我是最棒的";
//打开注册表
result = RegOpenKeyEx(
// 打开 HKEY_CURRENT_USER\Software\AnywayCompany
HKEY_CURRENT_USER, "Software\\AnywayCompany",
0, // 保留参数必须填 0
KEY_WRITE, // 打开权限,写入
&hKey // 打开之后的句柄
);
if (result == ERROR_SUCCESS)
{
printf("open success!\n");
}
else
{
printf("open failed!\n");
showErrorText(result);
}
// 删除 HKEY_CURRENT_USER\Software\AnywayCompany 下的 Name
result = RegDeleteValue(hKey, "Name");
if (result == ERROR_SUCCESS)
{
printf("delete success!\n");
}
else
{
printf("delete failed!\n");
showErrorText(result);
}
//关闭注册表:
RegCloseKey(hKey);
// 暂停
system("pause");
return 0;
}
/*
* 根据错误码输出错误信息
*/
void showErrorText(DWORD error_num)
{
char* msg = NULL;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
error_num,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // 使用默认语言
(LPSTR)&msg,
0,
NULL
);
printf("Error code %d: ", error_num);
if (msg == NULL)
printf("%s\n", "Unknown error");
else
printf("%s\n", msg);
}
结果:
删除注册表项键的实例:
#include <windows.h>
#include <stdio.h>
void showErrorText(DWORD error_num);
int main()
{
HKEY hKey;
HKEY subKey;
DWORD result;
char sname[] = "我是最棒的";
//打开注册表
result = RegOpenKeyEx(
// 打开 HKEY_CURRENT_USER\Software\AnywayCompany
HKEY_CURRENT_USER, "Software",
0, // 保留参数必须填 0
KEY_WRITE, // 打开权限,写入
&hKey // 打开之后的句柄
);
if (result == ERROR_SUCCESS)
{
printf("open success!\n");
}
else
{
printf("open failed!\n");
showErrorText(result);
}
// 删除 HKEY_CURRENT_USER\Software\AnywayCompany
result = RegDeleteKey(hKey, "AnywayCompany");
if (result == ERROR_SUCCESS)
{
printf("delete success!\n");
}
else
{
printf("delete failed!\n");
showErrorText(result);
}
//关闭注册表:
RegCloseKey(hKey);
// 暂停
system("pause");
return 0;
}
/*
* 根据错误码输出错误信息
*/
void showErrorText(DWORD error_num)
{
char* msg = NULL;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
error_num,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // 使用默认语言
(LPSTR)&msg,
0,
NULL
);
printf("Error code %d: ", error_num);
if (msg == NULL)
printf("%s\n", "Unknown error");
else
printf("%s\n", msg);
}
结果:
这里是我没有重新打开注册表编辑器的原因,如果重新打开就会发现这个文件夹不见了,已经删除了
获取mac地址:
#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib, "IPHLPAPI.lib")
#define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
#define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
int __cdecl main()
{
PIP_ADAPTER_INFO pAdapterInfo;
PIP_ADAPTER_INFO pAdapter = NULL;
DWORD dwRetVal = 0;
UINT i;
struct tm newtime;
char buffer[32];
errno_t error;
ULONG ulOutBufLen = sizeof(IP_ADAPTER_INFO);
pAdapterInfo = (IP_ADAPTER_INFO*)MALLOC(sizeof(IP_ADAPTER_INFO));
if (pAdapterInfo == NULL) {
printf("Error allocating memory needed to call GetAdaptersinfo\n");
return 1;
}
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
FREE(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO*)MALLOC(ulOutBufLen);
if (pAdapterInfo == NULL) {
printf("Error allocating memory needed to call GetAdaptersinfo\n");
return 1;
}
}
if ((dwRetVal = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
pAdapter = pAdapterInfo;
while (pAdapter) {
printf("\tComboIndex: \t%d\n", pAdapter->ComboIndex);
printf("\tAdapter Name: \t%s\n", pAdapter->AdapterName);
printf("\tAdapter Desc: \t%s\n", pAdapter->Description);
printf("\tAdapter Addr: \t");
for (i = 0; i < pAdapter->AddressLength; i++) {
if (i == (pAdapter->AddressLength - 1))
printf("%.2X\n", (int)pAdapter->Address[i]);
else
printf("%.2X-", (int)pAdapter->Address[i]);
}
printf("\tIndex: \t%d\n", pAdapter->Index);
printf("\tType: \t");
switch (pAdapter->Type) {
case MIB_IF_TYPE_OTHER:
printf("Other\n");
break;
case MIB_IF_TYPE_ETHERNET:
printf("Ethernet\n");
break;
case MIB_IF_TYPE_TOKENRING:
printf("Token Ring\n");
break;
case MIB_IF_TYPE_FDDI:
printf("FDDI\n");
break;
case MIB_IF_TYPE_PPP:
printf("PPP\n");
break;
case MIB_IF_TYPE_LOOPBACK:
printf("Lookback\n");
break;
case MIB_IF_TYPE_SLIP:
printf("Slip\n");
break;
default:
printf("Unknown type %ld\n", pAdapter->Type);
break;
}
printf("\tIP Address: \t%s\n",
pAdapter->IpAddressList.IpAddress.String);
printf("\tIP Mask: \t%s\n", pAdapter->IpAddressList.IpMask.String);
printf("\tGateway: \t%s\n", pAdapter->GatewayList.IpAddress.String);
printf("\t***\n");
if (pAdapter->DhcpEnabled) {
printf("\tDHCP Enabled: Yes\n");
printf("\t DHCP Server: \t%s\n",
pAdapter->DhcpServer.IpAddress.String);
printf("\t Lease Obtained: ");
error = _localtime32_s(&newtime, (__time32_t*)&pAdapter->LeaseObtained);
if (error)
printf("Invalid Argument to _localtime32_s\n");
else {
error = asctime_s(buffer, 32, &newtime);
if (error)
printf("Invalid Argument to asctime_s\n");
else
printf("%s", buffer);
}
printf("\t Lease Expires: ");
error = _localtime32_s(&newtime, (__time32_t*)&pAdapter->LeaseExpires);
if (error)
printf("Invalid Argument to _localtime32_s\n");
else {
error = asctime_s(buffer, 32, &newtime);
if (error)
printf("Invalid Argument to asctime_s\n");
else
printf("%s", buffer);
}
}
else
printf("\tDHCP Enabled: No\n");
if (pAdapter->HaveWins) {
printf("\tHave Wins: Yes\n");
printf("\t Primary Wins Server: %s\n",
pAdapter->PrimaryWinsServer.IpAddress.String);
printf("\t Secondary Wins Server: %s\n",
pAdapter->SecondaryWinsServer.IpAddress.String);
}
else
printf("\tHave Wins: No\n");
pAdapter = pAdapter->Next;
printf("\n");
}
}
else {
printf("GetAdaptersInfo failed with error: %d\n", dwRetVal);
}
if (pAdapterInfo)
FREE(pAdapterInfo);
return 0;
}
结果:
这里后面还有很多东西,就没有一一截图了
如果想了解更多:
https://wenku.baidu.com/view/aa5e3d126edb6f1aff001f69.html
https://www.cnblogs.com/qintangtao/archive/2012/12/21/2827540.html