项目下载地址: https://github.com/w123l123h/netConfig
在win7/win10上都测试通过
注册表地址: 计算机\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces
(和windows系统"控制面板\网络和 Internet\网络和共享中心"显示的名字一致)
注册表地址: 计算机\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network{4D36E972-E325-11CE-BFC1-08002BE10318}
//main.cpp:
#include <iostream>
#include <locale>
#include <string>
#include <codecvt>
#include "net_config.h"
#pragma execution_character_set("utf-8")
using namespace std;
int main()
{
std::locale::global(std::locale(""));
NetConfig config;
//网卡的key可以从注册表获取, 路径: 计算机\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\
config.set_key("{cd574b17-73e1-42df-a50c-f95d7f15c1d0}");
//设置静态ip之前要调用下enable_dhcp, 可以把以前设置的ip和网关都清空, 否则注册表会有多个ip信息
if (config.enable_dhcp()) {
cout << "enable_dhcp success" << endl;
}
else {
cout << "error code: " << config.get_last_error_code() << endl;
}
if (config.set_ip_config("192.168.1.100", "255.255.255.0", "192.168.1.1")) {
cout << "set_ip_config success" << endl;
}
else {
cout << "error code: " << config.get_last_error_code() << endl;
}
if (config.set_dns("255.255.255.0", "")) {
cout << "set_dns success" << endl;
}
else {
cout << "error code: " << config.get_last_error_code() << endl;
}
cin.get();
return 0;
}
//net_config.h
#pragma once
#include <string>
#include <memory>
#include <vector>
#include <Wbemidl.h>
class NetConfig
{
public:
NetConfig() {}
NetConfig(const std::string &key)
:key_(key){ }
~NetConfig();
//设置网络设备GUID
void set_key(const std::string &key) {
clear();
key_ = key;
}
//启用DHCP
bool enable_dhcp();
//启动静态IP,设置IP,掩码,网关
bool set_ip_config(const std::string &ip, const std::string &mask, const std::string &gateway);
bool set_ip_config(const std::string &ip, const std::string &mask);
//设置网关
bool set_gateway(const std::string &gateway);
//设置DNS地址
bool set_dns(const std::string &default_dns, const std::string &backup_dns);
//设置自动DNS
bool set_auto_dns();
int get_last_error_code()const {
return last_error_code_;
}
void clear();
private:
NetConfig(const NetConfig &rhs) = delete;
NetConfig &operator = (const NetConfig &rhs) = delete;
private:
//初始化
bool init();
//创建COM数组
std::shared_ptr<SAFEARRAY> create_SAFEARRAY(const std::vector<std::string> &args);
bool set_dns_base(bool is_auto, const std::string &default_dns, const std::string &backup_dns);
bool exec_method(const wchar_t *method, IWbemClassObject *params_instance);
private:
std::string key_;
bool is_init_ = false;
IWbemLocator* p_instance_ = NULL;
IWbemServices* p_service_ = NULL;
IEnumWbemClassObject* p_enum_ = NULL;
IWbemClassObject *p_obj_ = NULL;
IWbemClassObject *p_config = NULL;
VARIANT path_;
int last_error_code_ = 0;
};
//net_config.cpp
#include "net_config.h"
#include <codecvt>
#include <atlbase.h>
#include <comutil.h>
#ifdef _DEBUG
#include <iostream>
#endif
#pragma comment(lib, "wbemuuid.lib")
#pragma comment(lib, "comsuppw.lib")
#pragma execution_character_set("utf-8")
using namespace std;
NetConfig::~NetConfig()
{
clear();
}
bool NetConfig::enable_dhcp()
{
if (!init())
return false;
return exec_method(L"EnableDHCP", NULL);
}
bool NetConfig::set_ip_config(const std::string & ip, const std::string & mask, const std::string & gateway)
{
if (set_ip_config(ip, mask)) {
return set_gateway(gateway);
}
return false;
}
bool NetConfig::set_ip_config(const std::string & ip, const std::string & mask)
{
bool rt = false;
if (!init())
return rt;
IWbemClassObject *params = NULL;
IWbemClassObject *paramsInst = NULL;
p_config->GetMethod(_bstr_t("EnableStatic"), 0, ¶ms, NULL);
params->SpawnInstance(0, ¶msInst);
auto p1 = create_SAFEARRAY({ ip });
VARIANT paramVt;
paramVt.vt = VT_ARRAY | VT_BSTR;
paramVt.parray = p1.get();
paramsInst->Put(L"IPAddress", 0, ¶mVt, NULL);
p1 = create_SAFEARRAY({ mask });
paramVt.parray = p1.get();
paramsInst->Put(L"SubnetMask", 0, ¶mVt, NULL);
rt = exec_method(L"EnableStatic", paramsInst);
if (params) {
params->Release();
}
return rt;
}
bool NetConfig::set_dns(const std::string & default_dns, const std::string & backup_dns)
{
return set_dns_base(false, default_dns, backup_dns);
}
bool NetConfig::set_auto_dns()
{
return set_dns_base(true, "", "");
}
bool NetConfig::set_gateway(const std::string & gateway)
{
bool rt = false;
if (!init())
return rt;
IWbemClassObject *params = NULL;
IWbemClassObject *paramsInst = NULL;
p_config->GetMethod(_bstr_t("SetGateways"), 0, ¶ms, NULL);
params->SpawnInstance(0, ¶msInst);
auto p1 = create_SAFEARRAY({ gateway });
VARIANT paramVt;
paramVt.vt = VT_ARRAY | VT_BSTR;
paramVt.parray = p1.get();
paramsInst->Put(L"DefaultIPGateway", 0, ¶mVt, NULL);
paramVt.vt = VT_UINT;
paramVt.uintVal = 1;
paramsInst->Put(L"GatewayCostMetric", 0, ¶mVt, NULL);
rt = exec_method(L"SetGateways", paramsInst);
if (params) {
params->Release();
}
return rt;
}
void NetConfig::clear()
{
if (p_config) {
p_config->Release();
p_config = nullptr;
}
if (p_obj_) {
p_obj_->Release();
p_obj_ = nullptr;
}
if (p_enum_) {
p_enum_->Release();
p_enum_ = nullptr;
}
if (p_service_) {
p_service_->Release();
p_service_ = nullptr;
}
if (p_instance_) {
p_instance_->Release();
p_instance_ = nullptr;
}
if (is_init_) {
CoUninitialize();
}
is_init_ = false;
}
bool NetConfig::init()
{
if (is_init_) {
return true;
}
// Step 1: Initialize COM.
HRESULT hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres)) {
#ifdef _DEBUG
cout << "CoInitializeEx failed: " << hres << endl;
#endif
return false;
}
/*
// Step 2: Set general COM security levels
hres = CoInitializeSecurity(
NULL,
-1, // COM negotiates service
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
//ASSERT_THROW(SUCCEEDED(hres), "CoInitializeSecurity failed");
if (FAILED(hres)) {
#ifdef _DEBUG
cout << "CoInitializeSecurity failed: " << hres << endl;
#endif
return false;
}
*/
// Step 3: Obtain the initial locator to WMI
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator,
(LPVOID*)&p_instance_);
//ASSERT_THROW(SUCCEEDED(hres), "CoCreateInstance failed");
if (FAILED(hres)) {
#ifdef _DEBUG
cout << "CoCreateInstance failed: " << hres << endl;
#endif
return false;
}
// Step 4: Connect to the local root\cimv2 namespace and obtain pointer pSvc to make IWbemServices calls.
hres = p_instance_->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"),
NULL,
NULL,
0,
NULL,
0,
0,
&p_service_
);
//ASSERT_THROW(SUCCEEDED(hres), "ConnectServer failed");
if (FAILED(hres)) {
#ifdef _DEBUG
cout << "ConnectServer failed: " << hres << endl;
#endif
return false;
}
// Step 5: Set security levels for the proxy
hres = CoSetProxyBlanket(
p_service_, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
);
//ASSERT_THROW(SUCCEEDED(hres), "CoSetProxyBlanket failed");
if (FAILED(hres)) {
#ifdef _DEBUG
cout << "CoSetProxyBlanket failed: " << hres << endl;
#endif
return false;
}
// 通过适配器名称来找到指定的适配器对象.
CComBSTR TheQuery = L"SELECT * FROM Win32_NetworkAdapterConfiguration WHERE SettingID = \"";
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> conversion;
TheQuery += conversion.from_bytes(key_).c_str();
TheQuery += L"\"";
hres = p_service_->ExecQuery(
//SysAllocString(L"WQL"),
L"WQL",
TheQuery,
WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY,
NULL,
&p_enum_);
//ASSERT_THROW(SUCCEEDED(hres), "ExecQuery failed");
if (FAILED(hres)) {
#ifdef _DEBUG
cout << "ExecQuery failed: " << hres << endl;
#endif
return false;
}
// Get the adapter object.
ULONG num = 0;
hres = p_enum_->Next(WBEM_INFINITE, 1, &p_obj_, &num);
//ASSERT_THROW(SUCCEEDED(hres), "Next failed");
if (FAILED(hres)) {
#ifdef _DEBUG
cout << "Next failed: " << hres << endl;
#endif
return false;
}
//ASSERT_THROW(0 < num, "Next failed");
if (num < 1) {
#ifdef _DEBUG
cout << "Next failed num < 1" << endl;
#endif
return false;
}
VariantInit(&path_);
hres = p_obj_->Get(L"__PATH", 0, &path_, NULL, NULL);
//ASSERT_THROW(SUCCEEDED(hres), "Get path failed");
if (FAILED(hres)) {
#ifdef _DEBUG
cout << "Get failed: " << hres << endl;
#endif
return false;
}
hres = p_service_->GetObject(_bstr_t(L"Win32_NetworkAdapterConfiguration"), 0, NULL, &p_config, NULL);
//ASSERT_THROW(SUCCEEDED(hres), "GetObject Win32_NetworkAdapterConfiguration failed");
if (FAILED(hres)) {
#ifdef _DEBUG
cout << "GetObject failed: " << hres << endl;
#endif
return false;
}
is_init_ = true;
return true;
}
std::shared_ptr<SAFEARRAY> NetConfig::create_SAFEARRAY(const std::vector<std::string> &args)
{
SAFEARRAY *psa = SafeArrayCreateVector(VT_BSTR, 0, args.size());
long idx[] = { 0 };
for (int i = 0; i < args.size(); i++) {
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> conversion;
BSTR ip = SysAllocString(conversion.from_bytes(args[i]).c_str());
idx[0] = i;
if (FAILED(SafeArrayPutElement(psa, idx, ip))) {
return (false);
}
SysFreeString(ip);
}
return shared_ptr<SAFEARRAY>(psa, [](SAFEARRAY *psa) {SafeArrayDestroy(psa); });
}
bool NetConfig::set_dns_base(bool is_auto, const std::string & default_dns, const std::string & backup_dns)
{
bool rt = false;
if (!init())
return rt;
IWbemClassObject *params = NULL;
IWbemClassObject *paramsInst = NULL;
p_config->GetMethod(_bstr_t("SetDNSServerSearchOrder"), 0, ¶ms, NULL);
params->SpawnInstance(0, ¶msInst);
shared_ptr<SAFEARRAY> p1;
if (is_auto) {
paramsInst->Put(L"DNSServerSearchOrder", 0, NULL, NULL);
}
else {
if (backup_dns.size()) {
p1 = create_SAFEARRAY({ default_dns, backup_dns });
}
else {
p1 = create_SAFEARRAY({ default_dns });
}
VARIANT paramVt;
paramVt.vt = VT_ARRAY | VT_BSTR;
paramVt.parray = p1.get();
paramsInst->Put(L"DNSServerSearchOrder", 0, ¶mVt, NULL);
}
rt = exec_method(L"SetDNSServerSearchOrder", paramsInst);
if (params) {
params->Release();
}
return rt;
}
bool NetConfig::exec_method(const wchar_t * method, IWbemClassObject * params_instance)
{
bool rt = false;
IWbemClassObject *results = NULL;
auto res = p_service_->ExecMethod(path_.bstrVal, _bstr_t(method), 0, NULL, params_instance, &results, NULL);
if (SUCCEEDED(res)) {
VARIANT vtRet;
VariantInit(&vtRet);
if (!FAILED(results->Get(L"ReturnValue", 0, &vtRet, NULL, 0))) {
if (vtRet.uintVal == 0 || vtRet.uintVal == 1) {
rt = true;
}
else {
last_error_code_ = vtRet.uintVal;
#ifdef _DEBUG
wcout << method << " failed, result: " << last_error_code_ << endl;
#endif
}
}
#ifdef _DEBUG
else {
cout << "ExecMethod Get ReturnValue failed: " << res << endl;
}
#endif
VariantClear(&vtRet);
results->Release();
}
#ifdef _DEBUG
else {
cout << "ExecMethod failed: " << res << endl;
}
#endif
if (params_instance) {
params_instance->Release();
}
return rt;
}
文章浏览阅读1k次。通过使用ajax方法跨域请求是浏览器所不允许的,浏览器出于安全考虑是禁止的。警告信息如下:不过jQuery对跨域问题也有解决方案,使用jsonp的方式解决,方法如下:$.ajax({ async:false, url: 'http://www.mysite.com/demo.do', // 跨域URL ty..._nginx不停的xhr
文章浏览阅读2k次。关于在 Oracle 中配置 extproc 以访问 ST_Geometry,也就是我们所说的 使用空间SQL 的方法,官方文档链接如下。http://desktop.arcgis.com/zh-cn/arcmap/latest/manage-data/gdbs-in-oracle/configure-oracle-extproc.htm其实简单总结一下,主要就分为以下几个步骤。..._extproc
文章浏览阅读1.5w次。linux下没有上面的两个函数,需要使用函数 mbstowcs和wcstombsmbstowcs将多字节编码转换为宽字节编码wcstombs将宽字节编码转换为多字节编码这两个函数,转换过程中受到系统编码类型的影响,需要通过设置来设定转换前和转换后的编码类型。通过函数setlocale进行系统编码的设置。linux下输入命名locale -a查看系统支持的编码_linux c++ gbk->utf8
文章浏览阅读750次。今天准备从生产库向测试库进行数据导入,结果在imp导入的时候遇到“ IMP-00009:导出文件异常结束” 错误,google一下,发现可能有如下原因导致imp的数据太大,没有写buffer和commit两个数据库字符集不同从低版本exp的dmp文件,向高版本imp导出的dmp文件出错传输dmp文件时,文件损坏解决办法:imp时指定..._imp-00009导出文件异常结束
文章浏览阅读143次。当下是一个大数据的时代,各个行业都离不开数据的支持。因此,网络爬虫就应运而生。网络爬虫当下最为火热的是Python,Python开发爬虫相对简单,而且功能库相当完善,力压众多开发语言。本次教程我们爬取前程无忧的招聘信息来分析Python程序员需要掌握那些编程技术。首先在谷歌浏览器打开前程无忧的首页,按F12打开浏览器的开发者工具。浏览器开发者工具是用于捕捉网站的请求信息,通过分析请求信息可以了解请..._初级python程序员能力要求
文章浏览阅读7.6k次,点赞2次,收藏6次。@Service标注的bean,类名:ABDemoService查看源码后发现,原来是经过一个特殊处理:当类的名字是以两个或以上的大写字母开头的话,bean的名字会与类名保持一致public class AnnotationBeanNameGenerator implements BeanNameGenerator { private static final String C..._@service beanname
文章浏览阅读6.9w次,点赞73次,收藏463次。1.前序创建#include<stdio.h>#include<string.h>#include<stdlib.h>#include<malloc.h>#include<iostream>#include<stack>#include<queue>using namespace std;typed_二叉树的建立
文章浏览阅读7.1k次。在Asp.net上使用Excel导出功能,如果文件名出现中文,便会以乱码视之。 解决方法: fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);_asp.net utf8 导出中文字符乱码
文章浏览阅读2.1k次,点赞4次,收藏23次。第一次实验 词法分析实验报告设计思想词法分析的主要任务是根据文法的词汇表以及对应约定的编码进行一定的识别,找出文件中所有的合法的单词,并给出一定的信息作为最后的结果,用于后续语法分析程序的使用;本实验针对 PL/0 语言 的文法、词汇表编写一个词法分析程序,对于每个单词根据词汇表输出: (单词种类, 单词的值) 二元对。词汇表:种别编码单词符号助记符0beginb..._对pl/0作以下修改扩充。增加单词
文章浏览阅读773次。我在使用adb.exe时遇到了麻烦.我想使用与bash相同的adb.exe shell提示符,所以我决定更改默认的bash二进制文件(当然二进制文件是交叉编译的,一切都很完美)更改bash二进制文件遵循以下顺序> adb remount> adb push bash / system / bin /> adb shell> cd / system / bin> chm..._adb shell mv 权限
文章浏览阅读6.8k次,点赞12次,收藏125次。1. 单目相机标定引言相机标定已经研究多年,标定的算法可以分为基于摄影测量的标定和自标定。其中,应用最为广泛的还是张正友标定法。这是一种简单灵活、高鲁棒性、低成本的相机标定算法。仅需要一台相机和一块平面标定板构建相机标定系统,在标定过程中,相机拍摄多个角度下(至少两个角度,推荐10~20个角度)的标定板图像(相机和标定板都可以移动),即可对相机的内外参数进行标定。下面介绍张氏标定法(以下也这么称呼)的原理。原理相机模型和单应矩阵相机标定,就是对相机的内外参数进行计算的过程,从而得到物体到图像的投影_相机-投影仪标定
文章浏览阅读2.2k次。文章目录Wayland 架构Wayland 渲染Wayland的 硬件支持简 述: 翻译一篇关于和 wayland 有关的技术文章, 其英文标题为Wayland Architecture .Wayland 架构若是想要更好的理解 Wayland 架构及其与 X (X11 or X Window System) 结构;一种很好的方法是将事件从输入设备就开始跟踪, 查看期间所有的屏幕上出现的变化。这就是我们现在对 X 的理解。 内核是从一个输入设备中获取一个事件,并通过 evdev 输入_wayland