EnumDisplayDevices function

Hello,

I am trying to get the DISPLAY_DEVICE structure of a multi-display Windows system.

In my test system, the function returns TRUE up to the 4th device (DevNum = 4) and then is FALSE for higher values, as expected by the function description:
link text

Here is the code:

Dim DevNum As UInt32
Dim mbDisplayDevice As MemoryBlock

Soft Declare Function EnumDisplayDevices Lib "user32" Alias "EnumDisplayDevicesA" (lpDevice As Ptr, iDevNum As UInt32, lpDisplayDevice As Ptr, dwFlags As UInt32) As Boolean

      DevNum = 0
      mbDisplayDevice = New MemoryBlock(424)  // = 4 + 32 + 128 + 4 + 128 + 128

      While EnumDisplayDevices(nil, DevNum, mbDisplayDevice, 0)
        // when TRUE, look within mbDisplayDevice but always filled with zeros!
        DevNum = DevNum + 1
      Wend

The output data should be available in the lpDisplayDevice MemoryBlock but this block is always filled with zeros (I looked at each iteration of DevNum!).
It should be formatted with the following structure (from Microsoft):

    typedef struct _DISPLAY_DEVICE {
    DWORD cb;
    TCHAR DeviceName[32];
    TCHAR DeviceString[128];
    DWORD StateFlags;
    TCHAR DeviceID[128];
    TCHAR DeviceKey[128];
    } DISPLAY_DEVICE, *PDISPLAY_DEVICE;

According to the function doc, the first function call is done with lpDevice = nil and the other calls (not shown in the code above) should be done with the proper lpDevice string found from the nil call. Since the MemoryBlock is filled with zeros, I cannot go deeper.

Thanks in advance for any help!

From the docs:

i.e.:

mbDisplayDevice.Int32Value(0) = mbDisplayDevice.Size

Hi Danny,

You need to set the first DWORD part of the mbDisplayDevice memoryblock to the size of the memory block. Something like this should do it:

[code] Dim DevNum As UInt32
Dim mbDisplayDevice As MemoryBlock

Soft Declare Function EnumDisplayDevices Lib “user32” Alias “EnumDisplayDevicesA” _
(lpDevice As Ptr, iDevNum As UInt32, lpDisplayDevice As Ptr, dwFlags As UInt32) As Boolean

DevNum = 0
mbDisplayDevice = New MemoryBlock(424) // = 4 + 32 + 128 + 4 + 128 + 128
mbDisplayDevice.UInt32Value(0) = 424
While EnumDisplayDevices(nil, DevNum, mbDisplayDevice, 0)
// when TRUE, look within mbDisplayDevice but always filled with zeros!
DevNum = DevNum + 1
Wend [/code]

@Andrew Lambert and @Jim Cramer,

You two are REALLY good with Declares! I am writing the book on Declares in Windows and each time I get stuck and do a search, one of your names always seems to be in the search results.

Thanks for all of your kind help in the Forums!!!

Andrews answer is the correct answer. I forgot about memoryblock.size.

Regards, jim

Andrew and Jim,

You are correct. Setting the size is what I was not doing. I did it following your response and it works.
Knowing the answer, the docs sentence cited by Andrew now looks so obvious…
(at some point I was looking for an API to initialize _DISPLAY_DEVICE, of course I did not find one. I was so close and so far!)

So thanks a lot for the answer!

Danny