Link to home
Start Free TrialLog in
Avatar of WxW
WxW

asked on

Problem with UPnP NAT

I want to configure my router to add a port mapping, and I use thie following code, which used to work before a few minutes and now p->get_StaticPortMappingCollection(&is); will even return S_OK, but is remains NULL.
Any solution?
Thanks

bool _cdecl UpnpNATAdd(int Port,int AddRemove,wchar_t* Interface,wchar_t* Protocol)
      {
   IUPnPNAT* p = 0;
   IStaticPortMappingCollection* is = 0;
   IStaticPortMapping* isp = 0;

   HRESULT hr = 0;

   CoCreateInstance(CLSID_UPnPNAT,0,CLSCTX_INPROC_SERVER,IID_IUPnPNAT,(void**)&p);
   if (!p)
      return false;


   hr = p->get_StaticPortMappingCollection(&is);
   if (FAILED(hr) || is == 0)
      {
      p->Release();
      return false;
      }

   if (AddRemove == 0)
         {
      wchar_t* ab = new wchar_t[1000];
      swprintf(ab,L"TurboIRC 6 %s %u",Protocol,Port);
      hr = is->Add(Port,Protocol,Port,Interface,VARIANT_TRUE,ab,&isp);
      delete[] ab;
      }
   else
         hr = is->Remove(Port,Protocol);
   if (FAILED(hr))
      {
      is->Release();
      p->Release();
      return false;
      }
   if (AddRemove == 0)
         {
      hr = isp->Enable(VARIANT_TRUE);
         isp->Release();
      }
   is->Release();
       p->Release();
   return true;
   }

ASKER CERTIFIED SOLUTION
Avatar of mxjijo
mxjijo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WxW
WxW

ASKER

Thanks.
Do you know why, in some cases,  p->get_StaticPortMappingCollection(&is) will return RPC_E_CANTCALLOUT_ININPUTSYNCCALL ?

Happens when I call it in the middle of a program, but within the main thread.
Avatar of WxW

ASKER

Found it. COM calls cannot be done from a message handler . (grrr) .
Thanks again.