I am trying to change TCP Window Size using WMI/C++, I found that
Win32_NetworkAdapterConfiguration has a method called SetTcpWindowSize
which takes a parameter TcpWindowSize). so I have following code, but
when I tried to put parameter value, the code always failed with
WBEM_E_TYPE_MISMATCH or 0x80041005. Any advice is greatly appreciated.
HRESULT hres;
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}
hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
if (FAILED(hres))
{
cout << "Failed to initialize security. Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres))
{
cout << "Failed to create IWbemLocator object."
<< " Err code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
IWbemServices *pSvc = NULL;
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
0, // Locale. NULL indicates current
NULL, // Security flags.
0, // Authority (e.g. Kerberos)
0, // Context object
&pSvc // pointer to IWbemServices proxy
);
if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x"
<< hex << hres << endl;
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;
hres = CoSetProxyBlanket(
pSvc, // Indicates the proxy to set
RPC_C_AUTHN_WINNT, // RPC_C_AUTHN_xxx
RPC_C_AUTHZ_NONE, // RPC_C_AUTHZ_xxx
NULL, // Server principal name
RPC_C_AUTHN_LEVEL_CALL, // RPC_C_AUTHN_LEVEL_xxx
RPC_C_IMP_LEVEL_IMPERSONATE, // RPC_C_IMP_LEVEL_xxx
NULL, // client identity
EOAC_NONE // proxy capabilities
);
if (FAILED(hres))
{
cout << "Could not set proxy blanket. Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
BSTR MethodName = SysAllocString(L"SetTcpWindowSize");
BSTR ClassPath =
SysAllocString(L"Win32_NetWorkAdapterConfiguration");
IWbemClassObject * pClass = NULL;
IWbemClassObject * pInClass = NULL;
IWbemClassObject * pInInst = NULL;
IWbemClassObject * pOutInst = NULL;
hres = pSvc->GetObject(ClassPath, 0, NULL, &pClass, NULL);
if( hres != WBEM_S_NO_ERROR )
return 0;
hres = pClass->GetMethod(MethodName, 0, &pInClass, NULL);
if( hres != WBEM_S_NO_ERROR )
{
pClass->Release();
return 0;
}
hres = pInClass->SpawnInstance(0, &pInInst);
if( hres != WBEM_S_NO_ERROR )
{
pClass->Release();
pInInst->Release();
}
VARIANT arg0;
arg0.vt = VT_UI2;
arg0.uiVal = 12000;
hres = pInInst->Put(_T("TcpWindowSize"), 0, &arg0, 0);
if( hres != WBEM_S_NO_ERROR ) // always failed with 0x80041005 or
WBEM_E_TYPE_MISMATCH
return 0;