473,883 Members | 2,078 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems with GetMonitorInfoE x 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(La youtKind.Sequen tial, CharSet = CharSet.Auto)]
internal class MonitorInfoEx {
public uint cbSize;
public RECT rcMonitor;
public RECT rcWork;
public UInt32 dwFlags;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=CCHDE VICENAME)]
public string szDeviceName;
}
[DllImport("user 32")]
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.WriteLi ne("Call to GetMonitorInfo failed");
}
Nov 15 '05 #1
5 6732
[DllImport("user 32")]
internal static extern bool GetMonitorInfo( IntPtr hMonitor, [Out]
MonitorInfoE x 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("user 32", CharSet = CharSet.Auto, SetLastError=tr ue)]
internal static extern bool GetMonitorInfo( IntPtr hMonitor, [Out] MonitorInfoEx
lpmi );

private const int CCHDEVICENAME = 32;
[StructLayout(La youtKind.Sequen tial, CharSet = CharSet.Auto)]
internal class MonitorInfoEx {
public uint cbSize;
public RECT rcMonitor;
public RECT rcWork;
public UInt32 dwFlags;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=CCHDE VICENAME)]
public string szDeviceName;
}
public bool MonitorEnumCall back( 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.GetLast Win32Error());
Win32Exception myEx = new Win32Exception( Marshal.GetLast Win32Error());
Console.WriteLi ne(myEx.ErrorCo de);
Console.WriteLi ne(myEx.Message );
}

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

Willy.

"JackRazz" <Ja******@NotVa lid.com> wrote in message
news:u2******** ******@tk2msftn gp13.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 MonitorFromWind ow to get this handle.

[DllImport("User 32")]
public static extern IntPtr MonitorFromWind ow( IntPtr hWnd, int dwFlags );

const int MONITOR_DEFAULT TOPRIMARY = 1;
.....
result = GetMonitorInfoE x( MonitorFromWind ow( IntPtr.Zero,
MONITOR_DEFAULT TOPRIMARY) , ref mi );
if ( result ) {
.....

Willy.
"JackRazz" <Ja******@NotVa lid.com> wrote in message
news:uo******** ******@TK2MSFTN GP12.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(La youtKind.Sequen tial)]
public struct RECT {
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("User 32")]
public static extern IntPtr MonitorFromWind ow( IntPtr hWnd, int dwFlags );

[DllImport("user 32",EntryPoint= "GetMonitorInfo ", CharSet = CharSet.Auto,
SetLastError=tr ue)]
internal static extern bool GetMonitorInfoE x( IntPtr hMonitor, ref
MonitorInfoEx lpmi );

private const int CCHDEVICENAME = 32;
[StructLayout(La youtKind.Sequen tial, CharSet = CharSet.Auto)]
internal struct MonitorInfoEx {
public int cbSize;
public RECT rcMonitor;
public RECT rcWork;
public UInt32 dwFlags;
[MarshalAs(Unman agedType.ByValT Str, SizeConst=CCHDE VICENAME)]
public string szDeviceName;
}

static void Main()
{
const int MONITOR_DEFAULT TOPRIMARY = 1;
bool result;
MonitorInfoEx mi = new MonitorInfoEx() ;
mi.cbSize = Marshal.SizeOf( mi);
result = GetMonitorInfoE x( MonitorFromWind ow(
IntPtr.Zero,MON ITOR_DEFAULTTOP RIMARY) , ref mi );
if ( result ) {
Console.WriteLi ne(mi.rcMonitor .left);
Console.WriteLi ne(mi.rcMonitor .right);
Console.WriteLi ne(mi.dwFlags.T oString());
Console.WriteLi ne(mi.szDeviceN ame);
}
else {
Console.WriteLi ne(Marshal.GetL astWin32Error() );

}
}
}

Willy.

"JackRazz" <Ja******@NotVa lid.com> wrote in message
news:uo******** ******@TK2MSFTN GP12.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
2012
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 words one would use to look for it. First, in broad philosophical terms, code actually -is- data. Code is the data that's fed into a compiler, interpreter, or microprocessor that tells it what to do. Code execution is, then, just a form another...
6
2889
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 suggestion for improvements or conversion to iteration would be much appreciated
2
1073
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 definition. A example of my code is shown below and when this is run, an error is produced within the BuildByteArray function due to the structure size not being set. Any help would be greatly appreciated.
1
442
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 structure: Private Structure CstData_type Dim Cst_AZ As DbLong
2
1255
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. The file server typically has 10s of thousands of files on it. We've nver bothered to count, but I can imagine that some environments excede 100,000 files. Problem #1) We use the HttpContext Cache with a dependency on some given file on the file...
2
3283
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 and arrays of pointers to them. I have gotten the program to compile with gcc on WinXP. If the file i read doesnt have alot of records, it runs thru. But once i add more, it dies. In this program i have 4 files setup to read. The
0
1306
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 class (from which I instantiated an object). One of the object methods is supposed to return a data record (a structure).
8
4903
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" error. Here's my Structure declaration: <StructLayout(LayoutKind.Sequential, charset:=CharSet.Ansi)> _ Public Structure CONFIG_PARM <MarshalAs(UnmanagedType.LPStr)> Public address1 As String <MarshalAs(UnmanagedType.LPStr)> Public...
3
2220
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 is a System.Web.UI.WebControls.Menu (ASP.NET 2.0) object. If I add the menu to a simple ASPX web form, it works nice. However, I am adding it to my web part, by overriding the RenderWebPart method and doing something like:
0
9797
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10763
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7978
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7136
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6006
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4622
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4229
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.