[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1569
  • Last Modified:

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;
   }

0
WxW
Asked:
WxW
  • 2
1 Solution
 
mxjijoCommented:

I think it is a known bug.
Take a look at
http://cvs.sourceforge.net/viewcvs.py/dcplusplus/dcplusplus/windows/UPnP.cpp?rev=1.3

and go thru UPnP::OpenPorts() function.

0
 
WxWAuthor Commented:
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.
0
 
WxWAuthor Commented:
Found it. COM calls cannot be done from a message handler . (grrr) .
Thanks again.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now