IUPnPNAT *nat = NULL; HRESULT hr; MMechostr(0, "UPnP> GetExternalIP: Initialization:\n"); IUPnPDeviceFinder* piDeviceFinder = NULL; MMechostr(0, "UPnP> UP: Device Finder ..."); //if (!SUCCEEDED(CoCreateInstance(CLSID_UPnPDeviceFinder, NULL, CLSCTX_ALL, IID_IUPnPDeviceFinder, (void**) &piDeviceFinder)) || (piDeviceFinder==NULL) ) //CLSID_UPnPDeviceFinder use __uuidof(UPnPDeviceFinder) // IID_IUPnPDeviceFinder use __uuidof(IUPnPDeviceFinder), if (!SUCCEEDED(CoCreateInstance(__uuidof(UPnPDeviceFinder), NULL, CLSCTX_ALL, __uuidof(IUPnPDeviceFinder), (void**) &piDeviceFinder)) || (piDeviceFinder==NULL) ) { MMechostr(0, "UPnP> UP: Failed\n"); return 0; } MMechostr(0,"\n"); BSTR bStrDev = SysAllocString( L"urn:schemas-upnp-org:service:WANIPConnection:1" ); IUPnPDevices* piFoundDevices = NULL; MMechostr(0, "UPnP> UP: Finding Devices ..."); if (!SUCCEEDED( piDeviceFinder->FindByType(bStrDev, 0, &piFoundDevices)) || (piFoundDevices==NULL)) { MMechostr(0, "UPnP> UP: Failed\n"); piDeviceFinder->Release(); piDeviceFinder = NULL; SysFreeString(bStrDev); bStrDev = NULL; return 0; } MMechostr(0,"\n"); piDeviceFinder->Release(); piDeviceFinder = NULL; SysFreeString(bStrDev); bStrDev = NULL; IUnknown * pUnk = NULL; BSTR bStr = NULL; if (SUCCEEDED(piFoundDevices->get__NewEnum(&pUnk)) && (pUnk!=NULL)) { IEnumVARIANT * pEnumVar = NULL; hr = pUnk->QueryInterface(IID_IEnumVARIANT, (void **) &pEnumVar); if (SUCCEEDED(hr)) { VARIANT varCurDevice; VariantInit(&varCurDevice); pEnumVar->Reset(); // Loop through each device in the collection while (S_OK == pEnumVar->Next(1, &varCurDevice, NULL)) { IUPnPDevice * pDevice = NULL; IDispatch * pdispDevice = V_DISPATCH(&varCurDevice); if (SUCCEEDED(pdispDevice->QueryInterface(__uuidof(IUPnPDevice), (void **) &pDevice))) { hr = pDevice->get_Description(&bStr); if (SUCCEEDED(hr)) { MMechostr(0,"> Device found: %S\n", bStr); SysFreeString(bStr); bStr = NULL; } hr = pDevice->get_ManufacturerName(&bStr); if (SUCCEEDED(hr)) { MMechostr(0,"> Manufacturer: %S\n", bStr); SysFreeString(bStr); bStr = NULL; } hr = pDevice->get_ModelName(&bStr); if (SUCCEEDED(hr)) { MMechostr(0,"> Model: %S\n", bStr); SysFreeString(bStr); bStr = NULL; } hr = pDevice->get_UniqueDeviceName(&bStr); if (SUCCEEDED(hr)) { MMechostr(0,"> %S\n", bStr); SysFreeString(bStr); bStr = NULL; } MMechostr(0,">Get ?\n"); // Create a BSTR to hold the service name BSTR bstrServiceName = L"urn:schemas-upnp-org:service:WANIPConnection:1"; char sExtIP[16]; // Get the list of services available on the device IUPnPServices *pServices = NULL; hr = pDevice->get_Services(&pServices); if (SUCCEEDED(hr)) { MMechostr(0,">Got dervices\n"); // Retrieve the service we are interested in IUPnPService * pAppService = NULL; hr = pServices->get_Item(bstrServiceName, &pAppService); if (SUCCEEDED(hr)) { // Do something interesting with the service object //pAppService->GetExternalIPAddress(sExtIP); //MMechostr(0,"> %s\n", sExtIP); MMechostr(0,">Got it !!!\n", sExtIP); pAppService->Release(); } pServices->Release(); } SysFreeString(bstrServiceName); bstrServiceName = NULL; } VariantClear(&varCurDevice); } pEnumVar->Release(); } pUnk->Release(); } MMechostr(0, "UPnP> UP: Init NAT ..."); CoCreateInstance(__uuidof(UPnPNAT), NULL, CLSCTX_ALL, __uuidof(IUPnPNAT), (void**)&nat); if (!nat) { MMechostr(0, " Failed\n"); return 0; } MMechostr(0, "\n"); MMechostr(0, "UPnP> UP: Getting collection ..."); hr = nat->get_StaticPortMappingCollection(&collection); if (hr == S_OK) MMechostr(0,"\nS_OK"); if (!collection) { MMechostr(0, " Failed\n"); if (hr == E_ABORT) MMechostr(0,"E_ABORT\n"); else if (hr == E_FAIL) MMechostr(0,"E_FAIL\n"); else if (hr == E_INVALIDARG) MMechostr(0,"E_INVALIDARG\n"); else if (hr == E_NOINTERFACE) MMechostr(0,"E_NOINTERFACE\n"); else if (hr == E_NOTIMPL) MMechostr(0,"E_NOTIMPL\n"); else if (hr == E_OUTOFMEMORY) MMechostr(0,"E_OUTOFMEMORY\n"); else if (hr == E_POINTER) MMechostr(0,"E_POINTER\n"); else if (hr == E_UNEXPECTED) MMechostr(0,"E_UNEXPECTED\n"); else MMechostr(0,"?\n"); nat->Release(); return 0; } MMechostr(0, "\n"); if (nat) nat->Release();