EnumDisplayDevices... What I'm doing wrong?
BlitzMax Forums/BlitzMax Programming/EnumDisplayDevices... What I'm doing wrong?
| ||
Title is self explanatory... what I'm doing wrong in the code below?Extern "Win32" Function EnumDisplayDevices:Int (Unused:Int, iDevNum:Int, display:Byte Ptr, flags:Int)"Win32"= "EnumDisplayDevicesA@16" EndExtern Type DISPLAY_DEVICE Field cb:Int Field DeviceName:Byte[32] 'Byte Ptr[32] Field DeviceString:Byte[128] 'Byte Ptr[128] Field StateFlags: Int Field DeviceID:Byte[128] 'Byte Ptr[128] Field DeviceKey:Byte[128] 'Byte Ptr[128] End Type Local dd:DISPLAY_DEVICE = New DISPLAY_DEVICE dd.cb = SizeOf(dd.cb) + SizeOf(dd.DeviceName) + SizeOf(dd.DeviceString) + SizeOf(dd.StateFlags) + SizeOf(dd.DeviceID) + SizeOf(dd.DeviceKey) Print dd.cb Print "--------------------------" Print "Retrieving devices info..." Print "--------------------------" Local i:Int=0 Local done:Int=False While Not done If EnumDisplayDevices(0, i, Varptr(dd), 0) Then Print "Display " + i + " info:" Print "Device ID:" + String.FromCString(dd.DeviceID) Else Print "Finished looking for display adapters" done=True End If i:+1 Wend Print "done!" I ran this code, and the program just ends, no error, no anything! Not even the "done!" is printed! I have no idea what is wrong with it :( Last edited 2011 Last edited 2011 Last edited 2011 |
| ||
Well, to begin with, a TCHAR[32] is not the same as a Byte Ptr[32]... You'll probably find it easier to either write some C glue, or a TBank... |
| ||
Edit: Updated the code, but it still doesn't work... ...but the same code on VB works. I really have no clue how to fix this :/ Also I don't get it why sizeof(type) returns a complete bogus value - I had to get sizeOf each type element to get it's size correctly. Ahhh... using Banks was the (ugly) solution: Last edited 2011 |
| ||
Also I don't get it why sizeof(type) returns a complete bogus value It returns the correct value. There are six fields, each of which is four bytes ( integer or pointer ). In C you just get a block of memory with names like DevideID giving offsets into the block. That's why Brucey suggested using C, or a bank and handle the offsets yourself. Last edited 2011 |
| ||
Byte is 1 byte and Byte Ptr is 4 bytes. |
| ||
VB <> BlitzMax Arrays in BlitzMax are not like arrays in C/C++. And Types too are not like structs... sizeof 4+32+128+4+128+128 = .... ? Despite what Floyd says, the six fields are not all 4 bytes, since the TCHAR fields are inlined character arrays of the struct, not pointers. If you don't want to use a TBank, do a straight memory allocation, but you'll have to remember to free it yourself... Local dd:Byte Ptr = MemAlloc(4+32+128+4+128+128) Int Ptr(dd)[0] = 4+32+128+4+128+128 .. call your function, passing in dd, not Var Ptr dd.... EnumDisplayDevices(0, i, dd, 0) .. then.. Print "Device Name:" + String.FromCString(dd + 4) etc... Try not to over-complicate things :-) |
| ||
Ah! That look so much better! Didn't know bmax had Malloc! :) |
| ||
Well, TBank hides all that away, and manages the deallocation of the memory for you. Otherwise you need to remember to MemFrree() your Byte Ptr.... |