cardif_windows_wmi.c

来自「linux 下通过802.1认证的安装包」· C语言 代码 · 共 2,302 行 · 第 1/5 页

C
2,302
字号
		lpMsgBuf = GetLastErrorStr(GetLastError());

		debug_printf(DEBUG_NORMAL, "Error was : %s", lpMsgBuf);

		LocalFree(lpMsgBuf);

		retval = -1;
		goto done;
	}

    hr = IWbemServices_ExecMethod(wmiSvc, IntPath, MethodName, 0,
		NULL, pClass, &pOutParams, NULL);
    if (FAILED(hr))
    {
        debug_printf(DEBUG_NORMAL, "Could not execute method. Error code = 0x%x\n", hr);

		lpMsgBuf = GetLastErrorStr(GetLastError());

		debug_printf(DEBUG_NORMAL, "Error : %s\n", lpMsgBuf);

		LocalFree(lpMsgBuf);

		retval = -1;
		goto done;
	}

    hr = IWbemClassObject_Get(pOutParams, L"ReturnValue", 0, 
        &varReturnValue, NULL, 0);
	if (FAILED(hr))
	{
		debug_printf(DEBUG_NORMAL, "Failed to get 'ReturnValue'.\n");

		retval = -1;
		goto done;
	}

	debug_printf(DEBUG_INT, "Return value : %d\n", varReturnValue.intVal);
	retval = varReturnValue.intVal;

	VariantClear(&varReturnValue);

done:
	// Clean up.
	if (pOutParams) IWbemClassObject_Release(pOutParams);
	if (pClass) IWbemClassObject_Release(pClass);

	if (MethodName) SysFreeString(MethodName);
	if (IntPath) SysFreeString(IntPath);
	if (ClassName) SysFreeString(ClassName);

	return retval;
}

/**
 * \brief Request that an interface change itself to using DHCP.
 *
 * @param[in] ctx   The context that we want to enable DHCP on.
 *
 * \retval <0 on non-WMI error
 * \retval >=0 on success, or WMI error
 **/
int cardif_windows_wmi_enable_dhcp(context *ctx)
{
	return cardif_windows_wmi_call(ctx, "EnableDHCP");
}

/**
 * \brief Request that an interface release a DHCP address.
 *
 * @param[in] ctx   The context that we want to enable DHCP on.
 *
 * \retval <0 on non-WMI error
 * \retval >=0 on success, or WMI error
 **/
int cardif_windows_wmi_release_dhcp(context *ctx)
{
	return cardif_windows_wmi_call(ctx, "ReleaseDHCPLease");
}

/**
 * \brief Request that an interface renew a DHCP address.
 *
 * @param[in] ctx   The context that we want to enable DHCP on.
 *
 * \retval <0 on non-WMI error
 * \retval >=0 on success, or WMI error
 **/
int cardif_windows_wmi_renew_dhcp(context *ctx)
{
	debug_printf(DEBUG_NORMAL, "Interface '%s' requested a DHCP renew.\n", ctx->desc);
	return cardif_windows_wmi_call_async(ctx, "RenewDHCPLease", "DHCP renew", cardif_windows_wmi_async_dhcp_renew_callback);
}

/**
 * \brief Request that an interface release/renew a DHCP address.
 *
 * @param[in] ctx   The context that we want to release/renew DHCP on.
 *
 * \retval <0 on non-WMI error
 * \retval >=0 on success, or WMI error
 **/
int cardif_windows_wmi_release_renew(context *ctx)
{
	debug_printf(DEBUG_NORMAL, "Interface '%s' requested a DHCP release.\n", ctx->desc);
	return cardif_windows_wmi_call_async(ctx, "ReleaseDHCPLease", "DHCP release", cardif_windows_wmi_async_dhcp_release_renew_callback);
}

/**
 * \brief Switch the interface to use static IP addresses, and set the
 *        IP address and netmask.
 *
 * @param[in] ipaddr   The new IP address to use.
 * @param[in] netmask    The new netmask to use.
 * 
 * \retval <0 on error
 * \retval 0 on success
 * \retval >0 error returned by WMI method call
 **/
int cardif_windows_wmi_set_static_ip(context *ctx, char *ipaddr, char *netmask)
{
	struct win_sock_data *sockData = NULL;
	HRESULT hr;
	VARIANT varIpaddr, varNetmask, varReturnValue;
	IWbemClassObject *pclsObj = NULL, *pClassInstance = NULL;
	IWbemClassObject *inobj = NULL, *outdata = NULL;
	IWbemCallResult *pResult = NULL;
	int retval = XENONE;
	BSTR MethodName = SysAllocString(L"EnableStatic");
	BSTR ClassName = SysAllocString(L"Win32_NetworkAdapterConfiguration");
	BSTR IntPath = NULL;
	SAFEARRAY *iparray = NULL;
	SAFEARRAY *maskarray = NULL;
	LPVOID lpMsgBuf = NULL;
	char apath[100];
	char lpath[200];

	if (ctx == NULL)
	{
		retval = -1;
		goto done;
	}

	sockData = (struct win_sock_data *)ctx->sockData;
	if (sockData == NULL)
	{
		debug_printf(DEBUG_NORMAL, "Couldn't get socket data for context '%s'!?\n", 
			ctx->intName);
		retval = -1;
		goto done;
	}

	if (sockData->wmiIntIdx == INVALID_WMI_IDX)
	{
		// We don't know the index, so locate it.
		if (cardif_windows_wmi_get_idx(ctx, NULL) != XENONE)
		{
			debug_printf(DEBUG_NORMAL, "Couldn't determine WMI interface index in %s()!\n",
					__FUNCTION__);
			retval = -1;
			goto done;
		}
	}

	sprintf(&apath, "Win32_NetworkAdapterConfiguration.Index='%d'", sockData->wmiIntIdx);
	mbstowcs((wchar_t *)&lpath, apath, strlen(apath)+1);

	IntPath = SysAllocString((OLECHAR *)&lpath);
	if (IntPath == NULL)
	{
		debug_printf(DEBUG_NORMAL, "Couldn't create interface path!\n");
		retval = -1;
		goto done;
	}

    hr = IWbemServices_GetObject(wmiSvc, ClassName, 0, NULL, &pclsObj, NULL);
	if (FAILED(hr))
	{
		debug_printf(DEBUG_NORMAL, "Failed to get class handle!\n");
		debug_printf(DEBUG_NORMAL, "Error #%x\n", hr);
		lpMsgBuf = GetLastErrorStr(GetLastError());

		debug_printf(DEBUG_NORMAL, "Error was : %s", lpMsgBuf);

		LocalFree(lpMsgBuf);

		retval = -1;
		goto done;
	}


	hr = IWbemClassObject_GetMethod(pclsObj, MethodName, 0, &inobj, NULL);
	if (FAILED(hr))
	{
		debug_printf(DEBUG_NORMAL, "Failed to get 'EnableStatic' method!\n");
		debug_printf(DEBUG_NORMAL, "Error #%x\n", hr);
		lpMsgBuf = GetLastErrorStr(GetLastError());

		debug_printf(DEBUG_NORMAL, "Error was : %s", lpMsgBuf);

		LocalFree(lpMsgBuf);

		retval = -1;
		goto done;
	}

    hr = IWbemClassObject_SpawnInstance(inobj, 0, &pClassInstance);
	if (FAILED(hr))
	{
		debug_printf(DEBUG_NORMAL, "Failed to get class instance to execute 'EnableStatic'!\n");
		debug_printf(DEBUG_NORMAL, "Error #%x\n", hr);
		lpMsgBuf = GetLastErrorStr(GetLastError());

		debug_printf(DEBUG_NORMAL, "Error was : %s", lpMsgBuf);

		LocalFree(lpMsgBuf);

		retval = -1;
		goto done;
	}

	iparray = SafeArrayCreateVector(VT_BSTR, 0, 1);
	if (iparray == NULL)
	{
		retval = XEMALLOC;
		goto done;
	}

	maskarray = SafeArrayCreateVector(VT_BSTR, 0, 1);
	if (maskarray == NULL)
	{
		retval = XEMALLOC;
		goto done;
	}

	hr = cardif_windows_wmi_set_safearray_str(iparray, ipaddr);
	if (FAILED(hr))
	{
		debug_printf(DEBUG_NORMAL, "Failed to set the IP address parameter!\n");
		retval = XEMALLOC;
		goto done;
	}

	hr = cardif_windows_wmi_set_safearray_str(maskarray, netmask);
	if (FAILED(hr))
	{
		debug_printf(DEBUG_NORMAL, "Failed to set the netmask parameter!\n");
		retval = XEMALLOC;
		goto done;
	}

	varIpaddr.vt = VT_ARRAY | VT_BSTR;
	varIpaddr.parray = iparray;

	hr = IWbemClassObject_Put(pclsObj, L"IPAddress", 0, &varIpaddr, 0);
	if (FAILED(hr))
	{
		debug_printf(DEBUG_NORMAL, "Failed to embed IP address in object!\n");
		retval = XEMALLOC;
		goto done;
	}

	varNetmask.vt = VT_ARRAY | VT_BSTR;
	varNetmask.parray = maskarray;

	hr = IWbemClassObject_Put(pclsObj, L"SubnetMask", 0, &varNetmask, 0);
	if (FAILED(hr))
	{
		debug_printf(DEBUG_NORMAL, "Failed to embed netmask in object!\n");
		retval = XEMALLOC;
		goto done;
	}

	hr = IWbemServices_ExecMethod(wmiSvc, IntPath, MethodName, 0, //WBEM_FLAG_RETURN_IMMEDIATELY, 
		NULL, pclsObj, &outdata, &pResult);
	if (FAILED(hr))
	{
		debug_printf(DEBUG_NORMAL, "Failed to execute 'EnableStatic' method!\n");
		retval = -1;
		goto done;
	}

done:
	// Clean up.
	if (pclsObj) IWbemClassObject_Release(pclsObj);
	if (pClassInstance) IWbemClassObject_Release(pClassInstance);
	if (inobj) IWbemClassObject_Release(inobj);
	if (outdata) IWbemClassObject_Release(outdata);

	if (iparray) cardif_windows_wmi_destroy_safearray(&iparray);
	if (maskarray) cardif_windows_wmi_destroy_safearray(&maskarray);

	if (MethodName) SysFreeString(MethodName);
	if (IntPath) SysFreeString(IntPath);
	if (ClassName) SysFreeString(ClassName);

	if (retval != 0) return retval;

	return cardif_windows_wmi_async("Set Static IP", ctx, pResult, 
									cardif_windows_wmi_async_static_ip_callback);
}

int cardif_windows_wmi_enable_static(context *ctx, char *ipaddr, char *netmask)
{
	struct win_sock_data *sockData = NULL;
	HRESULT hr;
	VARIANT varIpaddr, varNetmask, varReturnValue;
	IWbemClassObject *pclsObj = NULL, *pClassInstance = NULL;
	IWbemClassObject *inobj = NULL, *outdata = NULL;
	IWbemCallResult *pResult = NULL;
	int retval = XENONE;
	BSTR MethodName = SysAllocString(L"EnableStatic");
	BSTR ClassName = SysAllocString(L"Win32_NetworkAdapterConfiguration");
	BSTR IntPath = NULL;
	SAFEARRAY *iparray = NULL;
	SAFEARRAY *maskarray = NULL;
	LPVOID lpMsgBuf = NULL;
	char apath[100];
	char lpath[200];

	if (ctx == NULL)
	{
		retval = -1;
		goto done;
	}

	sockData = (struct win_sock_data *)ctx->sockData;
	if (sockData == NULL)
	{
		debug_printf(DEBUG_NORMAL, "Couldn't get socket data for context '%s'!?\n", 
			ctx->intName);
		retval = -1;
		goto done;
	}

	if (sockData->wmiIntIdx == INVALID_WMI_IDX)
	{
		// We don't know the index, so locate it.
		if (cardif_windows_wmi_get_idx(ctx, NULL) != XENONE)
		{
			debug_printf(DEBUG_NORMAL, "Couldn't determine WMI interface index in %s()!\n",
					__FUNCTION__);
			retval = -1;
			goto done;
		}
	}

	sprintf(&apath, "Win32_NetworkAdapterConfiguration.Index='%d'", sockData->wmiIntIdx);
	mbstowcs((wchar_t *)&lpath, apath, strlen(apath)+1);

	IntPath = SysAllocString((OLECHAR *)&lpath);
	if (IntPath == NULL)
	{
		debug_printf(DEBUG_NORMAL, "Couldn't create interface path!\n");
		retval = -1;
		goto done;
	}

    hr = IWbemServices_GetObject(wmiSvc, ClassName, 0, NULL, &pclsObj, NULL);
	if (FAILED(hr))
	{
		debug_printf(DEBUG_NORMAL, "Failed to get class handle!\n");
		debug_printf(DEBUG_NORMAL, "Error #%x\n", hr);
		lpMsgBuf = GetLastErrorStr(GetLastError());

		debug_printf(DEBUG_NORMAL, "Error was : %s", lpMsgBuf);

		LocalFree(lpMsgBuf);

		retval = -1;
		goto done;
	}


	hr = IWbemClassObject_GetMethod(pclsObj, MethodName, 0, &inobj, NULL);
	if (FAILED(hr))
	{
		debug_printf(DEBUG_NORMAL, "Failed to get 'EnableStatic' method!\n");
		debug_printf(DEBUG_NORMAL, "Error #%x\n", hr);
		lpMsgBuf = GetLastErrorStr(GetLastError());

		debug_printf(DEBUG_NORMAL, "Error was : %s", lpMsgBuf);

		LocalFree(lpMsgBuf);

		retval = -1;
		goto done;
	}

    hr = IWbemClassObject_SpawnInstance(inobj, 0, &pClassInstance);
	if (FAILED(hr))
	{
		debug_printf(DEBUG_NORMAL, "Failed to get class instance to execute 'EnableStatic'!\n");
		debug_printf(DEBUG_NORMAL, "Error #%x\n", hr);
		lpMsgBuf = GetLastErrorStr(GetLastError());

		debug_printf(DEBUG_NORMAL, "Error was : %s", lpMsgBuf);

		LocalFree(lpMsgBuf);

		retval = -1;
		goto done;
	}

	iparray = SafeArrayCreateVector(VT_BSTR, 0, 1);
	if (iparray == NULL)
	{
		retval = XEMALLOC;
		goto done;
	}

	maskarray = SafeArrayCreateVector(VT_BSTR, 0, 1);
	if (maskarray == NULL)
	{
		retval = XEMALLOC;
		goto done;
	}

	hr = cardif_windows_wmi_set_safearray_str(iparray, ipaddr);
	if (FAILED(hr))
	{
		debug_printf(DEBUG_NORMAL, "Failed to set the IP address parameter!\n");
		retval = XEMALLOC;
		goto done;
	}

	hr = cardif_windows_wmi_set_safearray_str(maskarray, netmask);
	if (FAILED(hr))
	{
		debug_printf(DEBUG_NORMAL, "Failed to set the netmask parameter!\n");
		retval = XEMALLOC;
		goto done;
	}

	varIpaddr.vt = VT_ARRAY | VT_BSTR;
	varIpaddr.parray = iparray;

	hr = IWbemClassObject_Put(pclsObj, L"IPAddress", 0, &varIpaddr, 0);
	if (FAILED(hr))
	{
		debug_printf(DEBUG_NORMAL, "Failed to embed IP address in object!\n");
		retval = XEMALLOC;
		goto done;
	}

	varNetmask.vt = VT_ARRAY | VT_BSTR;
	varNetmask.parray = maskarray;

	hr = IWbemClassObject_Put(pclsObj, L"SubnetMask", 0, &varNetmask, 0);
	if (FAILED(hr))
	{
		debug_printf(DEBUG_NORMAL, "Failed to embed netmask in object!\n");
		retval = XEMALLOC;
		goto done;
	}

	hr = IWbemServices_ExecMethod(wmiSvc, IntPath, MethodName, WBEM_FLAG_RETURN_IMMEDIATELY, 
		NULL, pclsObj, &outdata, &pResult);
	if (FAILE

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?