2012-10-17 3 views
1

여기 VBScript를 통해 모든 네트워크 연결을 표시하거나 숨길 수 있습니다. 하지만이 일을하기 위해 C++이나 COM 코드를 찾을 수 없습니다.HNet_ConnectionProperties를 운영하는 C++ 코드는 무엇입니까

strServer = "." 

Set objWMI = GetObject("winmgmts://" & strServer & "/root\Microsoft\HomeNet") 
Set objInstances = objWMI.InstancesOf("HNet_ConnectionProperties",48) 

On Error Resume Next 
For Each objInstance in objInstances 
    With objInstance 
     WScript.Echo .Connection 
     WScript.Echo .IsIcsPrivate 
     WScript.Echo .IsIcsPublic 
    End With 
On Error Goto 0 
Next 
+0

코드를 사용하여 공용 및 개인 네트워크 연결 공유를 비활성화하고 싶습니다. 다음은 MSDN http://support.microsoft.com/kb/828807/en-us의 문서입니다. –

답변

3

이것은 HNet_ConnectionProperties WMI 클래스

#include "stdafx.h" 
#define _WIN32_DCOM 
#include <iostream> 
using namespace std; 
#include <comdef.h> 
#include <Wbemidl.h> 
# pragma comment(lib, "wbemuuid.lib") 

//CREDENTIAL structure 
//http://msdn.microsoft.com/en-us/library/windows/desktop/aa374788%28v=vs.85%29.aspx 
#define CRED_MAX_USERNAME_LENGTH   513 
#define CRED_MAX_CREDENTIAL_BLOB_SIZE  512 
#define CREDUI_MAX_USERNAME_LENGTH CRED_MAX_USERNAME_LENGTH 
#define CREDUI_MAX_PASSWORD_LENGTH (CRED_MAX_CREDENTIAL_BLOB_SIZE/2) 


#pragma argsused 
int main(int argc, char* argv[]) 
{ 
    wchar_t pszName[CREDUI_MAX_USERNAME_LENGTH+1] = L"user"; 
    wchar_t pszPwd[CREDUI_MAX_PASSWORD_LENGTH+1] = L"password"; 
    BSTR strNetworkResource; 
    //To use a WMI remote connection set localconn to false and configure the values of the pszName, pszPwd and the name of the remote machine in strNetworkResource 
    bool localconn = true; 
    strNetworkResource = localconn ? L"\\\\.\\root\\Microsoft\\HomeNet" : L"\\\\remote--machine\\root\\Microsoft\\HomeNet"; 

    COAUTHIDENTITY *userAcct = NULL ; 
    COAUTHIDENTITY authIdent; 

    // Initialize COM. ------------------------------------------ 

    HRESULT hres; 
    hres = CoInitializeEx(0, COINIT_MULTITHREADED); 
    if (FAILED(hres)) 
    { 
     cout << "Failed to initialize COM library. Error code = 0x" << hex << hres << endl; 
     cout << _com_error(hres).ErrorMessage() << endl; 
     cout << "press enter to exit" << endl; 
     cin.get();  
     return 1;     // Program has failed. 
    } 

    // Set general COM security levels -------------------------- 

    if (localconn) 
     hres = CoInitializeSecurity(
      NULL, 
      -1,       // COM authentication 
      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 
      ); 
    else 
     hres = CoInitializeSecurity(
      NULL, 
      -1,       // COM authentication 
      NULL,      // Authentication services 
      NULL,      // Reserved 
      RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication 
      RPC_C_IMP_LEVEL_IDENTIFY, // Default Impersonation 
      NULL,      // Authentication info 
      EOAC_NONE,     // Additional capabilities 
      NULL       // Reserved 
      ); 

    if (FAILED(hres)) 
    { 
     cout << "Failed to initialize security. Error code = 0x" << hex << hres << endl; 
     cout << _com_error(hres).ErrorMessage() << endl; 
     CoUninitialize(); 
     cout << "press enter to exit" << endl; 
     cin.get();  
     return 1;     // Program has failed. 
    } 

    // Obtain the initial locator to WMI ------------------------- 

    IWbemLocator *pLoc = NULL; 
    hres = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc); 

    if (FAILED(hres)) 
    { 
     cout << "Failed to create IWbemLocator object." << " Err code = 0x" << hex << hres << endl; 
     cout << _com_error(hres).ErrorMessage() << endl; 
     CoUninitialize();  
     cout << "press enter to exit" << endl; 
     cin.get();  
     return 1;     // Program has failed. 
    } 

    // Connect to WMI through the IWbemLocator::ConnectServer method 

    IWbemServices *pSvc = NULL; 

    if (localconn) 
     hres = pLoc->ConnectServer(
      _bstr_t(strNetworkResource),  // Object path of WMI namespace 
      NULL,     // User name. NULL = current user 
      NULL,     // User password. NULL = current 
      0,      // Locale. NULL indicates current 
      NULL,     // Security flags. 
      0,      // Authority (e.g. Kerberos) 
      0,      // Context object 
      &pSvc     // pointer to IWbemServices proxy 
      ); 
    else 
     hres = pLoc->ConnectServer(
      _bstr_t(strNetworkResource), // Object path of WMI namespace 
      _bstr_t(pszName),    // User name 
      _bstr_t(pszPwd),    // User password 
      NULL,    // Locale 
      NULL,    // Security flags 
      NULL,    // Authority 
      NULL,    // Context object 
      &pSvc    // IWbemServices proxy 
      ); 

    if (FAILED(hres)) 
    { 
     cout << "Could not connect. Error code = 0x" << hex << hres << endl;  
     cout << _com_error(hres).ErrorMessage() << endl; 
     pLoc->Release(); 
     CoUninitialize(); 
     cout << "press enter to exit" << endl; 
     cin.get();   
     return 1;    // Program has failed. 
    } 

    cout << "Connected to root\\Microsoft\\HomeNet WMI namespace" << endl; 

    // Set security levels on the proxy ------------------------- 
    if (localconn) 
     hres = CoSetProxyBlanket(
      pSvc,      // 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 
     ); 
    else 
    { 
     // Create COAUTHIDENTITY that can be used for setting security on proxy 
     memset(&authIdent, 0, sizeof(COAUTHIDENTITY)); 
     authIdent.PasswordLength = wcslen (pszPwd); 
     authIdent.Password = (USHORT*)pszPwd; 
     authIdent.User = (USHORT*)pszName; 
     authIdent.UserLength = wcslen(pszName); 
     authIdent.Domain = 0; 
     authIdent.DomainLength = 0; 
     authIdent.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE; 
     userAcct = &authIdent; 

     hres = CoSetProxyBlanket(
      pSvc,       // Indicates the proxy to set 
      RPC_C_AUTHN_DEFAULT,   // RPC_C_AUTHN_xxx 
      RPC_C_AUTHZ_DEFAULT,   // RPC_C_AUTHZ_xxx 
      COLE_DEFAULT_PRINCIPAL,   // Server principal name 
      RPC_C_AUTHN_LEVEL_PKT_PRIVACY, // RPC_C_AUTHN_LEVEL_xxx 
      RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx 
      userAcct,      // client identity 
      EOAC_NONE      // proxy capabilities 
     ); 
    } 

    if (FAILED(hres)) 
    { 
     cout << "Could not set proxy blanket. Error code = 0x" << hex << hres << endl; 
     cout << _com_error(hres).ErrorMessage() << endl; 
     pSvc->Release(); 
     pLoc->Release(); 
     CoUninitialize(); 
     cout << "press enter to exit" << endl; 
     cin.get();  
     return 1;    // Program has failed. 
    } 

    // Use the IWbemServices pointer to make requests of WMI ---- 

    IEnumWbemClassObject* pEnumerator = NULL; 
    hres = pSvc->ExecQuery(L"WQL", L"SELECT * FROM HNet_ConnectionProperties", 
    WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pEnumerator); 

    if (FAILED(hres)) 
    { 
     cout << "ExecQuery failed" << " Error code = 0x" << hex << hres << endl; 
     cout << _com_error(hres).ErrorMessage() << endl; 
     pSvc->Release(); 
     pLoc->Release(); 
     CoUninitialize(); 
     cout << "press enter to exit" << endl; 
     cin.get();  
     return 1;    // Program has failed. 
    } 

    // Secure the enumerator proxy 
    if (!localconn) 
    { 

     hres = CoSetProxyBlanket(
      pEnumerator,     // Indicates the proxy to set 
      RPC_C_AUTHN_DEFAULT,   // RPC_C_AUTHN_xxx 
      RPC_C_AUTHZ_DEFAULT,   // RPC_C_AUTHZ_xxx 
      COLE_DEFAULT_PRINCIPAL,   // Server principal name 
      RPC_C_AUTHN_LEVEL_PKT_PRIVACY, // RPC_C_AUTHN_LEVEL_xxx 
      RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx 
      userAcct,      // client identity 
      EOAC_NONE      // proxy capabilities 
      ); 

     if (FAILED(hres)) 
     { 
      cout << "Could not set proxy blanket on enumerator. Error code = 0x" << hex << hres << endl; 
      cout << _com_error(hres).ErrorMessage() << endl; 
      pEnumerator->Release(); 
      pSvc->Release(); 
      pLoc->Release(); 
      CoUninitialize(); 
      cout << "press enter to exit" << endl; 
      cin.get();    
      return 1;    // Program has failed. 
     } 
    } 

    // Get the data from the WQL sentence 
    IWbemClassObject *pclsObj = NULL; 
    ULONG uReturn = 0; 

    while (pEnumerator) 
    { 
     HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); 

     if(0 == uReturn || FAILED(hr)) 
      break; 

     VARIANT vtProp; 

       hr = pclsObj->Get(L"Connection", 0, &vtProp, 0, 0);// Reference 
       if (!FAILED(hr)) 
       { 
        if ((vtProp.vt==VT_NULL) || (vtProp.vt==VT_EMPTY)) 
        wcout << "Connection : " << ((vtProp.vt==VT_NULL) ? "NULL" : "EMPTY") << endl; 
        else 
        wcout << "Connection : " << vtProp.bstrVal << endl; 
       } 
       VariantClear(&vtProp); 

       hr = pclsObj->Get(L"IsBridge", 0, &vtProp, 0, 0);// Boolean 
       if (!FAILED(hr)) 
       { 
        if ((vtProp.vt==VT_NULL) || (vtProp.vt==VT_EMPTY)) 
        wcout << "IsBridge : " << ((vtProp.vt==VT_NULL) ? "NULL" : "EMPTY") << endl; 
        else 
        wcout << "IsBridge : " << (vtProp.boolVal ? "True" : "False") << endl; 
       } 
       VariantClear(&vtProp); 

       hr = pclsObj->Get(L"IsBridgeMember", 0, &vtProp, 0, 0);// Boolean 
       if (!FAILED(hr)) 
       { 
        if ((vtProp.vt==VT_NULL) || (vtProp.vt==VT_EMPTY)) 
        wcout << "IsBridgeMember : " << ((vtProp.vt==VT_NULL) ? "NULL" : "EMPTY") << endl; 
        else 
        wcout << "IsBridgeMember : " << (vtProp.boolVal ? "True" : "False") << endl; 
       } 
       VariantClear(&vtProp); 

       hr = pclsObj->Get(L"IsFirewalled", 0, &vtProp, 0, 0);// Boolean 
       if (!FAILED(hr)) 
       { 
        if ((vtProp.vt==VT_NULL) || (vtProp.vt==VT_EMPTY)) 
        wcout << "IsFirewalled : " << ((vtProp.vt==VT_NULL) ? "NULL" : "EMPTY") << endl; 
        else 
        wcout << "IsFirewalled : " << (vtProp.boolVal ? "True" : "False") << endl; 
       } 
       VariantClear(&vtProp); 

       hr = pclsObj->Get(L"IsIcsPrivate", 0, &vtProp, 0, 0);// Boolean 
       if (!FAILED(hr)) 
       { 
        if ((vtProp.vt==VT_NULL) || (vtProp.vt==VT_EMPTY)) 
        wcout << "IsIcsPrivate : " << ((vtProp.vt==VT_NULL) ? "NULL" : "EMPTY") << endl; 
        else 
        wcout << "IsIcsPrivate : " << (vtProp.boolVal ? "True" : "False") << endl; 
       } 
       VariantClear(&vtProp); 

       hr = pclsObj->Get(L"IsIcsPublic", 0, &vtProp, 0, 0);// Boolean 
       if (!FAILED(hr)) 
       { 
        if ((vtProp.vt==VT_NULL) || (vtProp.vt==VT_EMPTY)) 
        wcout << "IsIcsPublic : " << ((vtProp.vt==VT_NULL) ? "NULL" : "EMPTY") << endl; 
        else 
        wcout << "IsIcsPublic : " << (vtProp.boolVal ? "True" : "False") << endl; 
       } 
       VariantClear(&vtProp); 


     pclsObj->Release(); 
     pclsObj=NULL; 
    } 

    // Cleanup 

    pSvc->Release(); 
    pLoc->Release(); 
    pEnumerator->Release(); 
    if (pclsObj!=NULL) 
    pclsObj->Release(); 

    CoUninitialize(); 
    cout << "press enter to exit" << endl; 
    cin.get(); 
    return 0; // Program successfully completed. 
} 

은 또한 당신이 WMI에 액세스하는 C++ 코드를 생성하는 데 도움을 줄 수 Delphi WMI Code Creator 같은 도구를 사용하여 고려에 액세스하기 위해 상응하는 C++ 코드입니다.