⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pagedevice.cpp

📁 PCI的一个测试程序,可以测试PCI driver和BOARD的功能.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
        if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
        {
            DeviceLocation = (TCHAR*) malloc(size);

            if (DeviceLocation != NULL)
            {
                status = SetupDiGetDeviceRegistryProperty(hDevInfo,
                                                          &DeviceInfoData,
                                                          SPDRP_LOCATION_INFORMATION,
                                                          NULL,
                                                          (PBYTE)DeviceLocation,
                                                          size,
                                                          NULL);
                if (!status)
                {
                    free(DeviceLocation);
                    DeviceLocation = NULL;
                }
            }

        }
        else
        {
            DeviceLocation = NULL;
        }

        //
        // If there is more than one device print description.
        //
        CString strInfor;

        strInfor.Format("%d- ", i);

        strInfor += DeviceName;

        if (DeviceLocation) {
            strInfor += "        ";
            strInfor +=  DeviceLocation;
        }

        p_devList.AddTail(strInfor);

        free(DeviceName);
        DeviceName = NULL;

        if (DeviceLocation) {
            free(DeviceLocation);
            DeviceLocation = NULL;
        }

        i++; // Cycle through the available devices.
    }

    return i;
}


HANDLE  CPageDevice::OpenDevice(int index, LPCTSTR p_szDeviceName)
{
    HDEVINFO hDevInfo;
    PSP_DEVICE_INTERFACE_DETAIL_DATA pDeviceInterfaceDetail;
    SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
    SP_DEVINFO_DATA DeviceInfoData;
    ULONG size;
    HANDLE hDevice;
    BOOL status = TRUE;
    int     i;

    //
    //  Retreive the device information for all PCIE devices.
    //
    hDevInfo = SetupDiGetClassDevs(&GUID_PCIE_INTERFACE,
                                   NULL,
                                   NULL,
                                   DIGCF_DEVICEINTERFACE |
                                   DIGCF_PRESENT);


    DeviceInterfaceData.cbSize  = sizeof(SP_DEVICE_INTERFACE_DATA);

    //
    //  Get information for specific device.
    //
    status = SetupDiEnumDeviceInterfaces(hDevInfo,
                                    NULL,
                                    (LPGUID)&GUID_PCIE_INTERFACE,
                                    index,
                                    &DeviceInterfaceData);

    if (!status)
    {
        return INVALID_HANDLE_VALUE;
    }

    //
    // Determine the size required for the DeviceInterfaceData
    //
    SetupDiGetDeviceInterfaceDetail(hDevInfo,
                                    &DeviceInterfaceData,
                                    NULL,
                                    0,
                                    &size,
                                    NULL);

    if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
    {
        return INVALID_HANDLE_VALUE;
    }

    pDeviceInterfaceDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA) malloc(size);

    if (!pDeviceInterfaceDetail)
    {
        AfxMessageBox("Insufficient memory.");
        return INVALID_HANDLE_VALUE;
    }

    //
    // Initialize structure and retrieve data.
    //
    pDeviceInterfaceDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
    
    DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
    DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

    status = SetupDiGetDeviceInterfaceDetail(hDevInfo,
                                             &DeviceInterfaceData,
                                             pDeviceInterfaceDetail,
                                             size,
                                             NULL,
                                             &DeviceInfoData);
    if (!status)
    {
        DWORD   dwError = GetLastError();
        return INVALID_HANDLE_VALUE;
    }

    hDevice = CreateFile(pDeviceInterfaceDetail->DevicePath,
                             GENERIC_READ|GENERIC_WRITE,
                             FILE_SHARE_READ | FILE_SHARE_WRITE,
                             NULL,
                             OPEN_EXISTING,
                             0,
                             NULL);

    if (hDevice == INVALID_HANDLE_VALUE)
    {
        CString strInfor;
        strInfor.Format("CreateFile failed.  Error:%d", GetLastError());
        AfxMessageBox(strInfor);
    }
    else
	{
		// decide PCI_FREQ
        char *p = strstr(p_szDeviceName, "Mhz");
		char *q = NULL;

		if (!p)
			p = strstr(p_szDeviceName, "MHZ");
		if (!p)
			p = strstr(p_szDeviceName, "mhz");


		while(p > p_szDeviceName && !(*p >= '0' && *p <= '9' || *p == '.'))
			--p;

		q = p;

		while(p > p_szDeviceName && (*p >= '0' && *p <= '9' || *p == '.'))
			--p;

		if (p > p_szDeviceName)
		{
			char szFreq[64];
			
			memset(szFreq, 0, sizeof(szFreq));

			strncpy(szFreq, p + 1, q - p);

			PCI_FREQ = 1000.0 / atof(szFreq);

			GetDlg()->Log(CString("PCI_FREQ is ") + szFreq + " MHZ");
		}
		else
		{
			PCI_FREQ = 1000.0 / atof(DEFAULT_PCI_FREQ);

			GetDlg()->Log(CString("PCI_FREQ is set to default: ") + DEFAULT_PCI_FREQ + " MHZ");
		}
	}


	free(pDeviceInterfaceDetail);

    return hDevice;
}

void CPageDevice::OnBnClickedButtonOpendevice()
{
    // TODO: 在此添加控件通知处理程序代码
    if (m_comboDeviceList.GetCount() <= 0)
        return ;

    if (m_comboDeviceList.GetCurSel() < 0)
        return;

    CString		strDeviceName;
	m_comboDeviceList.GetLBText(m_comboDeviceList.GetCurSel(), strDeviceName);

	HANDLE  hDevice = OpenDevice( m_comboDeviceList.GetCurSel(), strDeviceName);

    if (hDevice == INVALID_HANDLE_VALUE)
    {
        AfxMessageBox("Open Failed");
        return;
    }

    CString strInfor;
    
    m_comboDeviceList.GetLBText(m_comboDeviceList.GetCurSel(), strInfor);
    
    if (GetDlg()->GetDeviceHandle() != INVALID_HANDLE_VALUE)
    {
        Test_FreeUserAddress(GetDlg()->GetDeviceHandle());
        CloseHandle(GetDlg()->GetDeviceHandle());
        GetDlg()->SetBufferAddress_h2d(NULL, NULL, 0);
        GetDlg()->SetBufferAddress_d2h(NULL, NULL, 0);

        GetDlg()->Log("Device Disconnected.");
    }

    GetDlg()->Log("Device Connected.");

    GetDlg()->SetDeviceHandle(hDevice);
    GetDlg()->SetDeviceInfor(strInfor);

    ULONG  outBuffer[6];

    if (!Test_GetUserAddress(hDevice, outBuffer))
    {
        CloseHandle(GetDlg()->GetDeviceHandle());
        GetDlg()->SetDeviceHandle(INVALID_HANDLE_VALUE);

        GetDlg()->Log("Device Disconnected.");
    }

    GetDlg()->SetBufferAddress_h2d((void *)outBuffer[0], (void *)outBuffer[1], outBuffer[2]);
    GetDlg()->SetBufferAddress_d2h((void *)outBuffer[3], (void *)outBuffer[4], outBuffer[5]);
}

//void CPageDevice::OnBnClickedButton2()
//{
//    // TODO: 在此添加控件通知处理程序代码
//}

void CPageDevice::OnBnClickedCancel()
{
    // TODO: 在此添加控件通知处理程序代码
//    OnCancel();
}

void CPageDevice::OnCbnSelchangeComboDevice()
{
    // TODO: 在此添加控件通知处理程序代码
    if (m_comboDeviceList.GetCount() > 0 && m_comboDeviceList.GetCurSel() >= 0)
    {
        CString     strInfor;
        m_comboDeviceList.GetLBText(m_comboDeviceList.GetCurSel(), strInfor);

        GetDlgItem(IDC_STATIC_DEVICEINFOR)->SetWindowText(strInfor);
    }
}

//void CPageDevice::OnClose()
//{
//    // TODO: 在此添加消息处理程序代码和/或调用默认值
//
//    CDialog::OnClose();
//}

void CPageDevice::OnDestroy()
{
    CDialog::OnDestroy();

    // TODO: 在此处添加消息处理程序代码
    if (GetDlg()->GetDeviceHandle() != INVALID_HANDLE_VALUE)
    {
        Test_FreeUserAddress(GetDlg()->GetDeviceHandle());
        CloseHandle(GetDlg()->GetDeviceHandle());
        GetDlg()->SetDeviceHandle(INVALID_HANDLE_VALUE);

        GetDlg()->Log("Device Disconnected.");
    }
}

⌨️ 快捷键说明

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