16,548
社区成员




// Example code to retrieve vendor and device ID's for the primary display
// device.
//
// NOTE: Visual Studio 6 does not ship with a winuser.h that defines the
// EnumDisplayDevices function so in order to compile this code, you'll
// need to install the Platform SDK.
#include "stdafx.h"
#include <windows.h>
#include <string>
#include <iostream>
using namespace std;
bool GetDeviceIdentification(string &vendorID, string &deviceID)
{
DISPLAY_DEVICE dd;
dd.cb = sizeof(DISPLAY_DEVICE);
int i = 0;
string id;
// locate primary display device
while (EnumDisplayDevices(NULL, i, &dd, 0))
{
if (dd.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE)
{
id = dd.DeviceID;
break;
}
i++;
}
if (id == "") return false;
// get vendor ID
vendorID = id.substr(8, 4);
// get device ID
deviceID = id.substr(17, 4);
return true;
}
int main(void)
{
string vendorID;
string deviceID;
if (GetDeviceIdentification(vendorID, deviceID))
{
cout << "Vendor ID: " << vendorID << endl;
cout << "Device ID: " << deviceID << endl;
}
else
cout << "Unable to locate device information" << endl;
cin.get();
return 0;
}
#pragma pack(push, 8)
typedef struct _DISPLAY_DEVICEEX {
DWORD cb;
TCHAR DeviceName[32];
TCHAR DeviceString[128];
DWORD StateFlags;
TCHAR DeviceID[128];
TCHAR DeviceKey[128];
} DISPLAY_DEVICEEX, *PDISPLAY_DEVICEEX;
#pragma pack(pop)
DISPLAY_DEVICEEX dd = { sizeof(DISPLAY_DEVICEEX) };
int iDisp = 0;
while( EnumDisplayDevices(NULL, iDisp++, (DISPLAY_DEVICE*)&dd, 0) )
{
TRACE(_T("Name=[%s] Str=[%s] Flag=%08XH ID=[%s] Key=[%s]\n"),
dd.DeviceName, dd.DeviceString, dd.StateFlags, dd.DeviceID, dd.DeviceKey);
}