Hi,
I am having some issue with EnumDisplayDevices when called from C#.
When I use CharSet.Ansi (note - no unicode) for DISPLAY_DEVICE structure, I
get
0, DISPLAY1, raphics SuperSavage/IXC 1014, 1548305232,
VEN_5333&DEV_8C2E&SUBSYS_01FC1014&REV_05,
ISTRY\Machine\System\ControlSet001\Services\S3SSavage\Device0
1, DISPLAY3, eeting driver, 0, ,
ISTRY\Machine\System\ControlSet001\Services\mnmdd\Device0
When I try to use unicode (CharSet.Auto) I get garbage in device name, key,
string etc.
Platform SDK help says that display_device structure strings must be
declared as WCHAR, which means unicode. Or not?
And why some strings are losing starting symbols?
Here is declaration I use
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public class DISPLAY {
public long cb=0;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=32)]
public string DeviceName=new String(' ',32);
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=128)]
public string DeviceString=new String(' ',128);
public int StateFlags=0;
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=128)]
public string DeviceID=new String(' ',128);
[MarshalAs( UnmanagedType.ByValTStr, SizeConst=128)]
public string DeviceKey=new String(' ',128);
}
[DllImport("user32.dll")]
extern static bool EnumDisplayDevices(IntPtr lpDevice,
int iDevNum,
[In,Out] DISPLAY lpDisplayDevice,
int dwFlags);
and here is how function is called:
DISPLAY d=new DISPLAY();
d.cb=Marshal.SizeOf(d);
int id=0;
int dwf=0;
while (EnumDisplayDevices(IntPtr.Zero, id, d, dwf)) {
Console.WriteLine(
String.Format("{0}, {1}, {2}, {3}, {4}, {5}",
id,
d.DeviceName,
d.DeviceString,
d.StateFlags,
d.DeviceID,
d.DeviceKey
)
);
id++;
d.cb=Marshal.SizeOf(d);
}
What is wrong here?
Anybody can help?
Thanks
Alex
Nick Hall - 20 May 2004 14:06 GMT
Hi Alex,
Your DISPLAY class defines cb as long when according to the docs it should
be int.
Also, you can define the first parameter of EnumDisplayDevices as string and
pass null to it to achieve a similar effect. However, what you wrote would
still work.
Regards,
Nick Hall
> Hi,
>
[quoted text clipped - 100 lines]
>
> Alex
AlexS - 20 May 2004 14:31 GMT
Thanks, Nick
on W2K, where I test it, code produces results, which are
- inconsistent with Platform SDK docs - if I interpret WCHAR properly.
Unicode marshalling produces garbage
- some strings are truncated at the start when I use Ansi marshalling
So, it doesn't, at least as expected - can anybody see why?
Rgds
Alex
> Hi Alex,
>
[quoted text clipped - 115 lines]
> >
> > Alex