Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 12th, 2008, 12:21 AM   #1
rsnd
Hobbyist Programmer
 
rsnd's Avatar
 
Join Date: Jun 2005
Location: Helltown
Posts: 162
Rep Power: 4 rsnd is on a distinguished road
IUPnPNAT COM question

Ok I am trying to use the IUPnPNAT interface to add an entry to router's static port mapping. I instantiate the interface using CoCreateInstance and it returns no error. Then I try to get its port map collection using get_StaticPortMappingCollection and it returns no error but the returned interface's value is 0. Its meant to return a pointer to the interface like before. Is there anything i'm doing wrong?

Thanks in advance =)

Code:

#include <cstdio>
#include <windows.h>
#include "Natupnp.h."

inline void ReleaseObj(IUnknown* pInterface){
	if(pInterface){
		pInterface->Release();
	}
}

void log(char*arg_0,...) {
	char V8[1024];
	sprintf(V8, "%s\n", arg_0);
	va_list args;
	va_start (args, arg_0);
	vprintf (V8, args);
	va_end (args);
}


int HandleCOMError(HRESULT hr) {
	if(hr == S_OK){
		//log ("Success");
		return 0;
	}
	if (hr == REGDB_E_CLASSNOTREG){
		log ("REGDB_E_CLASSNOTREG: A specified class is not registered in the registration database. Also can indicate that the type of server you requested in the CLSCTX enumeration is not registered or the values for the server types in the registry are corrupt.");
	} else if (hr == CLASS_E_NOAGGREGATION){
		log ("CLASS_E_NOAGGREGATION: This class cannot be created as part of an aggregate.");
	} else if (hr == E_NOINTERFACE){
		log ("E_NOINTERFACE: The specified class does not implement the requested interface, or the controlling IUnknown does not expose the requested interface.");
	} else if (hr == E_ABORT){
		log ("E_ABORT: The operation was aborted. ");
	} else if (hr == E_FAIL){
		log ("E_FAIL:  An unspecified error occurred.");
	} else if (hr == E_INVALIDARG){
		log ("E_INVALIDARG: One of the parameters is invalid.");
	} else if (hr == E_NOTIMPL){
		log ("E_NOTIMPL: A specified method is not implemented.");
	} else if (hr == E_OUTOFMEMORY){
		log ("E_OUTOFMEMORY: The method was unable to allocate required memory. ");
	} else if (hr == E_POINTER){
		log ("E_POINTER: A pointer passed as a parameter is not valid.");
	} else if (hr == E_UNEXPECTED){
		log ("E_UNEXPECTED: The method failed for unknown reasons. ");
	} else {
		log ("Error of value 0x%x", hr);
	}

	return 1; 
}

HRESULT hr;
IUPnPNAT * upnpnat;
IStaticPortMappingCollection * spmaps;
long int count = 0;

int main (int argc, char**args) {
	// COM initializze
	hr = CoInitialize(NULL);


	upnpnat = NULL;
	log ("creating UPnPNAT");

	hr = CoCreateInstance(CLSID_UPnPNAT, NULL, CLSCTX_INPROC_SERVER, IID_IUPnPNAT, (void **) &upnpnat);
	if (HandleCOMError(hr)) {
		CoUninitialize();
		return 0;
	}

	log("Getting static port mapping collection");

	spmaps = 0;

	hr = upnpnat->get_StaticPortMappingCollection(&spmaps);

	if (HandleCOMError(hr)) {
		ReleaseObj(upnpnat);
		CoUninitialize();
		return 0;
	}

	log("Getting static port mapping collection count");

	
	hr = spmaps->get_Count(&count);
	
	if (HandleCOMError(hr)) {
		ReleaseObj(upnpnat);
		CoUninitialize();
		return 0;
	}

	log("%i static port maps found", count);



	log ("releasing UPnPNAT");
	//terminate the device finder
    ReleaseObj(upnpnat);
    upnpnat = NULL;
	CoUninitialize();

	return 0;
}

im using windows xp sp2, using the vc++ 2005 ee compiler set up with platform sdk windows server 2003 sp1 version

the above program outputs:
creating UPnPNAT
Getting static port mapping collection 
Getting static port mapping collection count
before the last line, spmaps is 0
__________________
Spread your wings and fly! Chicken!
rsnd is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Virtual pet site MySQL question proudnerd PHP 6 May 13th, 2006 7:22 PM
Visual Basic 2005 Question ReubenK Visual Basic .NET 3 Apr 5th, 2006 3:23 PM
How to post a question nnxion C++ 10 Jun 3rd, 2005 11:53 AM
How to post a question nnxion C++ 0 Jun 3rd, 2005 8:55 AM
How to post a question nnxion C 0 Jun 3rd, 2005 8:55 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:50 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC