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_StaticPortMappingCo llection(& 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;
IStaticPortMappingCollecti on* is = 0;
IStaticPortMapping* isp = 0;
HRESULT hr = 0;
CoCreateInstance(CLSID_UPn PNAT,0,CLS CTX_INPROC _SERVER,II D_IUPnPNAT ,(void**)& p);
if (!p)
return false;
hr = p->get_StaticPortMappingCo llection(& 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_T RUE,ab,&is p);
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;
}
Any solution?
Thanks
bool _cdecl UpnpNATAdd(int Port,int AddRemove,wchar_t* Interface,wchar_t* Protocol)
{
IUPnPNAT* p = 0;
IStaticPortMappingCollecti
IStaticPortMapping* isp = 0;
HRESULT hr = 0;
CoCreateInstance(CLSID_UPn
if (!p)
return false;
hr = p->get_StaticPortMappingCo
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Found it. COM calls cannot be done from a message handler . (grrr) .
Thanks again.
Thanks again.
ASKER
Do you know why, in some cases, p->get_StaticPortMappingCo
Happens when I call it in the middle of a program, but within the main thread.