471,319 Members | 1,411 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

PInvokeStackImbalance was detected.

The MDA appears on the call to OpenDesktop.

I have tried a variety of approaches (including specifying the A and W
variant with ExactSpelling=true, and PreserveSig=true) but none of them seem
to work. This code was copied from a 1.1 sample that purported to work
without an error.
public static class Desktop
{
private const long DESKTOP_SWITCHDESKTOP = 0x0100L;

[DllImport("user32.dll", CharSet=CharSet.Auto, PreserveSig=true)]
private static extern IntPtr OpenDesktop(string lpszDesktop, int
dwFlags, bool fInherit, long dwDesiredAccess);

[DllImport("user32.dll")]
private static extern bool SwitchDesktop(IntPtr hDesktop);

public static bool IsLocked()
{
IntPtr hdt = OpenDesktop("Default", 0, false,
DESKTOP_SWITCHDESKTOP);

if (IntPtr.Zero == hdt)
return false;

return SwitchDesktop(hdt);
}
}

Jan 26 '06 #1
4 10886
John,
[DllImport("user32.dll", CharSet=CharSet.Auto, PreserveSig=true)]
private static extern IntPtr OpenDesktop(string lpszDesktop, int
dwFlags, bool fInherit, long dwDesiredAccess);

^^^^

Should be a (u)int.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jan 26 '06 #2

"John Sudds" <John Su***@discussions.microsoft.com> wrote in message
news:98**********************************@microsof t.com...
| The MDA appears on the call to OpenDesktop.
|
| I have tried a variety of approaches (including specifying the A and W
| variant with ExactSpelling=true, and PreserveSig=true) but none of them
seem
| to work. This code was copied from a 1.1 sample that purported to work
| without an error.
|
|
| public static class Desktop
| {
| private const long DESKTOP_SWITCHDESKTOP = 0x0100L;
|
| [DllImport("user32.dll", CharSet=CharSet.Auto, PreserveSig=true)]
| private static extern IntPtr OpenDesktop(string lpszDesktop, int
| dwFlags, bool fInherit, long dwDesiredAccess);
|
| [DllImport("user32.dll")]
| private static extern bool SwitchDesktop(IntPtr hDesktop);
|
| public static bool IsLocked()
| {
| IntPtr hdt = OpenDesktop("Default", 0, false,
| DESKTOP_SWITCHDESKTOP);
|
| if (IntPtr.Zero == hdt)
| return false;
|
| return SwitchDesktop(hdt);
| }
| }
|

The last argument is not a long it should be an unsigned int.
The MDA did not exist in v1.1, so that means it's only reported now.

Willy.


Jan 26 '06 #3

THANKS to both of you! My problem was that ACCESS_MASK (the last param of
OpenDesktop) is unsigned.
"John Sudds" wrote:
The MDA appears on the call to OpenDesktop.

I have tried a variety of approaches (including specifying the A and W
variant with ExactSpelling=true, and PreserveSig=true) but none of them seem
to work. This code was copied from a 1.1 sample that purported to work
without an error.
public static class Desktop
{
private const long DESKTOP_SWITCHDESKTOP = 0x0100L;

[DllImport("user32.dll", CharSet=CharSet.Auto, PreserveSig=true)]
private static extern IntPtr OpenDesktop(string lpszDesktop, int
dwFlags, bool fInherit, long dwDesiredAccess);

[DllImport("user32.dll")]
private static extern bool SwitchDesktop(IntPtr hDesktop);

public static bool IsLocked()
{
IntPtr hdt = OpenDesktop("Default", 0, false,
DESKTOP_SWITCHDESKTOP);

if (IntPtr.Zero == hdt)
return false;

return SwitchDesktop(hdt);
}
}

Jan 26 '06 #4
For those who are interested in the final rewrite, here it is. This code is
running inside a screen saver, and accurately detects whether the Default
desktop has been locked (before, after, or during).

public static class Desktop
{
private const uint DESKTOP_READOBJECTS = 0x0001;
private const uint DESKTOP_WRITEOBJECTS = 0x0080;
private const uint DESKTOP_SWITCHDESKTOP = 0x0100;
private const uint AccessUnlocked = DESKTOP_READOBJECTS |
DESKTOP_WRITEOBJECTS | DESKTOP_SWITCHDESKTOP;

[DllImport("user32.dll", CharSet=CharSet.Auto, SetLastError=true)]
private static extern IntPtr OpenDesktop(string lpszDesktop, int
dwFlags,
bool fInherit, uint dwDesiredAccess);

[DllImport("user32.dll")]
private static extern bool SwitchDesktop(IntPtr hDesktop);

public static bool IsNotLocked()
{
IntPtr hdt = OpenDesktop("Default", 0, false, AccessUnlocked);

// If we can make the switch, the desktop is not locked
if (SwitchDesktop(hdt))
return true;

return false;
}
}

Jan 26 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Chandika | last post: by
32 posts views Thread by Clunixchit | last post: by
reply views Thread by MJKulangara | last post: by
2 posts views Thread by linuxfedora | last post: by

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.