Example #1
0
File: pp.c Project: kcrazy/winekit
void InitPortParams(
    IN OUT PPORT_PARAMS      Params,
    IN HDEVINFO              DeviceInfoSet,
    IN PSP_DEVINFO_DATA      DeviceInfoData
    )
{
    BOOL                        showAdvanced = TRUE;
    SP_DEVINFO_LIST_DETAIL_DATA detailData;

    ZeroMemory(Params, sizeof(PORT_PARAMS));

    Params->DeviceInfoSet = DeviceInfoSet;
    Params->DeviceInfoData = DeviceInfoData;
    Params->ChangesEnabled = TRUE;

    //
    // Now we know how big our structure is, so we can allocate memory
    //
    Params->pAdvancedData =
        (PADVANCED_DATA) LocalAlloc(LPTR, sizeof(ADVANCED_DATA));

    if (Params->pAdvancedData == NULL) {
        //
        // Not enough memory
        //
        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
        showAdvanced = FALSE;
    }
    else
    {
        Params->pAdvancedData->HidePolling = FALSE;
    }
    //
    // See if we are being invoked locally or over the network.  If over the net,
    // then disable all possible changes.
    //
    detailData.cbSize = sizeof(SP_DEVINFO_LIST_DETAIL_DATA);
    if (SetupDiGetDeviceInfoListDetail(DeviceInfoSet, &detailData) &&
        detailData.RemoteMachineHandle != NULL) {
        showAdvanced = FALSE;
        Params->ChangesEnabled = FALSE;
    }


    if (Params->pAdvancedData)
    {
        Params->pAdvancedData->DeviceInfoSet  = DeviceInfoSet;
        Params->pAdvancedData->DeviceInfoData = DeviceInfoData;
    }
    
    Params->ShowAdvanced = showAdvanced;
}
Example #2
0
BOOL GetDevStatus(HDEVINFO h, SP_DEVINFO_DATA *dev_info_data, PULONG status, PULONG problem)
{
    SP_DEVINFO_LIST_DETAIL_DATA detail;

    memset(&detail, 0, sizeof(detail));
    detail.cbSize = sizeof(detail);

    SetupDiGetDeviceInfoListDetail(h, &detail);
    if (CM_Get_DevNode_Status_Ex(status,
                                 problem,
                                 dev_info_data->DevInst,
                                 0,
                                 detail.RemoteMachineHandle) != CR_SUCCESS) {

        printf("SetupDiGetDeviceInfoListDetail: %x\n", GetLastError());
        return FALSE;
    }
    return TRUE;
}
BOOL DeleteDevice(HDEVINFO info, SP_DEVINFO_DATA *dev_info_data)
{
	SP_REMOVEDEVICE_PARAMS p;
	SP_DEVINFO_LIST_DETAIL_DATA detail;
	char device_id[MAX_PATH];
	if (info == NULL || dev_info_data == NULL)
	{
		return FALSE;
	}

	memset(&detail, 0, sizeof(detail));
	detail.cbSize = sizeof(detail);

	if (SetupDiGetDeviceInfoListDetail(info, &detail) == FALSE ||
		CM_Get_Device_ID_Ex(dev_info_data->DevInst, device_id, sizeof(device_id),
		0, detail.RemoteMachineHandle) != CR_SUCCESS)
	{
		logError("CM_Get_Device_ID_Ex: %x\n", GetLastError());
		return FALSE;
	}

	memset(&p, 0, sizeof(p));
	p.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
	p.ClassInstallHeader.InstallFunction = DIF_REMOVE;
	p.Scope = DI_REMOVEDEVICE_GLOBAL;

	if (SetupDiSetClassInstallParams(info, dev_info_data, &p.ClassInstallHeader, sizeof(p)) == FALSE)
	{
		logError("SetupDiSetClassInstallParams: %x\n", GetLastError());
		return FALSE;
	}

	if (SetupDiCallClassInstaller(DIF_REMOVE, info, dev_info_data) == FALSE)
	{
		logError("SetupDiCallClassInstaller: %x\n", GetLastError());
		return FALSE;
	}

	return TRUE;
}
Example #4
0
BOOL DumpDeviceWithInfo(_In_ HDEVINFO Devs, _In_ PSP_DEVINFO_DATA DevInfo, _In_opt_ LPCTSTR Info)
/*++

Routine Description:

    Write device instance & info to stdout

Arguments:

    Devs    )_ uniquely identify device
    DevInfo )

Return Value:

    none

--*/
{
    TCHAR devID[MAX_DEVICE_ID_LEN];
    BOOL b = TRUE;
    SP_DEVINFO_LIST_DETAIL_DATA devInfoListDetail;

    devInfoListDetail.cbSize = sizeof(devInfoListDetail);
    if((!SetupDiGetDeviceInfoListDetail(Devs,&devInfoListDetail)) ||
            (CM_Get_Device_ID_Ex(DevInfo->DevInst,devID,MAX_DEVICE_ID_LEN,0,devInfoListDetail.RemoteMachineHandle)!=CR_SUCCESS)) {
        StringCchCopy(devID, ARRAYSIZE(devID), TEXT("?"));
        b = FALSE;
    }

    if(Info) {
        _tprintf(TEXT("%-60s: %s\n"),devID,Info);
    } else {
        _tprintf(TEXT("%s\n"),devID);
    }
    return b;
}
Example #5
0
void
LptInitPropParams(IN OUT PLPT_PROP_PARAMS  Params,
                  IN HDEVINFO              DeviceInfoSet,
                  IN PSP_DEVINFO_DATA      DeviceInfoData)
{
    SP_DEVINFO_LIST_DETAIL_DATA detailData;

    ZeroMemory(Params, sizeof(LPT_PROP_PARAMS));

    Params->DeviceInfoSet = DeviceInfoSet;
    Params->DeviceInfoData = DeviceInfoData;
    Params->ChangesEnabled = TRUE;

    //
    // See if we are being invoked locally or over the network.  If over the net,
    // then disable all possible changes.
    //
    detailData.cbSize = sizeof(SP_DEVINFO_LIST_DETAIL_DATA);
    if (SetupDiGetDeviceInfoListDetail(DeviceInfoSet, &detailData) &&
        detailData.RemoteMachineHandle != NULL) {
        Params->ChangesEnabled = FALSE;
    }

} // LptInitPropParams
Example #6
0
bool remove(const std::wstring& hardware_id, bool& reboot_required)
{
	bool result = false;

	HDEVINFO devices = SetupDiGetClassDevsEx(NULL, NULL, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT, NULL, NULL, NULL);

	if (devices != INVALID_HANDLE_VALUE)
	{
		std::wcerr << "Got device information set." << std::endl;

		SP_DEVINFO_LIST_DETAIL_DATA device_info_list_detail;
		memset(&device_info_list_detail, 0x00, sizeof(device_info_list_detail));
		device_info_list_detail.cbSize = sizeof(device_info_list_detail);

		if (SetupDiGetDeviceInfoListDetail(devices, &device_info_list_detail))
		{
			std::wcerr << "Got device information list details." << std::endl;

			SP_DEVINFO_DATA device_info;
			device_info.cbSize = sizeof(device_info);

			for (DWORD index = 0; SetupDiEnumDeviceInfo(devices, index, &device_info); ++index)
			{
				TCHAR device_id[MAX_DEVICE_ID_LEN];

				if (CM_Get_Device_ID_Ex(device_info.DevInst, device_id, MAX_DEVICE_ID_LEN, 0, device_info_list_detail.RemoteMachineHandle) == CR_SUCCESS)
				{
					std::list<std::wstring> device_hardware_id_list;

					if (getDeviceProperty(devices, device_info, SPDRP_HARDWAREID, device_hardware_id_list))
					{
						bool match = false;

						for (std::list<std::wstring>::const_iterator device_hardware_id = device_hardware_id_list.begin(); device_hardware_id != device_hardware_id_list.end(); ++device_hardware_id)
						{
							if (*device_hardware_id == hardware_id)
							{
								match = true;

								break;
							}
						}

						if (match)
						{
							std::wstring friendly_name;

							if (getDeviceProperty(devices, device_info, SPDRP_FRIENDLYNAME, friendly_name) && (friendly_name.length() > 0))
							{
								std::wcerr << "Removing device: " << friendly_name << " (" << device_id << ")" << std::endl;
							} else
							{
								std::wcerr << "Removing device: " << device_id << std::endl;
							}

							SP_REMOVEDEVICE_PARAMS remove_device_params;
							remove_device_params.ClassInstallHeader.cbSize = sizeof(SP_CLASSINSTALL_HEADER);
							remove_device_params.ClassInstallHeader.InstallFunction = DIF_REMOVE;
							remove_device_params.Scope = DI_REMOVEDEVICE_GLOBAL;
							remove_device_params.HwProfile = 0;

							result = true;

							if (!SetupDiSetClassInstallParams(devices, &device_info, &remove_device_params.ClassInstallHeader, sizeof(remove_device_params)) || 
									!SetupDiCallClassInstaller(DIF_REMOVE, devices, &device_info))
							{
								std::wcerr << "Failed to set the class installer." << std::endl;

								result = false;
							}

							if (result)
							{
								reboot_required = false;

								SP_DEVINSTALL_PARAMS device_params;

								if (SetupDiGetDeviceInstallParams(devices, &device_info, &device_params))
								{
									if (device_params.Flags & (DI_NEEDRESTART | DI_NEEDREBOOT))
									{
										reboot_required = true;
									}
								}
							}
						}
					}
				}
			}
		}

		SetupDiDestroyDeviceInfoList(devices);
	}

	return result;
}
Example #7
0
BOOL DumpDeviceResources(_In_ HDEVINFO Devs, _In_ PSP_DEVINFO_DATA DevInfo)
/*++

Routine Description:

    Dump Resources to stdout

Arguments:

    Devs    )_ uniquely identify device
    DevInfo )

Return Value:

    none

--*/
{
    SP_DEVINFO_LIST_DETAIL_DATA devInfoListDetail;
    ULONG status = 0;
    ULONG problem = 0;
    LOG_CONF config = 0;
    BOOL haveConfig = FALSE;

    //
    // see what state the device is in
    //
    devInfoListDetail.cbSize = sizeof(devInfoListDetail);
    if((!SetupDiGetDeviceInfoListDetail(Devs,&devInfoListDetail)) ||
            (CM_Get_DevNode_Status_Ex(&status,&problem,DevInfo->DevInst,0,devInfoListDetail.RemoteMachineHandle)!=CR_SUCCESS)) {
        return FALSE;
    }

    //
    // see if the device is running and what resources it might be using
    //
    if(!(status & DN_HAS_PROBLEM)) {
        //
        // If this device is running, does this devinst have a ALLOC log config?
        //
        if (CM_Get_First_Log_Conf_Ex(&config,
                                     DevInfo->DevInst,
                                     ALLOC_LOG_CONF,
                                     devInfoListDetail.RemoteMachineHandle) == CR_SUCCESS) {
            haveConfig = TRUE;
        }
    }
    if(!haveConfig) {
        //
        // If no config so far, does it have a FORCED log config?
        // (note that technically these resources might be used by another device
        // but is useful info to show)
        //
        if (CM_Get_First_Log_Conf_Ex(&config,
                                     DevInfo->DevInst,
                                     FORCED_LOG_CONF,
                                     devInfoListDetail.RemoteMachineHandle) == CR_SUCCESS) {
            haveConfig = TRUE;
        }
    }

    if(!haveConfig) {
        //
        // if there's a hardware-disabled problem, boot-config isn't valid
        // otherwise use this if we don't have anything else
        //
        if(!(status & DN_HAS_PROBLEM) || (problem != CM_PROB_HARDWARE_DISABLED)) {
            //
            // Does it have a BOOT log config?
            //
            if (CM_Get_First_Log_Conf_Ex(&config,
                                         DevInfo->DevInst,
                                         BOOT_LOG_CONF,
                                         devInfoListDetail.RemoteMachineHandle) == CR_SUCCESS) {
                haveConfig = TRUE;
            }
        }
    }

    if(!haveConfig) {
        //
        // if we don't have any configuration, display an apropriate message
        //
        Padding(1);
        FormatToStream(stdout,(status & DN_STARTED) ? MSG_DUMP_NO_RESOURCES : MSG_DUMP_NO_RESERVED_RESOURCES );
        return TRUE;
    }
    Padding(1);
    FormatToStream(stdout,(status & DN_STARTED) ? MSG_DUMP_RESOURCES : MSG_DUMP_RESERVED_RESOURCES );

    //
    // dump resources
    //
    DumpDeviceResourcesOfType(DevInfo->DevInst,devInfoListDetail.RemoteMachineHandle,config,ResType_All);

    //
    // release handle
    //
    CM_Free_Log_Conf_Handle(config);

    return TRUE;
}
Example #8
0
BOOL DumpDeviceStatus(_In_ HDEVINFO Devs, _In_ PSP_DEVINFO_DATA DevInfo)
/*++

Routine Description:

    Write device status to stdout

Arguments:

    Devs    )_ uniquely identify device
    DevInfo )

Return Value:

    none

--*/
{
    SP_DEVINFO_LIST_DETAIL_DATA devInfoListDetail;
    ULONG status = 0;
    ULONG problem = 0;
    BOOL hasInfo = FALSE;
    BOOL isPhantom = FALSE;
    CONFIGRET cr = CR_SUCCESS;

    devInfoListDetail.cbSize = sizeof(devInfoListDetail);
    if((!SetupDiGetDeviceInfoListDetail(Devs,&devInfoListDetail)) ||
            ((cr = CM_Get_DevNode_Status_Ex(&status,&problem,DevInfo->DevInst,0,devInfoListDetail.RemoteMachineHandle))!=CR_SUCCESS)) {
        if ((cr == CR_NO_SUCH_DEVINST) || (cr == CR_NO_SUCH_VALUE)) {
            isPhantom = TRUE;
        } else {
            Padding(1);
            FormatToStream(stdout,MSG_DUMP_STATUS_ERROR);
            return FALSE;
        }
    }
    //
    // handle off the status/problem codes
    //
    if (isPhantom) {
        Padding(1);
        FormatToStream(stdout,MSG_DUMP_PHANTOM);
        return TRUE;
    }
    if((status & DN_HAS_PROBLEM) && problem == CM_PROB_DISABLED) {
        hasInfo = TRUE;
        Padding(1);
        FormatToStream(stdout,MSG_DUMP_DISABLED);
        return TRUE;
    }
    if(status & DN_HAS_PROBLEM) {
        hasInfo = TRUE;
        Padding(1);
        FormatToStream(stdout,MSG_DUMP_PROBLEM,problem);
    }
    if(status & DN_PRIVATE_PROBLEM) {
        hasInfo = TRUE;
        Padding(1);
        FormatToStream(stdout,MSG_DUMP_PRIVATE_PROBLEM);
    }
    if(status & DN_STARTED) {
        Padding(1);
        FormatToStream(stdout,MSG_DUMP_STARTED);
    } else if (!hasInfo) {
        Padding(1);
        FormatToStream(stdout,MSG_DUMP_NOTSTARTED);
    }
    return TRUE;
}
Example #9
0
HDEVINFO GetDevInfoFromDeviceId(SP_DEVINFO_DATA *dev_info_data, CHAR *device_id)
{
    HDEVINFO dev_info;
    SP_DEVINFO_DATA data;
    UINT i;
    BOOL found;
    CHAR *buffer;
    UINT buffer_size = 8092;
    DWORD required_size;
    DWORD data_type;

    dev_info = SetupDiGetClassDevsEx(NULL, NULL, NULL, DIGCF_ALLCLASSES | DIGCF_PRESENT, NULL, NULL, NULL);
    if (dev_info == NULL)
    {
        return NULL;
    }

#if 0
    SP_DEVINFO_LIST_DETAIL_DATA detail_data;
    memset(&detail_data, 0, sizeof(detail_data));
    detail_data.cbSize = sizeof(detail_data);
    if (SetupDiGetDeviceInfoListDetail(dev_info, &detail_data) == FALSE)
    {
        DestroyDevInfo(dev_info);
        return NULL;
    }
#endif

    memset(&data, 0, sizeof(data));
    data.cbSize = sizeof(data);
    found = FALSE;
    buffer = (LPTSTR)LocalAlloc(LPTR, buffer_size);
    if (!buffer) {
        printf("Alloc: %x\n", GetLastError());
        goto out;
    }

    for (i = 0; SetupDiEnumDeviceInfo(dev_info, i, &data); i++) {
        while (!SetupDiGetDeviceRegistryProperty(dev_info,
                &data,
                SPDRP_HARDWAREID,
                &data_type,
                (PBYTE)buffer,
                buffer_size,
                &required_size)) {
            if (ERROR_INSUFFICIENT_BUFFER == GetLastError()) {
                // Change the buffer size.
                if (buffer) {
                    LocalFree(buffer);
                }
                // Double the size to avoid problems on
                // W2k MBCS systems per KB 888609.
                buffer_size *= 2;
                buffer = (LPTSTR)LocalAlloc(LPTR, buffer_size);
                if (!buffer) {
                    printf("LocalAlloc: %x\n", GetLastError());
                    goto out;
                }
            } else {
                // EnumNext
                break;
            }
        }

        if (stricmp(buffer, device_id) == 0) {
            found = TRUE;
        }

        if (found) {
            goto out;
        }

        memset(&data, 0, sizeof(data));
        data.cbSize = sizeof(data);
    }
out:
    if (buffer)
        LocalFree(buffer);
    if (found == FALSE) {
        DestroyDevInfo(dev_info);
        return NULL;
    } else {
        memcpy(dev_info_data, &data, sizeof(data));
        return dev_info;
    }
}