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

LVL 6
WxWAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

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.