473,324 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Problems with GetMonitorInfoEx Structure

I'm having problems trying to call GetMonitorInfo. I believe the problem has
something to do with the szDeviceName. Passing a MonitorInfo struct works fine. I
added the szDeviceName and the struct is filled with garbage. What am I doing
wrong??

Thanks - JackRazz
Pretty version of this snippet is here: http://rafb.net/paste/results/M3114698.html

private const int CCHDEVICENAME = 32;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal class MonitorInfoEx {
public uint cbSize;
public RECT rcMonitor;
public RECT rcWork;
public UInt32 dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=CCHDEVICENAME)]
public string szDeviceName;
}
[DllImport("user32")]
internal static extern bool GetMonitorInfo( IntPtr hMonitor, [Out]
MonitorInfoEx lpmi );
MonitorInfoEx mi = new MonitorInfoEx();
mi.cbSize =(uint) Marshal.SizeOf(mi);

bool result = GetMonitorInfo( hMonitor, mi ) ;
if ( result ) {
Mon.MonitorArea= mi.rcMonitor;
Mon.WorkArea= mi.rcWork;
Mon.Flags = (int) mi.dwFlags;
Mon.hMonitor = hMonitor;
//Mon.deviceName=mi.szDeviceName;
}
else {
Console.WriteLine("Call to GetMonitorInfo failed");
}
Nov 15 '05 #1
5 6693
[DllImport("user32")]
internal static extern bool GetMonitorInfo( IntPtr hMonitor, [Out]
MonitorInfoEx lpmi );


Add CharSet=CharSet.Auto to the DllImport attribute.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #2
> Add CharSet=CharSet.Auto to the DllImport attribute.
Mattias,
First, thanks for the reply. It didn't work. I worked on it last night to capture
the LastError, which is 127 - 'The parameter is incorrect.' Any other suggestions.
I've updated the listing to show your suggested change with the full callback
procedure.

-------------------------------------------------------------------------------------
----
Pretty print version here --> http://rafb.net/paste/results/p3091664.html
//I'm having problems trying to call GetMonitorInfo. I believe the problem has
//something to do with szDeviceName. Passing a MonitorInfo struct works fine. I
//added the szDeviceName and the struct is filled with garbage.

[DllImport("user32", CharSet = CharSet.Auto, SetLastError=true)]
internal static extern bool GetMonitorInfo( IntPtr hMonitor, [Out] MonitorInfoEx
lpmi );

private const int CCHDEVICENAME = 32;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal class MonitorInfoEx {
public uint cbSize;
public RECT rcMonitor;
public RECT rcWork;
public UInt32 dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=CCHDEVICENAME)]
public string szDeviceName;
}
public bool MonitorEnumCallback( IntPtr hMonitor, IntPtr hDC,ref RECT lprcMonitor,
object dwData ) {

CMonitor Mon = new CMonitor();
MonitorInfoEx mi = new MonitorInfoEx();
mi.cbSize =(uint) Marshal.SizeOf(mi);

bool result = GetMonitorInfo( hMonitor, mi ) ;
if ( result ) {
Mon.MonitorArea= mi.rcMonitor;
Mon.WorkArea= mi.rcWork;
Mon.Flags = (int) mi.dwFlags;
Mon.hMonitor = hMonitor;
//Mon.deviceName=mi.szDeviceName;
}
else {
//throw new Win32Exception(Marshal.GetLastWin32Error());
Win32Exception myEx = new Win32Exception(Marshal.GetLastWin32Error());
Console.WriteLine(myEx.ErrorCode);
Console.WriteLine(myEx.Message);
}

mMonitors.Add(Mon);
return result;
}
Nov 15 '05 #3
How does RECT looks like?
It's always good to post complete reproducible code snippets.

Willy.

"JackRazz" <Ja******@NotValid.com> wrote in message
news:u2**************@tk2msftngp13.phx.gbl...
Add CharSet=CharSet.Auto to the DllImport attribute.

Mattias,
First, thanks for the reply. It didn't work. I worked on it last night

to capture the LastError, which is 127 - 'The parameter is incorrect.' Any other suggestions. I've updated the listing to show your suggested change with the full callback procedure.

Nov 15 '05 #4
GetMonitorInfo take a handle to a monitor as first argument not a DC handle.
So you nee to call MonitorFromWindow to get this handle.

[DllImport("User32")]
public static extern IntPtr MonitorFromWindow( IntPtr hWnd, int dwFlags );

const int MONITOR_DEFAULTTOPRIMARY = 1;
.....
result = GetMonitorInfoEx( MonitorFromWindow( IntPtr.Zero,
MONITOR_DEFAULTTOPRIMARY) , ref mi );
if ( result ) {
.....

Willy.
"JackRazz" <Ja******@NotValid.com> wrote in message
news:uo**************@TK2MSFTNGP12.phx.gbl...
How does RECT looks like? The RECT members are int's.
It's always good to post complete reproducible code snippets.


Agreed, thanks for offering to look at it. I reduced it to a simple

CMonitorInfo class that does everything when an instance is created in Form_Load. I have a break set in the callback, just load and run.

Just to reiterate, I'm having problems trying to call GetMonitorInfo. I believe the problem has something to do with szDeviceName. Passing a MonitorInfo struct works, MonitorInfoEx doesn't.

JackRazz
MS reference on MonitorInfoEx:
http://msdn.microsoft.com/library/de...nitor_81v6.asp
Latest Pretty Print version of the CMonitorInfo class included in the class http://rafb.net/paste/results/r3091643.html

Nov 15 '05 #5
Forgot the most important, change MonitorInfoEx and RECT into a struct i.s.o
a class....

Here is a working console sample.

using System;
using System.Runtime.InteropServices;

class Tester {
[StructLayout(LayoutKind.Sequential)]
public struct RECT {
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("User32")]
public static extern IntPtr MonitorFromWindow( IntPtr hWnd, int dwFlags );

[DllImport("user32",EntryPoint="GetMonitorInfo", CharSet = CharSet.Auto,
SetLastError=true)]
internal static extern bool GetMonitorInfoEx( IntPtr hMonitor, ref
MonitorInfoEx lpmi );

private const int CCHDEVICENAME = 32;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal struct MonitorInfoEx {
public int cbSize;
public RECT rcMonitor;
public RECT rcWork;
public UInt32 dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=CCHDEVICENAME)]
public string szDeviceName;
}

static void Main()
{
const int MONITOR_DEFAULTTOPRIMARY = 1;
bool result;
MonitorInfoEx mi = new MonitorInfoEx();
mi.cbSize = Marshal.SizeOf(mi);
result = GetMonitorInfoEx( MonitorFromWindow(
IntPtr.Zero,MONITOR_DEFAULTTOPRIMARY) , ref mi );
if ( result ) {
Console.WriteLine(mi.rcMonitor.left);
Console.WriteLine(mi.rcMonitor.right);
Console.WriteLine(mi.dwFlags.ToString());
Console.WriteLine(mi.szDeviceName);
}
else {
Console.WriteLine(Marshal.GetLastWin32Error());

}
}
}

Willy.

"JackRazz" <Ja******@NotValid.com> wrote in message
news:uo**************@TK2MSFTNGP12.phx.gbl...
How does RECT looks like? The RECT members are int's.
It's always good to post complete reproducible code snippets.


Agreed, thanks for offering to look at it. I reduced it to a simple

CMonitorInfo class that does everything when an instance is created in Form_Load. I have a break set in the callback, just load and run.

Just to reiterate, I'm having problems trying to call GetMonitorInfo. I believe the problem has something to do with szDeviceName. Passing a MonitorInfo struct works, MonitorInfoEx doesn't.

JackRazz
MS reference on MonitorInfoEx:
http://msdn.microsoft.com/library/de...nitor_81v6.asp
Latest Pretty Print version of the CMonitorInfo class included in the class http://rafb.net/paste/results/r3091643.html

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Steve Jorgensen | last post by:
Hi folks, I'm posting this message because it's an issue I come up against relatively often, but I can't find any writings on the subject, and I haven't been able to figure out even what key...
6
by: Jamal | last post by:
I am working on binary files of struct ACTIONS I have a recursive qsort/mergesort hybrid that 1) i'm not a 100% sure works correctly 2) would like to convert to iteration Any comments or...
2
by: BigC | last post by:
Hi, I am having problems converting from a Structure to a ByteArray. The main problem being that the structure contains a variable length array, which cannot be defined within the structure...
1
by: Falko Wagner | last post by:
Hi there, I am currently translating a VB 6.0 application to .NET and have the following problem: The data structure I need to pass to a DLL function call has a structure variable inside its...
2
by: Jack Fox | last post by:
We are encountering a couple of problems with our ASP.NET / IIS 6.0 applications: In each of 3 production environments we maintain a Windows Server 2003 machine running NTFS as a file server....
2
by: Mike | last post by:
Hi, I am new to C and having problems with the following program. Basically I am trying to read some files, loading data structures into memory for latter searching. I am trying to use structres...
0
by: MikeCS | last post by:
Hi all I would like some help with this issue. I am new to VB 2005 (OK with VB6) My problem is that I cannot seem to return a structure from a function. Example: I defined a structure in a...
8
webroten
by: webroten | last post by:
I've been working through trying to access a C DLL from VB.NET. I've read many online postings, but I'm still having problems. Now, my error is the "Attempted to read or write protected memory"...
3
by: =?ISO-8859-1?Q?Jo=E3o_Maia?= | last post by:
Hi there, I am having a weird problem in trying to use a ASP.NET menu inside a custom web part. I am developing a custom web part that has a menu inside (just the menu, nothing else). The menu...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.