473,387 Members | 1,497 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,387 software developers and data experts.

stack overflow using NativeWindow

Hi,

I am trying to dynamically install/deinstall a message handler to a
System.Windows.Forms.Form using NativeWindow.

I do not use IMessageFilter derived class because it intercept only the
PostMessage messages.

My problem is that when I install the message handler the second time a
"Fatal stack overflow error" occurs.

To reproduce my problem you have only to start the program at the end of
this post, click the left mouse button on the form and wait for a while.

My test program install the NativeWindow message handler on start.
Clicking the left mouse button the program deinstall and reinstall the
message handler.

Many thanks for any help.

Marco.

////////////////////
//Code start

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace TestNativeWindows
{
/// <summary>
/// Summary description for MyForm.
/// </summary>
public class MyForm
: System.Windows.Forms.Form
{
private MyNativeWindow m_MessageHandler;

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public MyForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
m_MessageHandler = new MyNativeWindow(this);
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
m_MessageHandler.ReleaseHandle();
m_MessageHandler = null;
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// MyForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
this.ClientSize = new System.Drawing.Size(480, 88);
this.Name = "MyForm";
this.Text = "MyForm";
this.MouseDown += new
System.Windows.Forms.MouseEventHandler(this.MyForm _MouseDown);
this.ResumeLayout(false);
}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MyForm());
}

private void MyForm_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
m_MessageHandler.Acquire(this);
else if (e.Button == System.Windows.Forms.MouseButtons.Middle)
System.Diagnostics.Debug.WriteLine("WM_MBUTTONDOWN handled
by the FORM message handler");
else if (e.Button == System.Windows.Forms.MouseButtons.Right)
m_MessageHandler.ReleaseHandle();
}
}; // MyForm

public class MyNativeWindow
: NativeWindow
{
private const int WM_MBUTTONDOWN = 0x0207;

private System.Windows.Forms.Form m_parent = null;

public void Acquire(System.Windows.Forms.Form parent)
{
this.ReleaseHandle();

m_parent = parent;
AssignHandle(m_parent.Handle);
System.Diagnostics.Debug.WriteLine("Acquire");
}

public override void ReleaseHandle()
{
// Window was destroyed, release hook.
if (m_parent != null)
{
base.ReleaseHandle();
m_parent = null;
System.Diagnostics.Debug.WriteLine("Release");
}
}

public MyNativeWindow(System.Windows.Forms.Form parent)
{
System.Diagnostics.Debug.WriteLine("MyNativeWindow ");
Acquire(parent);
}

~MyNativeWindow()
{
System.Diagnostics.Debug.WriteLine("~MyNativeWindo w");
this.ReleaseHandle();
}

protected override void OnHandleChange()
{
System.Diagnostics.Debug.WriteLine("OnHandleChange (" +
Handle.ToString() + ") ");
}
[System.Security.Permissions.PermissionSet(System.S ecurity.Permissions.SecurityAction.Demand,
Name="FullTrust")]
protected override void WndProc(ref Message m)
{
// Listen for operating system messages

switch (m.Msg)
{
case WM_MBUTTONDOWN:
System.Diagnostics.Debug.WriteLine("WM_MBUTTONDOWN
handled by NATIVE_WINDOW message handler");
return;
}
base.WndProc(ref m);
}
}; //class MyNativeWindow

}

//Code end
////////////////////

--
For direct reply change underscore to dot

Nov 16 '05 #1
0 2145

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

Similar topics

7
by: Aguilar, James | last post by:
Hello all, To begin, yes, this -is- a homework assignment. However, it is for my Algorithms class, and my instructor has given us explicit permission to use "expert" groups like newsgroups, so...
19
by: Jim | last post by:
I have spent the past few weeks designing a database for my company. The problem is I have started running into what I believe are stack overflow problems. There are two tab controls on the form...
4
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. ...
2
by: David W. Walker | last post by:
I am attempting to port a C code that runs OK on a number of Linux and Unix machines to Windows XP using Visual Studio C++. I have set the program up as a console application, but when I try to run...
2
by: Ali | last post by:
Hi, I got stack overflow errors while using an unmanaged dll from managed c# application. When i use editbin.exe to increase stack size of app , it works. Is there a way to increase stack size of...
7
by: amit.atray | last post by:
Environement : Sun OS + gnu tools + sun studio (dbx etc) having some Old C-Code (ansi + KR Style) and code inspection shows some big size variable (auto) allocated (on stack) say for ex. char...
12
by: Geoff Cox | last post by:
Hello, Some one is getting the "stack overflow" error message when using a Javascript program which I wrote when using Internet Explorer but not when using Mozilla. Any ideas as to how I...
5
by: moondaddy | last post by:
I'm trying to serialize some xaml elements using the XamlWriter and I'm getting a stack overflow error because the elements have some public properties which get called about a million ( a lot)...
0
by: bradman1861 | last post by:
hello i am getting a overflow error when I try to run this: here is the error: System.Data.OleDb.OleDbException was unhandled Message="Overflow" Source="Microsoft JET Database Engine" ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...

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.