NetConfig.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #pragma once
  2. class NetConfig
  3. {
  4. public:
  5. NetConfig() {
  6. is_init_ = false;
  7. p_instance_ = nullptr;
  8. p_service_ = nullptr;
  9. p_enum_ = nullptr;
  10. p_obj_ = nullptr;
  11. p_config = nullptr;
  12. last_error_code_ = 0;
  13. }
  14. NetConfig(const std::string& key)
  15. :key_(key) {
  16. is_init_ = false;
  17. p_instance_ = nullptr;
  18. p_service_ = nullptr;
  19. p_enum_ = nullptr;
  20. p_obj_ = nullptr;
  21. p_config = nullptr;
  22. last_error_code_ = 0;
  23. }
  24. ~NetConfig();
  25. //设置网络设备GUID
  26. void set_key(const std::string& key) {
  27. clear();
  28. key_ = key;
  29. }
  30. //启用DHCP
  31. bool enable_dhcp();
  32. //启动静态IP,设置IP,掩码,网关
  33. bool set_ip_config(const std::string& ip, const std::string& mask, const std::string& gateway);
  34. bool set_ip_config(const std::string& ip, const std::string& mask);
  35. //设置网关
  36. bool set_gateway(const std::string& gateway);
  37. //设置DNS地址
  38. bool set_dns(const std::string& default_dns, const std::string& backup_dns);
  39. //设置自动DNS
  40. bool set_auto_dns();
  41. int get_last_error_code()const {
  42. return last_error_code_;
  43. }
  44. void clear();
  45. /*
  46. private:
  47. NetConfig(const NetConfig &rhs) = delete;
  48. NetConfig &operator = (const NetConfig &rhs) = delete;
  49. */
  50. private:
  51. //初始化
  52. bool init();
  53. //创建COM数组
  54. std::shared_ptr<SAFEARRAY> create_SAFEARRAY(const std::vector<std::string>& args);
  55. bool set_dns_base(bool is_auto, const std::string& default_dns, const std::string& backup_dns);
  56. bool exec_method(const wchar_t* method, IWbemClassObject* params_instance);
  57. private:
  58. std::string key_;
  59. bool is_init_;
  60. IWbemLocator* p_instance_;
  61. IWbemServices* p_service_;
  62. IEnumWbemClassObject* p_enum_;
  63. IWbemClassObject* p_obj_;
  64. IWbemClassObject* p_config;
  65. VARIANT path_;
  66. int last_error_code_;
  67. };