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

📄 pagedevice.cpp

📁 PCI的一个测试程序,可以测试PCI driver和BOARD的功能.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// PageDevice.cpp : 实现文件
//

#include "stdafx.h"
#include "PCIEBoardTest.h"
#include "TestFunc.h"
#include "PageDevice.h"

#include "PageTest.h"
#include "Pagedebug.h"
#include "PageChart.h"
#include "PCIEBoardTestDlg.h"
#include "public.h"


#include <setupapi.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include ".\pagedevice.h"

int iItemSize[16] = {0x20};
int	iItemCount    = 1;

int iDeviceReadSize  = 0x20;
int iDeviceWriteSize = 0x20;

const char * CopyRight = "Version 1.0\n"
"2007- All rights reserved by Token2000@163.com\n\n";

//DEFINE_GUID(GUID_PCIE_INTERFACE, 
//	0x133b9c58, 0x7988, 0x4bf2, 0xb2, 0x34, 0xfa, 0xad, 0x8a, 0x74, 0xc8, 0xfb);

GUID GUID_PCIE_INTERFACE = { 0x133b9c58, 0x7988, 0x4bf2, {0xb2, 0x34, 0xfa, 0xad, 0x8a, 0x74, 0xc8, 0xfb} };

// CPageDevice 对话框

IMPLEMENT_DYNAMIC(CPageDevice, CDialog)
CPageDevice::CPageDevice(CWnd* pParent /*=NULL*/)
	: CDialog(CPageDevice::IDD, pParent)
{
}

CPageDevice::~CPageDevice()
{
}

void CPageDevice::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Control(pDX, IDC_COMBO_DEVICE, m_comboDeviceList);
}


BEGIN_MESSAGE_MAP(CPageDevice, CDialog)
    ON_WM_TIMER()
    ON_WM_CREATE()
    ON_BN_CLICKED(IDC_BUTTON_OPENDEVICE, OnBnClickedButtonOpendevice)
//    ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButton2)
ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel)
ON_CBN_SELCHANGE(IDC_COMBO_DEVICE, OnCbnSelchangeComboDevice)
//ON_WM_CLOSE()
ON_WM_DESTROY()
END_MESSAGE_MAP()


// CPageDevice 消息处理程序

void CPageDevice::OnTimer(UINT nIDEvent)
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    if (m_comboDeviceList.GetCount() == 0)
    {
        // 遍历所有设备,以将设备列表加入到combolist中
        CList<CString, CString&>    listDevice;

        GetDeviceList(listDevice);

        POSITION    pos = listDevice.GetHeadPosition();
        CString     strInfor;
        int         i = 0;

        while(pos)
        {
            strInfor = listDevice.GetNext(pos);

            m_comboDeviceList.InsertString(i, strInfor);

            ++i;
        }
        
        if (m_comboDeviceList.GetCount() > 0)
        {
            m_comboDeviceList.SetCurSel(0);
        }
    }

    if (GetDlg()->GetDeviceHandle() != INVALID_HANDLE_VALUE)
    {
        CString strDevInfor;
        CString strInfor;

        GetDlg()->GetDeviceInfor(strDevInfor);

        strInfor  = CopyRight;
        strInfor += "\nDevice connected: \n";
        strInfor += strDevInfor;

        GetDlgItem(IDC_STATIC_OPENINFOR)->SetWindowText(strInfor);
    }
    else
    {
        CString strInfor;
        strInfor  = CopyRight;
        strInfor += "\nNo Device connected";
        GetDlgItem(IDC_STATIC_OPENINFOR)->SetWindowText(strInfor);
    }

    CDialog::OnTimer(nIDEvent);
}

int CPageDevice::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CDialog::OnCreate(lpCreateStruct) == -1)
        return -1;

    // TODO:  在此添加您专用的创建代码
    SetTimer(1, 500, NULL);

    return 0;
}


int CPageDevice::GetDeviceList(CList<CString, CString&>& p_devList)
{
    HDEVINFO hDevInfo;
    PSP_DEVICE_INTERFACE_DETAIL_DATA pDeviceInterfaceDetail;
    SP_DEVICE_INTERFACE_DATA DeviceInterfaceData;
    SP_DEVINFO_DATA DeviceInfoData;

    ULONG size;
    int count, i;
    BOOL status = TRUE;
    TCHAR *DeviceName = NULL;
    TCHAR *DeviceLocation = NULL;

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

    //
    //  Initialize the SP_DEVICE_INTERFACE_DATA Structure.
    //
    DeviceInterfaceData.cbSize  = sizeof(SP_DEVICE_INTERFACE_DATA);

    //
    //  Determine how many devices are present.
    //
    count = 0;
    while(SetupDiEnumDeviceInterfaces(hDevInfo,
                                      NULL,
                                      &GUID_PCIE_INTERFACE,
                                      count++,  //Cycle through the available devices.
                                      &DeviceInterfaceData)
          );

    //
    // Since the last call fails when all devices have been enumerated,
    // decrement the count to get the true device count.
    //
    count--;

    //
    //  If the count is zero then there are no devices present.
    //
    if (count == 0)
    {
        return 0;
    }

    //
    //  Initialize the appropriate data structures in preparation for
    //  the SetupDi calls.
    //
    DeviceInterfaceData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
    DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);

    //
    //  Loop through the device list to allow user to choose
    //  a device.  If there is only one device, select it
    //  by default.
    //
    i = 0;
    while (SetupDiEnumDeviceInterfaces(hDevInfo,
                                       NULL,
                                       (LPGUID)&GUID_PCIE_INTERFACE,
                                       i,
                                       &DeviceInterfaceData))
    {

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

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

        pDeviceInterfaceDetail = (PSP_DEVICE_INTERFACE_DETAIL_DATA) malloc(size);

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

        //
        // Initialize structure and retrieve data.
        //
        pDeviceInterfaceDetail->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
        status = SetupDiGetDeviceInterfaceDetail(hDevInfo,
                                                 &DeviceInterfaceData,
                                                 pDeviceInterfaceDetail,
                                                 size,
                                                 NULL,
                                                 &DeviceInfoData);

        free(pDeviceInterfaceDetail);

        if (!status)
        {
            return 0;
        }

        //
        //  Get the Device Name
        //  Calls to SetupDiGetDeviceRegistryProperty require two consecutive
        //  calls, first to get required buffer size and second to get
        //  the data.
        //
        SetupDiGetDeviceRegistryProperty(hDevInfo,
                                        &DeviceInfoData,
                                        SPDRP_DEVICEDESC,
                                        NULL,
                                        (PBYTE)DeviceName,
                                        0,
                                        &size);

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

        DeviceName = (TCHAR*) malloc(size);
        if (!DeviceName)
        {
            AfxMessageBox("Insufficient memory.");
            return 0;
        }

        status = SetupDiGetDeviceRegistryProperty(hDevInfo,
                                                  &DeviceInfoData,
                                                  SPDRP_DEVICEDESC,
                                                  NULL,
                                                  (PBYTE)DeviceName,
                                                  size,
                                                  NULL);
        if (!status)
        {
            free(DeviceName);
            return 0;
        }

        //
        //  Now retrieve the Device Location.
        //
        SetupDiGetDeviceRegistryProperty(hDevInfo,
                                         &DeviceInfoData,
                                         SPDRP_LOCATION_INFORMATION,
                                         NULL,
                                         (PBYTE)DeviceLocation,
                                         0,
                                         &size);

⌨️ 快捷键说明

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