I hope that can help to find a way for using with COMate.
dhcp and the new.
the code is not really complete but demonstrate the way for pasing the right parameter.
Code: Select all
IncludePath "..\..\"
; EnableExplicit
IncludeFile "VariantHelper_Include.pb"
#COINIT_MULTITHREAD = 0
#RPC_C_AUTHN_LEVEL_DEFAULT = 0
#RPC_C_AUTHN_LEVEL_CONNECT = 2
#RPC_C_IMP_LEVEL_IDENTIFY = 2
#EOAC_NONE = 0
#RPC_C_AUTHN_WINNT = 10
#RPC_C_AUTHZ_NONE = 0
#RPC_C_AUTHN_LEVEL_CALL = 3
#RPC_C_IMP_LEVEL_IMPERSONATE = 3
#CLSCTX_INPROC_SERVER = 1
#wbemFlagReturnImmediately = 16
#wbemFlagForwardOnly = 32
#IFlags = #wbemFlagReturnImmediately + #wbemFlagForwardOnly
#WBEM_INFINITE = $FFFFFFFF
#WMISeparator = ","
Global pLocator.IWbemLocator=#Null
;IWbemLocator *pLocator = NULL;
Global pNamespace.IWbemServices=0
;IWbemServices *pNamespace = 0;
Global pClass.IWbemClassObject=#Null
;IWbemClassObject *pClass = NULL;
Global pMethod.IWbemClassObject=#Null
;IWbemClassObject *pMethod = NULL;
;IWbemClassObject *pInInst = NULL;
Global pInInst.IWbemClassObject=#Null
;IWbemClassObject *pOutInst = NULL;
Global pOutInst.IWbemClassObject=#Null
Procedure StringToBStr(string$)
Protected Unicode$ = Space(StringByteLength(string$, #PB_Unicode) + 1)
Protected bstr_string.l
PokeS(@Unicode$, String$, -1, #PB_Unicode)
bstr_string = SysAllocString_(@Unicode$)
ProcedureReturn bstr_string
EndProcedure
Procedure.s UniToPB(*Unicode)
ProcedureReturn PeekS(*Unicode, #PB_Any, #PB_Unicode)
EndProcedure
; ProcedureDLL.s wmi_release(dumdum$)
; ;- WMI Release
; svc\release()
; loc\release()
; pEnumerator\release()
; If error=0
; pclsObj\release()
; EndIf
; CoUninitialize_()
; If FindString(dumdum$, "ERROR", 1)
; MessageRequester("", dumdum$)
; End
; EndIf
; EndProcedure
;// Strings needed later
className=StringToBStr("Win32_NetworkAdapterConfiguration")
methodName=StringToBStr("EnableStatic")
namespacePath=StringToBStr("root\cimv2")
;BSTR className = SysAllocString(L"Win32_NetworkAdapterConfiguration");
;BSTR methodName = SysAllocString(L"EnableStatic");
;BSTR namespacePath = SysAllocString(L"root\\cimv2");
;// Create WbemLocator Instance
;HRESULT hr;
;hr = CoCreateInstance(CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER,
;IID_IWbemLocator, (LPVOID *) &pLocator);
CoInitializeEx_(0,#COINIT_MULTITHREAD)
hres=CoInitializeSecurity_(0, -1,0,0,#RPC_C_AUTHN_LEVEL_DEFAULT,#RPC_C_IMP_LEVEL_IMPERSONATE,0,#EOAC_NONE,0)
If hres <> 0: txt$="ERROR: unable To call CoInitializeSecurity":Debug txt$:Else: Debug "CoInitializeSecurity..done!": EndIf
hr=CoCreateInstance_(?CLSID_WbemLocator,0,#CLSCTX_INPROC_SERVER,?IID_IWbemLocator,@pLocator)
If hr <> 0: txt$="ERROR: unable To call CoCreateInstance":Debug txt$:Else: Debug "CoCreateInstance..done!": EndIf
;// Using the WbemLocator, connect To the namespace
; hr = pLocator->ConnectServer(namespacePath, NULL, NULL, NULL, 0,
; NULL, NULL, &pNamespace);
hr=pLocator\ConnectServer(namespacePath,0,0,0,0,0,0,@pNamespace)
If hr <> 0: txt$="ERROR: unable To call IWbemLocator::ConnectServer": Debug txt$:Else: Debug "IWbemLocator::ConnectServer..done!": EndIf
; hr = CoSetProxyBlanket(
; pNamespace, // 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
; );
hr=CoSetProxyBlanket_(pNamespace,#RPC_C_AUTHN_WINNT,#RPC_C_AUTHZ_NONE ,#Null,#RPC_C_AUTHN_LEVEL_CALL,#RPC_C_IMP_LEVEL_IMPERSONATE,#Null,#EOAC_NONE)
If hr <> 0: txt$="ERROR: unable To call IWbemLocator::CoSetProxyBlanket": Debug txt$:Else: Debug "IWbemLocator::CoSetProxyBlanket..done!": EndIf
; If (FAILED(hr))
; {
; pNamespace->Release();
; pLocator->Release();
; CoUninitialize();
; Return 0; // Program has failed.
; }
;// Get the class object
;hr = pNamespace->GetObject(className, 0, NULL, &pClass, NULL);
hr = pNamespace\GetObject(className, 0, #Null, @pClass, #Null);
If hr <> 0: txt$="ERROR: unable To get the class object: "+UniToPB(className): Debug txt$:Else: Debug "class object: "+UniToPB(className)+"..done!": EndIf
;// Get the method we want To use in this class
;hr = pClass->GetMethod(methodName, 0, &pMethod, NULL);
hr = pClass\GetMethod(methodName, 0, @pMethod, #Null);
If hr <> 0: txt$="ERROR: unable To get the Methode to use: "+UniToPB(methodName): Debug txt$:Else: Debug "Methode to use: "+UniToPB(methodName)+"..done!": EndIf
;// Spawn an instance of the method so we can set it up (parameters)
;hr = pMethod->SpawnInstance(0, &pInInst);
hr = pMethod\SpawnInstance(0, @pInInst);
If hr <> 0: txt$="ERROR: unable To Spawn an instance of: "+UniToPB(methodName): Debug txt$:Else: Debug "Spawn an instance of: "+UniToPB(methodName)+"..done!": EndIf
;// EnableStatic() expects To receive the args in an array
; BSTR ArgNameIP = SysAllocString(L"IPAddress");
; BSTR ArgNameSM = SysAllocString(L"SubnetMask");
; BSTR IP = SysAllocString(L"172.21.0.1");
; BSTR SM = SysAllocString(L"255.255.0.0");
ArgNameIP=StringToBStr("IPAddress")
ArgNameSM=StringToBStr("SubnetMask")
IP=StringToBStr("192.168.66.8")
SM=StringToBStr("255.255.255.0")
; sab[1].SAFEARRAYBOUND
;
; SAFEARRAYBOUND sab[1];
; sab[0].cElements = 1;
; sab[0].lLbound = 0;
;
;
; LONG i = 0; // index needed for SafeArrayPutElement()
;
;
; // var1 is the array that holds one element which is IP
; VARIANT var1;
; var1.vt = VT_ARRAY | VT_BSTR;
; var1.parray = SafeArrayCreate(VT_BSTR, 1, sab); // create the array
; SafeArrayPutElement(var1.parray, &i, IP); // put IP in the array
; at pos 0
Define.safearray *psaIP, *psaSN,*psa_dest
Define.variant varIP, varSN,var_dest
Define.l i
; Create SafeArray
;Debug "Create SafeArray with first element is number 0 and 1 elements as string IP"
*psaIP = saCreateSafeArray(#TString, 0, 1)
*psaSN = saCreateSafeArray(#TString, 0, 1)
Debug "First Element: " + Str(saLBound(*psaIP))
Debug "Last Element: " + Str(saUBound(*psaIP))
Debug "Count Element: " + Str(saCount(*psaIP))
; Fill SafeArray IP
For i = saLBound(*psaIP) To saUBound(*psaIP)
SA_BSTR(*psaIP, i) = T_BSTR("192.168.200.1")
Next
; Fill SafeArray SubNet
For i = saLBound(*psaSN) To saUBound(*psaSN)
SA_BSTR(*psaSN, i) = T_BSTR("255.255.255.0")
Next
; ; Output SafeArray
; If *psaIP
; For i = saLBound(*psaIP) To saUBound(*psaIP)
; Debug "Index: " + Str(i) + " Value: " + SA_STR(*psaIP, i)
; Next
; EndIf
; Set Variant SafeArray as string IP
V_ARRAY_STR(varIP) = *psaIP
V_ARRAY_STR(varSN) = *psaSN
;// put the ArgName And the Argument itself into the Method instance we
;have
;hr = pInInst->Put(ArgNameIP, 0, &var1, 0);
hr = pInInst\Put(ArgNameIP, 0, varIP, 0);
If hr <> 0: txt$="ERROR: unable To put argument: "+UniToPB(ArgNameIP): Debug txt$:Else: Debug "put argument: "+UniToPB(ArgNameIP)+"..done!": EndIf
VariantClear_(varIP);
;VariantClear(&var1);
;hr = pInInst->Put(ArgNameSM, 0, &var2, 0);
hr = pInInst\Put(ArgNameSM, 0,varSN, 0);
If hr <> 0: txt$="ERROR: unable To put argument: "+UniToPB(ArgNameSM): Debug txt$:Else: Debug "put argument: "+UniToPB(ArgNameSM)+"..done!": EndIf
;VariantClear(&var2);
VariantClear_(varSN);
;
; ; // Enumerate the instances of the NetworkAdapter To get at the __PATH
; ; property
; ; // To the one And only network card we really want To modify
; ; IEnumWbemClassObject* pEnum;
; pEnum.IWbemClassObject
; ; hr = pNamespace->CreateInstanceEnum(className,
; ; WBEM_FLAG_RETURN_IMMEDIATELY | WBEM_FLAG_FORWARD_ONLY,
; ; NULL, &pEnum);
; #WBEM_FLAG_RETURN_IMMEDIATELY = 16;0x00000010
; #WBEM_FLAG_FORWARD_ONLY = 32;0x00000020
; hr = pNamespace\CreateInstanceEnum(className,#WBEM_FLAG_RETURN_IMMEDIATELY | #WBEM_FLAG_FORWARD_ONLY,#Null, @pEnum);
; If hr <> 0: txt$="ERROR: unable To enum class:"+UniToPB(className):Debug txt$:Else: Debug "enum class: "+UniToPB(className)+"..done!": EndIf
; ;
; // Get the instance
; ULONG numRet;
; BSTR PropName;
; VARIANT desc;
; VARIANT dhcp;
; VARIANT ipEnabled;
; VARIANT pathVar;
; IWbemClassObject *pInstance;
; While ( hr != WBEM_S_FALSE)
; {
; hr = pEnum->Next(WBEM_INFINITE, (ULONG)1, &pInstance, &numRet);
;
; If(0 == numRet)
; Break;
; // we know that the one With DHCP is the one we want
; PropName = SysAllocString(L"DHCPEnabled");
; pInstance->Get(PropName, 0, &dhcp, 0, 0);
; If (dhcp.bVal)
; {
; SysFreeString(PropName);
; PropName = SysAllocString(L"IPEnabled");
; pInstance->Get(PropName, 0, &ipEnabled, 0, 0);
; SysFreeString(PropName);
; If(ipEnabled.bVal)
; {
; // Print out the description just For us To see
; SysFreeString(PropName);
; PropName = SysAllocString(L"Description");
; pInstance->Get(PropName, 0, &desc, 0, 0);
; SysFreeString(PropName);
; Break;
; }
; }
; SysFreeString(PropName);
; }
;
;
; // now we have the instance of the actual network card we want To modify
; // let's query it's PATH property so that we can call the method on it.
; PropName = SysAllocString(L"__PATH");
; pInstance->Get(PropName, 0, &pathVar, 0, 0);
; BSTR InstancePath = pathVar.bstrVal;
; BSTR InstancePath = SysAllocString(L"Win32_NetworkAdapterConfiguration=2");
InstancePath=StringToBStr("Win32_NetworkAdapterConfiguration.Index=4")
;
; // finally we get To call the actuall method we want (EnableStatic())
; hr = pNamespace->ExecMethod(InstancePath, methodName, 0, NULL,
; pInInst, &pOutInst, NULL);
hr = pNamespace\ExecMethod(InstancePath, methodName, 0, #Null, pInInst, @pOutInst, #Null);
Debug hr
; long ret = 0;
; If(hr != WBEM_S_NO_ERROR)
; {
; BSTR Text;
hr = pOutInst\GetObjectText(0, Text);
Debug hr
Debug Text
; // CString strText = Text;
; // MessageBox(strText);
; }
; Else
; {
; // Get the EnableStatic method Return value
;
; VARIANT ret_value;
; BSTR strReturnValue = SysAllocString(L"ReturnValue");
; hr = pOutInst->Get(strReturnValue, 0, &ret_value, 0, 0);
; long ret = V_I4(&ret_value);
; }
;
;
;
; // We're done!
; // Free up resources
; SysFreeString(className);
; SysFreeString(methodName);
; SysFreeString(namespacePath);
; SysFreeString(ArgNameIP);
; SysFreeString(ArgNameSM);
; SysFreeString(IP);
; SysFreeString(SM);
; SysFreeString(PropName);
; SysFreeString(InstancePath);
; pLocator->Release();
; pClass->Release();
; pMethod->Release();
; pNamespace->Release();
; pInInst->Release();
; pOutInst->Release();
; pEnum->Release();
; pInstance->Release();
; CoUninitialize();
; Return ret;
; }
;
; Back To top
DataSection
CLSID_IEnumWbemClassObject:
;1B1CAD8C-2DAB-11D2-B604-00104B703EFD
Data.l $1B1CAD8C
Data.w $2DAB, $11D2
Data.b $B6, $04, $00, $10, $4B, $70, $3E, $FD
IID_IEnumWbemClassObject:
;7C857801-7381-11CF-884D-00AA004B2E24
Data.l $7C857801
Data.w $7381, $11CF
Data.b $88, $4D, $00, $AA, $00, $4B, $2E, $24
CLSID_WbemLocator:
;4590f811-1d3a-11d0-891f-00aa004b2e24
Data.l $4590F811
Data.w $1D3A, $11D0
Data.b $89, $1F, $00, $AA, $00, $4B, $2E, $24
IID_IWbemLocator:
;dc12a687-737f-11cf-884d-00aa004b2e24
Data.l $DC12A687
Data.w $737F, $11CF
Data.b $88, $4D, $00, $AA, $00, $4B, $2E, $24
IID_IUnknown:
;00000000-0000-0000-C000-000000000046
Data.l $00000000
Data.w $0000, $0000
Data.b $C0, $00, $00, $00, $00, $00, $00, $46
EndDataSection