Article View: borland.public.cppbuilder.nativeapi
Article #5136WMI EnableDNS not working - ErrorCoder 68 Help!!
From: Maurice Anderson
Date: Sat, 24 Sep 2005 21:54
Date: Sat, 24 Sep 2005 21:54
416 lines
14747 bytes
14747 bytes
Hello, I have the code below which is to attach to WMI. Once connected, I
am able to succesfully execute several WMI functions from class
Win32_NetworkAdapterConfiguration such as EnableStatic() and SetGateways().
Both are dynamic functions that work at the object level of the class.
However when I try to run EnableDNS, a static function executed on the class
itself, I get error 68 which is "Invalid input parameter.". However, as you
can see from my code, all 4 of my input parameters are standard.
Everything under Button1Click(TObject *Sender) works perfectly(special
thanks to Remy) which is to setup and connect me to WMI(root\cimv2). The
problem occurs under Button2Click(TObject *Sender) which is the
"EnableDNS()" code.
Any assistance or insight would be greatly appreciated:
------------Unit1.h-------------------------
//--------------------------------------------------------------------------
-
#ifndef Unit1H
#define Unit1H
//--------------------------------------------------------------------------
-
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "C:\Projects\FromWork\TWmi\Include\Wbemidl.h"
//--------------------------------------------------------------------------
-
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
TMemo *Memo1;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
private: // User declarations
bool bDidCoInit;
IWbemServices *pSvc;
IWbemLocator *pLoc;
IEnumWbemClassObject* pEnumerator;
void __fastcall Initialize();
void __fastcall ConnectToRoot(const AnsiString &Path);
void __fastcall ExecuteQuery(const AnsiString &Query);
void __fastcall Cleanup(void);
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//--------------------------------------------------------------------------
-
extern PACKAGE TForm1 *Form1;
//--------------------------------------------------------------------------
-
#endif
------------Unit1.cpp-------------------------
//--------------------------------------------------------------------------
-
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
# pragma comment(lib, "wbemuuid.lib")
//--------------------------------------------------------------------------
-
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//--------------------------------------------------------------------------
-
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Initialize();
try
{
ConnectToRoot("ROOT\\CIMV2");
ExecuteQuery("SELECT * FROM Win32_NetworkAdapterConfiguration");
}
catch(const Exception &)
{
Cleanup();
throw;
}
}
//--------------------------------------------------------------------------
void __fastcall TForm1::Cleanup(void)
{
// Cleanup
// ========
if( pSvc )
{
pSvc->Release();
pSvc = NULL;
}
if( pLoc )
{
pLoc->Release();
pLoc = NULL;
}
if( pEnumerator )
{
pEnumerator->Release();
pEnumerator = NULL;
}
if( bDidCoInit )
{
CoUninitialize();
bDidCoInit = false;
}
}
//--------------------------------------------------------------------------
void __fastcall TForm1::Initialize()
{
// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------
HRESULT hres = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if( FAILED(hres) )
throw Exception("Failed to initialize COM");
// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------
// Note: If you are using Windows 2000, you need to specify -
// the default authentication credentials for a user by using
// a SOLE_AUTHENTICATION_LIST structure in the pAuthList ----
// parameter of CoInitializeSecurity ------------------------
HRESULT hres2 = 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_ANY_AUTHORITY, // Additional capabilities
NULL // Reserved
);
if( FAILED(hres2) )
{
if(hres2 != RPC_E_TOO_LATE){
ShowMessage(hres2);
CoUninitialize();
throw Exception("Failed to initialize COM security");
}
}
// Step 3: ---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator,
(LPVOID *) &pLoc
);
if( FAILED(hres) )
{
CoUninitialize();
throw Exception("Failed to create WMI locator");
}
// all ok
bDidCoInit = true;
}
//--------------------------------------------------------------------------
void __fastcall TForm1::ConnectToRoot(const AnsiString &Path)
{
// Step 4: -----------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method
// Connect to the namespace with the current user and obtain pointer
pSvc
// to make IWbemServices calls.
if( !pLoc )
throw Exception("The WMI locator has not been created yet");
if( pSvc )
{
// disconnect from the current server
pSvc->Release();
pSvc = NULL;
}
HRESULT hres = pLoc->ConnectServer(
WideString(Path), // 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
);
if( FAILED(hres) )
throw Exception("Failed to connect to WMI Root");
// Step 5: --------------------------------------------------
// Set security levels on the proxy -------------------------
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
);
if( FAILED(hres) )
{
pSvc->Release();
pSvc = NULL;
throw Exception("Failed to set WMI services proxy security");
}
}
//--------------------------------------------------------------------------
void __fastcall TForm1::ExecuteQuery(const AnsiString &Query)
{
// Step 6: --------------------------------------------------
// Use the IWbemServices pointer to make requests of WMI ----
// For example, get the name of the operating system
if( !pSvc )
throw Exception("The WMI services proxy has not been created
yet");
if( pEnumerator )
{
// disconnect from the current query
pEnumerator->Release();
pEnumerator = NULL;
}
HRESULT hres = pSvc->ExecQuery(
WideString("WQL"),
WideString(Query),
WBEM_FLAG_BIDIRECTIONAL | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator
);
if( FAILED(hres) )
throw Exception("Failed on ExecuteCOMQuery");
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::Button2Click(TObject *Sender)
{
IWbemLocator *pLocator = NULL;
IWbemClassObject *pClass = NULL;
IWbemClassObject *pMethod = NULL;
IWbemClassObject *pInInst = NULL;
IWbemClassObject *pOutInst = NULL;
// Strings needed later
BSTR className = SysAllocString(L"Win32_NetworkAdapterConfiguration");
BSTR methodName = SysAllocString(L"EnableDNS");
// Create WbemLocator Instance
HRESULT hr;
hr = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLocator);
// Get the class object
hr = pSvc->GetObject(className, 0, NULL, &pClass, NULL);
// Get the method we want to use in this class
hr = pClass->GetMethod(methodName, 0, &pMethod, NULL);
// Spawn an instance of the method so we can set it up (parameters)
hr = pMethod->SpawnInstance(0, &pInInst);
// EnableDNS() expects to receive 4 args
BSTR ArgNameDNSHostName = SysAllocString(L"DNSHostName");
BSTR ArgNameDNSDomain = SysAllocString(L"DNSDomain");
BSTR ArgNameDNSServerSearchOrder =
SysAllocString(L"DNSServerSearchOrder");
BSTR ArgNameDNSDomainSuffixSearchOrder =
SysAllocString(L"DNSDomainSuffixSearchOrder");
BSTR DNSHostName = SysAllocString(L"MyHost");
BSTR DNSDomain = SysAllocString(L"MyHost.com");
BSTR DNSServerSearchOrder = SysAllocString(L"169.164.144.189");
BSTR DNSDomainSuffixSearchOrder = SysAllocString(L"MyHost2.com");
SAFEARRAYBOUND sab[1];
sab[0].cElements = 1;
sab[0].lLbound = 0;
LONG i = 0; // index needed for SafeArrayPutElement()
// var1 holds one String which is DNSHostName
VARIANT var1;
var1.bstrVal = DNSHostName;
//var2 holds one String which is DNSDomain
VARIANT var2;
var2.bstrVal = DNSDomain;
// var3 is the array that holds one element which is
DNSServerSearchOrder
VARIANT var3;
var1.vt = VT_ARRAY | VT_BSTR;
var1.parray = SafeArrayCreate(VT_BSTR, 1, sab); // create the array
SafeArrayPutElement(var1.parray, &i, DNSServerSearchOrder); // put IP
in the array at pos 0
// var4 is the array that holds one element which is
DNSDomainSuffixSearchOrder
VARIANT var4;
var2.vt = VT_ARRAY | VT_BSTR;
var2.parray = SafeArrayCreate(VT_BSTR, 1, sab); // create the array
SafeArrayPutElement(var2.parray, &i, DNSDomainSuffixSearchOrder);
// put SM in the array at pos 0
// put the ArgName and the Argument itself into the Method instance we
have
hr = pInInst->Put(ArgNameDNSHostName, 0, &var1, 0);
VariantClear(&var1);
hr = pInInst->Put(ArgNameDNSDomain, 0, &var2, 0);
VariantClear(&var2);
hr = pInInst->Put(ArgNameDNSServerSearchOrder, 0, &var3, 0);
VariantClear(&var3);
hr = pInInst->Put(ArgNameDNSDomainSuffixSearchOrder, 0, &var4, 0);
VariantClear(&var4);
BSTR InstancePath = SysAllocString(L"Win32_NetworkAdapterConfiguration");
// finally we get to call the actuall method we want (EnableStatic())
hr = pSvc->ExecMethod(InstancePath, methodName, 0, NULL,
pInInst, &pOutInst, NULL);
if(hr != WBEM_S_NO_ERROR)
{
ShowMessage(hr);
}
else{
/* Get function return value*/
VARIANT varReturnValue;
hr = pOutInst->Get((L"ReturnValue"), 0, &varReturnValue, NULL, 0);
int retcode = varReturnValue.intVal;
ShowMessage(retcode);
}
// We're done!
// Free up resources
SysFreeString(methodName);
SysFreeString(ArgNameDNSHostName);
SysFreeString(ArgNameDNSDomain);
SysFreeString(ArgNameDNSServerSearchOrder);
SysFreeString(ArgNameDNSDomainSuffixSearchOrder);
SysFreeString(DNSHostName);
SysFreeString(DNSDomain);
SysFreeString(DNSServerSearchOrder);
SysFreeString(DNSDomainSuffixSearchOrder);
SysFreeString(InstancePath);
pLocator->Release();
pClass->Release();
pMethod->Release();
pInInst->Release();
pOutInst->Release();
CoUninitialize();
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
Cleanup();
}
//--------------------------------------------------------------------------
-
Technical Details
Message-ID:
<4335cab8@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,368cc5701f7b7eea,start X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Maurice Anderson" <mauriceanderson@hotmail.com> Newsgroups: borland.public.cppbuilder.nativeapi Subject: WMI EnableDNS not working - ErrorCoder 68 Help!! Date: Sat, 24 Sep 2005 14:54:18 -0700 Lines: 422 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1437 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441 NNTP-Posting-Host: 64.30.203.223 Message-ID: <4335cab8@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1127598776 64.30.203.223 (24 Sep 2005 14:52:56 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail