Form.FormBorderStyle Native Exception Access violation - c#

On a WindowsCE platform (custom build) our C# gui uses regular forms to show an "popup menu".
We set the FormBorderstyle to None as we don't want the form controls to be visible.
Some clients reported "Gray boxes" after a while.
After some testing here we could reproduce the problem quite fast. When we open 2 different menu's (forms) constantly the platform shows us an native exception.
Error
A native exception has occurred
in Tiger.CEHost.exe. Select Quit and
then restart this program, or select
Details for more information.
The details:
Error
ExceptionCode: 0xC0000005
ExceptionAdress: 0x00000001
Reading: 0x00000001
at WL.SetSTyle(IntPtr hwnThis, UInt32 dwMask, UInt32 dwStyle)
at Form._SetBorderStyle(AGL_WINDOWSTYLE wstyVal, AGL_WINDOWSTYLE wstyMask)
at Form.set_FormBorderStyle(FormBorderStyle value)
at pDropDown.PopupForm.Show()
at pDropDown.Show()
at pButton.ShowHideDropDown()
at pButton.OnClick(EventArgs e)
at Control.WnProc(WM wm, Int32 wParam, Int32 lParam)
at Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
at EVL.EnterMainLoop(IntPtr hwnMain)
at Application.Run(Form fm)
at Program.Main(String[] args)
It always seems to fail at the FormBorderStyle property. We've already tried to remove all the pInvokes as perhaps some memory was overwritten, but this didn't help.
We also log each call to the Show method and each call is made in the gui thread and the Form contains a valid handle.

I've never seen this, which tends to make me think that it's less likely to be a problem in the CF or even your app.
Does your device have enough memory to run the app? A low-memory condition should throw an OOM, but I've seen it do other, less predicatble things, so it's always the first thing to check.
If memory is not the issue, are you certain that it's not a platform problem? Remember, since a large portion of the OS is developed by the OEM, you can't rule out problems in the OS.
I'd try two things:
Does the same app run fine on some other hardware (even the emulator) without problems? If it works on other hardware, it heavily implicates the platform as the problem.
Since it's fairly easy to repro with a small app in C#, I'd recommend building an app in C/C++ that does the same functional items to see if it behaves or gives the same issue.

It seems to be a bug in netcfagl3_5.dll (will notify Microsoft about this)
When we set the FormBorderstyle using pinvokes (SetWindowLong) we can't reproduce the problem.
In case someone experiences this rare bug, this is the code to set the formborderstyle without using the .net FormBorderStyle property.
private const uint WS_OVERLAPPED = 0x00000000;
private const uint WS_POPUP = 0x80000000;
private const uint WS_CHILD = 0x40000000;
private const uint WS_MINIMIZE = 0x20000000;
private const uint WS_VISIBLE = 0x10000000;
private const uint WS_DISABLED = 0x08000000;
private const uint WS_CLIPSIBLINGS = 0x04000000;
private const uint WS_CLIPCHILDREN = 0x02000000;
private const uint WS_MAXIMIZE = 0x01000000;
private const uint WS_CAPTION = 0x00C00000;
private const uint WS_BORDER = 0x00800000;
private const uint WS_DLGFRAME = 0x00400000;
private const uint WS_VSCROLL = 0x00200000;
private const uint WS_HSCROLL = 0x00100000;
private const uint WS_SYSMENU = 0x00080000;
private const uint WS_THICKFRAME = 0x00040000;
private const uint WS_GROUP = 0x00020000;
private const uint WS_TABSTOP = 0x00010000;
private const int WS_MINIMIZEBOX = 0x00020000;
private const int WS_MAXIMIZEBOX = 0x00010000;
private const uint WS_EX_DLGMODALFRAME = 0x00000001;
private const uint WS_EX_NOPARENTNOTIFY = 0x00000004;
private const uint WS_EX_TOPMOST = 0x00000008;
private const uint WS_EX_ACCEPTFILES = 0x00000010;
private const uint WS_EX_TRANSPARENT = 0x00000020;
private const uint WS_EX_MDICHILD = 0x00000040;
private const uint WS_EX_TOOLWINDOW = 0x00000080;
private const uint WS_EX_WINDOWEDGE = 0x00000100;
private const uint WS_EX_CLIENTEDGE = 0x00000200;
private const uint WS_EX_CONTEXTHELP = 0x00000400;
private const uint WS_EX_STATICEDGE = 0x00020000;
private const int WS_EX_NOANIMATION = 0x04000000;
public const int GWL_EX_STYLE = -20;
public const int GWL_STYLE = (-16);
public static void SetNoBorder(Form form) {
RemoveFormStyle(form, GWL_STYLE, (int)(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZE | WS_MAXIMIZE | WS_SYSMENU));
RemoveFormStyle(form, GWL_EX_STYLE, (int)(WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE));
}
public static void RemoveFormStyle(Form f, int modifier, int style) {
int currStyle = GetWindowLong(f.Handle, GWL_EX_STYLE);
currStyle &= ~style;
SetWindowLong(f.Handle, modifier, currStyle);
}
[DllImport("Coredll.dll", SetLastError = true)]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("coredll.dll", SetLastError = true)]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);

Related

Form program made up of VisualBasic6 about C# Application TopMost not working

First of all, please understand that I am not good at English so I am not good at choosing or write sentence or word.
I am having an issue with C# TopMost not working.
calling method is at Called C# Application From VB6.
according to each specific situation show different from each other Form.
All forms have the following code:
// Define
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
private const UInt32 SWP_NOSIZE = 0x0001;
private const UInt32 SWP_NOMOVE = 0x0002;
private const UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE;
// All forms have the following code:
SetWindowPos(f.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
Commom.User32.AllowSetForegroundWindow((uint)Process.GetCurrentProcess().Id);
Commom.User32.SetForegroundWindow(h);
Commom.User32.ShowWindow(h, 1);
f.BringToFront();
f.TopLevel = true;
f.TopMost = true;
f.Focus();
// f is Form, h is Handle
Despite the above, it's TopMost not working.
I want you to help me if you know the problem or if you can help me.
Have a good day.

C# virtual mouse click in Flash apps

I have a problem with C# virtual mouse click.. you know that we should use "user32.dll" library and the mouse_event functions..
I use the following function :
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
private const int MOUSEEVENTF_LEFTDOWN = 0x0002;
private const int MOUSEEVENTF_LEFTUP = 0x0004;
private const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
private const int MOUSEEVENTF_MIDDLEUP = 0x0040;
private const int MOUSEEVENTF_MOVE = 0x0001;
private const int MOUSEEVENTF_XDOWN = 0x0080;
private const int MOUSEEVENTF_XUP = 0x0100;
private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
private const int MOUSEEVENTF_RIGHTUP = 0x0010;
and use this like that :
mouse_event(MOUSEEVENTF_LEFTDOWN, X, Y, 0, 0);
Guys, It is work on the all applications very good and Ok ! But in Flash Applications no
I have seen a post in here :
Problem automating mouse and interacting with Flash apps
They said that use from the all mouse_event functions , I used all of them but no one couldn't help me...
An other person who says use SendInput , You know better than me that the shapes that made by flash player doesn't have handle. and just main program has access to them. so we can't use SendInput...
I wanna do my problem with this code.. ( I don't like use difficult methods like Dynamic Data Exchange , etc... ) I wanna do it with my learnings... Can i ?
Thank you very much...
I found it...
You should Run your process as Administrator to do that...

How to remove 3d border (sunken) from MDIClient component in MDI parent form?

I am developing WinForms MDI app in VS2010 (.NET 4.0) and I just hate 3D border in MDI parent form.
So any ideas on how to remove it (make it flat or just no border it all) ?
I know this is an old post but I have spent some time and pain working the 3D border stuff out (because I needed it too) from fragments across the internet including:
Elements from Jacob Slusser's page at codeproject.com (Accessed 1st Aug'12)
So here goes:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace MDITest
{
public static class MDIClientSupport
{
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", ExactSpelling = true)]
private static extern int SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
private const int GWL_EXSTYLE = -20;
private const int WS_EX_CLIENTEDGE = 0x200;
private const uint SWP_NOSIZE = 0x0001;
private const uint SWP_NOMOVE = 0x0002;
private const uint SWP_NOZORDER = 0x0004;
private const uint SWP_NOREDRAW = 0x0008;
private const uint SWP_NOACTIVATE = 0x0010;
private const uint SWP_FRAMECHANGED = 0x0020;
private const uint SWP_SHOWWINDOW = 0x0040;
private const uint SWP_HIDEWINDOW = 0x0080;
private const uint SWP_NOCOPYBITS = 0x0100;
private const uint SWP_NOOWNERZORDER = 0x0200;
private const uint SWP_NOSENDCHANGING = 0x0400;
public static bool SetBevel(this Form form, bool show)
{
foreach (Control c in form.Controls)
{
MdiClient client = c as MdiClient;
if (client != null)
{
int windowLong = GetWindowLong(c.Handle, GWL_EXSTYLE);
if (show)
{
windowLong |= WS_EX_CLIENTEDGE;
}
else
{
windowLong &= ~WS_EX_CLIENTEDGE;
}
SetWindowLong(c.Handle, GWL_EXSTYLE, windowLong);
// Update the non-client area.
SetWindowPos(client.Handle, IntPtr.Zero, 0, 0, 0, 0,
SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
return true;
}
}
return false;
}
}
}
In the form load event call:
form.SetBevel(false);
Don't forget to change the namespace and remember this is an extension method but it could be changed to be just a method call in another class or in you MDI parent form.
If you would prefer not to import external libraries there is also following cheat which repositions/resizes the mdi container control.
protected override void OnLoad(EventArgs e)
{
var mdiclient = this.Controls.OfType<MdiClient>().Single();
this.SuspendLayout();
mdiclient.SuspendLayout();
var hdiff = mdiclient.Size.Width - mdiclient.ClientSize.Width;
var vdiff = mdiclient.Size.Height - mdiclient.ClientSize.Height;
var size = new Size(mdiclient.Width + hdiff, mdiclient.Height + vdiff);
var location = new Point(mdiclient.Left - (hdiff / 2), mdiclient.Top - (vdiff / 2));
mdiclient.Dock = DockStyle.None;
mdiclient.Size = size;
mdiclient.Location = location;
mdiclient.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
mdiclient.ResumeLayout(true);
this.ResumeLayout(true);
base.OnLoad(e);
}
Try changing the FormBorderStyle property to FixedSingle

How do I use INTERNET_HANDLE_TYPE_INTERNET to constrain the effects of END_BROWSER_SESSION to the current WebBrowser?

We have a WebBrowser that needs to have login information reset.
There is a solution in this question that suggests using INTERNET_HANDLE_TYPE_INTERNET with INTERNET_OPTION_END_BROWSER_SESSION. However, I'm not sure where INTERNET_HANDLE_TYPE_INTERNET should go.
private const int INTERNET_OPTION_END_BROWSER_SESSION = 42;
private const int INTERNET_HANDLE_TYPE_INTERNET = 1;
[DllImport("wininet.dll", SetLastError = true)]
private static extern bool InternetSetOption(IntPtr hInternet, int dwOption,
IntPtr lpBuffer, int lpdwBufferLength);
I call it right before I navigate:
InternetSetOption(IntPtr.Zero, INTERNET_OPTION_END_BROWSER_SESSION, IntPtr.Zero,
0);
browserCtrl.WebBrowser.Navigate(loginUrl);

how to change the window style of a form outside your app?

how to change the window style of a form outside your app?hard question?
i am actually trying to move a form witch is topmost and has no border.
i have the handle(hWnd) of the window.
i can write thousands of lines of code if guaranteed to work.
Assuming that this window could be from any app produced from any kind of Win32-based runtime, it looks like you'll have to resort to p/invoke of the core Win32 apis for window operations.
For example, you could use SetWindowPos, which can be imported from user32.dll. It's signature is this:
BOOL SetWindowPos(HWND hWnd,
HWND hWndInsertAfter,
int X,
int Y,
int cx,
int cy,
UINT uFlags
);
I'm not going to assume that you've done a p/invoke import before, so let's go from the top. Let's just bash out a windows forms app:
1) Create a windows forms app and then add these declarations to the Form1 class:
/* hWndInsertAfter constants. Lifted from WinUser.h,
* lines 4189 onwards depending on Platform SDK version */
public static IntPtr HWND_TOP = (IntPtr)0;
public static IntPtr HWND_BOTTOM = (IntPtr)1;
public static IntPtr HWND_TOPMOST = (IntPtr)(-1);
public static IntPtr HWND_NOTOPMOST = (IntPtr)(-2);
/* uFlags constants. Lifted again from WinUser.h,
* lines 4168 onwards depending on Platform SDK version */
/* these can be |'d together to combine behaviours */
public const int SWP_NOSIZE = 0x0001;
public const int SWP_NOMOVE = 0x0002;
public const int SWP_NOZORDER = 0x0004;
public const int SWP_NOREDRAW = 0x0008;
public const int SWP_NOACTIVATE = 0x0010;
public const int SWP_FRAMECHANGED = 0x0020;
public const int SWP_SHOWWINDOW = 0x0040;
public const int SWP_HIDEWINDOW = 0x0080;
public const int SWP_NOCOPYBITS = 0x0100;
public const int SWP_NOOWNERZORDER = 0x0200; /* Don't do owner Z ordering */
public const int SWP_NOSENDCHANGING = 0x0400; /* Don't send WM_WINDOWPOSCHANGING */
public const int SWP_DRAWFRAME = SWP_FRAMECHANGED;
public const int SWP_NOREPOSITION = SWP_NOOWNERZORDER;
public const int SWP_DEFERERASE = 0x2000;
public const int SWP_ASYNCWINDOWPOS = 0x4000;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SetWindowsPos(IntPtr hWnd,
IntPtr hWndInsertAfter,
int x,
int y,
int cx,
int cy,
UInt32 uFlags);
The annoying thing with p/invoke of Win32 windows methods is that you then have to start importing various numeric constants etc that Win32 uses - hence all the gumph beforehand.
Refer to the MSDN link for the SetWindowPos method for an explanation of what they do.
2) Add a button to the form called cmdMakeHidden and then write the handler as follows:
private void cmdMakeHidden_Click(object sender, EventArgs e)
{
//also causes the icon in the start bar to disappear
//SWP_HIDEWINDOW is the 'kill -9' of the windows world without actually killing!
SetWindowPos(this.Handle, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_HIDEWINDOW);
}
Replace the 'this.Handle' with the window handle of your choice to hide that window.
This method is actually used to apply multiple changes at once, hence the need to use some of the SWP_NO* options. For example, you should specify SWP_NOSIZE otherwise passing 0 for cx and cy will cause the window to shrink to zero width and height at the same time.
To demonstrate moving a window, add another button your form called cmdMove and then write the click handler as follows:
private void cmdMove_Click(object sender, EventArgs e)
{
SetWindowPos(this.Handle, HWND_TOP, 100, 100, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOREPOSITION);
}
This code moves your form to 100,100 whenever you hit the button.
Again, replace the this.Handle as you see fit. HWND_TOP here is completely optional, since reordering has been disabled with the SWP_NOZORDER and SWP_NOREPOSITION flags.
Hope this helps get you on the right track!

Categories