473,626 Members | 3,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help required in understanding an access violation exception

Lee
Hi all, been playing with some code overriding WndProc to get
information about mouse events.
So far I've tried to capture when the left mouse button is pressed
using the code below.

Sometimes the clicks will register fine, and other times it'll cause an
acess violation (Attempted to read or write protected memory. This is
often an indication that other memory is corrupt.)

So I'm wondering why this occurs and how I can prevent it from occuring

[code below]

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows. Forms;
using System.Collecti ons;

namespace Mirach
{
public partial class MirachManager : UserControl
{

private ArrayList objects = new ArrayList();

public MirachManager()
{
InitializeCompo nent();
}
public void AddControl(obje ct control)
{
objects.Add(con trol);
this.Invalidate ();
}

public void CheckMousePosit ion()
{
foreach (_DControl myControl in objects)
{
if ((Cursor.Positi on.X >= myControl.Top &&
Cursor.Position .X <= myControl.Top + myControl.Heigh t) &&
(Cursor.Positio n.Y >= myControl.Left && Cursor.Position .Y <=
myControl.Left + myControl.Width ))
{
Console.WriteLi ne("MouseOver") ;
break;
}
}
}

protected override void OnPaint(PaintEv entArgs e)
{
e.Graphics.Fill Rectangle(new SolidBrush(this .BackColor),
this.Top, this.Left, this.Width, this.Height);
foreach (_DControl myControl in objects)
{
myControl.Draw (e.Graphics);
}
}
private const int WM_LBUTTONDOWN = 0x201;
private const int WM_LBUTTONUP = 0x202;
struct Points
{
public short X;
public short Y;
}
protected override void WndProc(ref
System.Windows. Forms.Message m)
{
System.Windows. Forms.Message myMsg = m;
switch (myMsg.Msg)
{
case WM_LBUTTONDOWN:
Console.WriteLi ne("Left Mouse Button Down");
Points p = (Points)myMsg.G etLParam(typeof (Points));
Console.WriteLi ne(p.X);
break;
case WM_LBUTTONUP:
Console.WriteLi ne("Left Mouse Button Up");
break;
}
//Console.WriteLi ne(m);
base.WndProc (ref m);
}

private void GameField_Load( object sender, EventArgs e)
{

}
}
}

Nov 26 '06 #1
1 4195
Lee

Im sure someone else will be able to give you a more exact answer but
it will have something to do with the mapping of your struct onto the
L_Param in memory. Something isnt lining up properly which is causing
you access violation

As a fix you can try this code

Int32 result = (Int32)m.LParam ;
int xpos = (int)result >16;
int ypos = (int)result & 0xFFFF;

(Youll need to check if ive got my x and y the right way round :-)

On Nov 27, 10:26 am, "Lee" <lvtur...@gmail .comwrote:
Hi all, been playing with some code overriding WndProc to get
information about mouse events.
So far I've tried to capture when the left mouse button is pressed
using the code below.

Sometimes the clicks will register fine, and other times it'll cause an
acess violation (Attempted to read or write protected memory. This is
often an indication that other memory is corrupt.)

So I'm wondering why this occurs and how I can prevent it from occuring

[code below]

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows. Forms;
using System.Collecti ons;

namespace Mirach
{
public partial class MirachManager : UserControl
{

private ArrayList objects = new ArrayList();

public MirachManager()
{
InitializeCompo nent();
}
public void AddControl(obje ct control)
{
objects.Add(con trol);
this.Invalidate ();
}

public void CheckMousePosit ion()
{
foreach (_DControl myControl in objects)
{
if ((Cursor.Positi on.X >= myControl.Top &&
Cursor.Position .X <= myControl.Top + myControl.Heigh t) &&
(Cursor.Positio n.Y >= myControl.Left && Cursor.Position .Y <=
myControl.Left + myControl.Width ))
{
Console.WriteLi ne("MouseOver") ;
break;
}
}
}

protected override void OnPaint(PaintEv entArgs e)
{
e.Graphics.Fill Rectangle(new SolidBrush(this .BackColor),
this.Top, this.Left, this.Width, this.Height);
foreach (_DControl myControl in objects)
{
myControl.Draw (e.Graphics);
}
}
private const int WM_LBUTTONDOWN = 0x201;
private const int WM_LBUTTONUP = 0x202;
struct Points
{
public short X;
public short Y;
}
protected override void WndProc(ref
System.Windows. Forms.Message m)
{
System.Windows. Forms.Message myMsg = m;
switch (myMsg.Msg)
{
case WM_LBUTTONDOWN:
Console.WriteLi ne("Left Mouse Button Down");
Points p = (Points)myMsg.G etLParam(typeof (Points));
Console.WriteLi ne(p.X);
break;
case WM_LBUTTONUP:
Console.WriteLi ne("Left Mouse Button Up");
break;
}
//Console.WriteLi ne(m);
base.WndProc (ref m);
}

private void GameField_Load( object sender, EventArgs e)
{

}
}

}
Nov 27 '06 #2

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

Similar topics

15
18007
by: Steven Reddie | last post by:
I understand that access violations aren't part of the standard C++ exception handling support. On Windows, a particular MSVC compiler option enables Microsoft's Structured Exception Handling (SEH) in C++ EH so that a catch (...) will catch an access violation. I don't know if other platforms support something similar. I'm wondering about how to best protect an application or library from poorly written user-defined callbacks. It...
0
2717
by: Steven Reddie | last post by:
In article <slrnbnj19j.av.juergen@monocerus.manannan.org>, Juergen Heinzl wrote: >In article <f93791bd.0309282133.650da850@posting.google.com>, Steven Reddie wrote: >> I understand that access violations aren't part of the standard C++ >> exception handling support. On Windows, a particular MSVC compiler >> option enables Microsoft's Structured Exception Handling (SEH) in C++ >> EH so that a catch (...) will catch an access violation. ...
5
2347
by: Alex | last post by:
Hello Im working on project for my college, nevermind (but it urgent :((()... So I have this code example (in VS 6.0) in main class : REAL *Input; ....
1
1443
by: Timothy Madden | last post by:
Hello I have just read (in a MSDN article named Deep C++, in part 8) that member functions operator new and operator delete (and operator new and operator delete) are required in order to have the language automaticaly delete a memory bloc alocated with _new_ after the constructor for the new object throws. Is it true that only class delete is called in a new expresion to free memory after the constructor throw ?
0
1834
by: techie | last post by:
I have created an event sink in my ATL COM project. The event sink receives events from a C# component. There is no problem with receiving events but when my COM object is released I get an access violation - (MSCORWKS.DLL): 0xC0000005: Access Violation. At first I thought it could be due to my C++ code but after quite a lot of investigation I don't think there is nothing wrong with it. The access violation occurs after the destructor...
2
2854
by: Boris Fortes | last post by:
I need to unhook event receiver as result of native C++ event. It unhooks successfully, but __raise does not return and throws access violation. Visual Studio 2003 How to reproduce: Consol Win32 exe project
7
1710
by: Kai-Uwe Bux | last post by:
Please consider: struct X { int x; X ( int i = 0 ) : x ( i ) {}
1
13894
by: =?Utf-8?B?c2F0aGVlc2t1bWFy?= | last post by:
In my project i have a component named PTS Engine which is developed in VC++ ..Net 2003. And it is migrated to VC++.NET 2005 version. Initially this migration process has coded and tested in VC++ .NET 2005 Beta version. In beta version everything is working fine. When i tryied to run in .NET 2005 full version i am facing the following access violation Error. "Unhandled exception at 0x4bc0145c in Engine.exe: 0xc0000005: Access Violation...
39
4268
by: Martin | last post by:
I have an intranet-only site running in Windows XPPro, IIS 5.1, PHP 5.2.5. I have not used or changed this site for several months - the last time I worked with it, all was well. When I tried it just now, I am getting the subject error message (specifically: PHP has encountered an access violation at 00F76E21). The error is NOT occurring on every page request (but it is on most of them) and, when I get the error, simply pressing <F5to...
0
8269
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8203
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
8711
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...
1
8368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8512
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7203
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
6125
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
4206
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2630
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

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.