ExecMethod returns WBEM_E_FAILED with CreateZone
- From: "Aurelien" <kindman216@xxxxxxxxxxx>
- Date: Thu, 28 Dec 2006 15:49:00 +0100
Hello everybody,
I'm trying to create a new DNS zone on a MS/DNS server using WMI. Creating
primary zones works fine, but creating secondaries doesn't work, using same
initialization code plus one parameter for the master address : ExecMethod
returns WBEM_E_FAILED, and I can't see the point. I'm not sure if the
problem comes from the initialization of the BSTR[] or an obscure internal
problem. If you can examine the following code, it would be a great help.
Thanks by advance,
Aurelien.
// test_wmi.cpp : définit le point d'entrée pour l'application console.
//
#include "stdafx.h"
#pragma comment(lib, "wbemuuid.lib")
#pragma warning(disable: 4996)
int _tmain(int argc, _TCHAR* argv[])
{
IWbemLocator *pIWbemLocator = NULL;
IWbemServices *pWbemServices = NULL;
IEnumWbemClassObject *pEnumObject = NULL;
IWbemClassObject *pClassObject = NULL;
IWbemClassObject *zone_class = NULL;
IWbemClassObject *zone_class_in_params = NULL;
IWbemClassObject *params_in = NULL;
pEnumObject = NULL;
wchar_t zonename[256];
unsigned long uCount = 1, uReturned;
HRESULT hr;
if (CoInitializeEx(NULL, COINIT_MULTITHREADED) != S_OK)
{
printf("CoInitializeEx failed\n");
return 1;
}
if (CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, 0) != S_OK)
{
printf("CoInitializeSecurity failed\n");
return 1;
}
if (FAILED(CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER,
IID_IWbemLocator,
(void **)&pIWbemLocator)))
{
printf("CoCreateInstance failed\n");
return 1;
}
if (FAILED(pIWbemLocator->ConnectServer(_bstr_t(L"ROOT\\microsoftdns"),
NULL, NULL, 0, NULL, 0, 0, &pWbemServices)))
{
printf("ConnectServer failed\n");
return 1;
}
if (FAILED(CoSetProxyBlanket(pWbemServices, RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE, NULL, RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE)))
{
printf("CoSetProxyBlanket failed\n");
return 1;
}
if (pWbemServices->GetObject(_bstr_t(L"MicrosoftDNS_Zone"), 0, NULL,
&zone_class, NULL) !=
WBEM_S_NO_ERROR)
{
printf("GetObject failed\n");
}
if ((hr = zone_class->GetMethod(_bstr_t(L"CreateZone"), 0,
&zone_class_in_params, NULL)) == WBEM_S_NO_ERROR)
{
if (FAILED(zone_class_in_params->SpawnInstance(0, ¶ms_in)))
{
printf("Spawninstance failed\n");
}
VARIANT zname, ztype, zds, ziparray;
V_VT(&zname) = VT_BSTR;
V_BSTR(&zname) = _bstr_t(L"test.intranet");
V_VT(&ztype) = VT_BSTR;
V_BSTR(&ztype) = _bstr_t(L"1"); // Master=1/w2k 0/w2k3 Slave=2/w2k 1/w2k3
V_VT(&zds) = VT_BOOL;
V_BOOL(&zds) = false;
V_VT(&ziparray) = VT_ARRAY | VT_BSTR;
SAFEARRAYBOUND sab;
SAFEARRAY *ipArray;
BSTR *pbStr = NULL;
BSTR ip = _bstr_t(L"10.0.0.10");
sab.lLbound = 1;
sab.cElements = 1;
if ((ipArray = SafeArrayCreate(VT_BSTR, 1, &sab)) != NULL)
{
if ((hr = SafeArrayAccessData(ipArray, (void HUGEP **)&pbStr)) == S_OK)
{
pbStr[0] = ip;
}
else
{
printf("SafeArrayAccessData failed\n");
}
SafeArrayUnaccessData(ipArray);
V_ARRAY(&ziparray) = ipArray;
}
else
{
printf("SafeArrayCreate failed\n");
}
if (FAILED(params_in->Put(_bstr_t(L"ZoneName"), 0, &zname, 0)))
printf("ZoneName failed\n");
if (FAILED(params_in->Put(_bstr_t(L"ZoneType"), 0, &ztype, 0)))
printf("ZoneType failed\n");
if (FAILED(params_in->Put(_bstr_t(L"DsIntegrated"), 0, &zds, 0)))
printf("DsIntegrated failed\n");
if (FAILED(params_in->Put(_bstr_t(L"IpAddr"), 0, &ziparray, 0)))
printf("IpAddr failed\n");
hr = pWbemServices->ExecMethod(_bstr_t(L"MicrosoftDNS_Zone"),
_bstr_t(L"CreateZone"), 0, NULL,
params_in, NULL, NULL);
if (FAILED(hr))
{
printf("CreateZone returned error %x", hr);
}
else
{
printf("CreateZone returned OK\n");
}
}
else
{
printf("GetMethod returned error %x\n", hr);
}
return 0;
}
.
- Prev by Date: Re: Urgent:- Reading event log files using Win32_NTLogevent class
- Previous by thread: Re: block some exe
- Index(es):
Loading