Can I get an example of the DNS WMI in C++

I need an example of the CreateZone method from the DNS WMI in C++. Please, I have been messing with this forever, and can not get it.

Below is a Vbscript implementation. I do however need it in C++. This is pretty urgent!!
Function CreateZone(strZoneName)
Dim ErrorCode
Dim objLocator, objSService
Dim objA
Dim objServer
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objSService = objLocator.ConnectServer(Application("strPServer"), "root\microsoftdns", Application("strLogin"), Application("strPass"))
Set objServer = objSService.Get("MicrosoftDNS_Server.name="".""")
objSService.Security_.ImpersonationLevel = 3
Set objA = objSService.Get("MicrosoftDNS_ZONE")
If Application("ServerOS") = "2000" Then
objA.CreateZone strZoneName, 1, , , ""
End If
If Application("ServerOS") = "2003" Then
objA.CreateZone strZoneName, 0, True
End If
Set objLocator = Nothing
Set objSService = Nothing
Set objA = Nothing
Set objServer = Nothing
If Err.Number <> 0 Then 
CreateZone = False
Else
CreateZone = True
End If
End Function

Open in new window

LVL 2
hilltopAsked:
Who is Participating?
 
hilltopConnect With a Mentor Author Commented:
Ive given up. I have already been over that stuff. I cannot even connect to the name space.
0
 
trinitrotolueneDirector - Software EngineeringCommented:
suggest you take a look at the following documentation on MSDN. It lists out all the requisite methods needed for zoning....

http://msdn.microsoft.com/en-us/library/ms682757(VS.85).aspx

You need to create an instance of the MicrosoftDNS_Zone class. Once you create an object then you can call the functions that are provided by the MicrosoftDNS_Zone class.

the following link gives info on the createzone method.
http://msdn.microsoft.com/en-us/library/ms682760(VS.85).aspx
0
 
hilltopAuthor Commented:
I've been all over that stuff.
I cant even connect to the name space
Please help!


#define _WIN32_DCOM
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>
 
# pragma comment(lib, "wbemuuid.lib")
 
int main(int iArgCnt, char ** argv)
{
HRESULT hr;
hr = CoInitializeEx(0, COINIT_MULTITHREADED); 
if (FAILED(hr)) 
{ cout << "Failed to initialize COM library. Error code = 0x"
       << hex << hr << endl; 
  return hr;
}
 
hr =  CoInitializeSecurity(
    NULL,                      // Security descriptor    
    -1,                        // COM negotiates authentication service
    NULL,                      // Authentication services
    NULL,                      // Reserved
    RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication level for proxies
    RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation level for proxies
    NULL,                        // Authentication info
    EOAC_NONE,                   // Additional capabilities of the client or server
    NULL);                       // Reserved
 
if (FAILED(hr))
{
   cout << "Failed to initialize security. Error code = 0x" 
        << hex << hr << endl;
   CoUninitialize();
   return hr;                  // Program has failed.
}
 
    IWbemLocator *pLoc = 0;
   
 
    hr = CoCreateInstance(CLSID_WbemLocator, 0, 
        CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID *) &pLoc);
 
    if (FAILED(hr))
    {
        cout << "Failed to create IWbemLocator object. Err code = 0x"
             << hex << hr << endl;
        CoUninitialize();
        return hr;     // Program has failed.
    }
 
IWbemServices *pSvc = 0;
 
    // Connect to the root\default namespace with the current user.
    hr = pLoc->ConnectServer(
            BSTR(L"root\microsoftdns"), 
            NULL, NULL, 0, NULL, 0, 0, &pSvc);
 
    if (FAILED(hr))
    {
        cout << "Could not connect. Error code = 0x" 
             << hex << hr << endl;
        pLoc->Release();
        CoUninitialize();
        return hr;      // Program has failed.
    }
 
    cout << "Connected to WMI" << endl;
 
    CoUninitialize();
    return 0;
}

Open in new window

0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
trinitrotolueneDirector - Software EngineeringCommented:
what is the exact error? and why are you using COM?
0
 
hilltopAuthor Commented:
Could not connect. Error code = 0x8004100e

This is on a machine that does have the provider installed. I can connect to other name spaces with the same code.
0
 
hilltopAuthor Commented:
You must use COM to access WMI in C++
0
 
trinitrotolueneDirector - Software EngineeringCommented:
Check whether you are loggin in as administrator

Also here's some more info on a problem with the host provider when Vista is used....
http://support.microsoft.com/kb/935553/

What is the OS you are running on?
0
 
trinitrotolueneDirector - Software EngineeringCommented:
>>>>>>>>>>>>>>>>>>BSTR(L"root\microsoftdns")


should it not be root\\microsoftdns

I guess the double slash is necessary to escape
0
 
trinitrotolueneDirector - Software EngineeringCommented:
here's some more interesting reading in case you were not aware of it earlier

http://msdn.microsoft.com/en-us/library/aa394558(VS.85).aspx
0
 
trinitrotolueneConnect With a Mentor Director - Software EngineeringCommented:
hello!!!

did it help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.