Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / May 2004

Tip: Looking for answers? Try searching our database.

EnumDisplayDevices

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
AlexS - 20 May 2004 05:14 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.