473,326 Members | 2,099 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,326 software developers and data experts.

C#-APP: Problem sending EM_POSFROMCHAR message to RTB

Hello all!

I need to override the RichTextBox GetPositionFromCharIndex(int index) method and implement it by myself because of a performance issue in my app.

The EM_POSFROMCHAR message return the point in wParam:

EM_POSFROMCHAR
wParam = (LPPOINT) lpPoint; // address of structure receiving character position
lParam = (LPARAM) wCharIndex; // zero-based index of character

I have tried to define the POINT structure as a structure in C# but I'm getting an Attempt to read or write protected memory error at runtime. When I define the POINT structure as a class everything works fine. Since I make an very intensive use of this method in my app I think the structure implementation is the right choice and it will perform better from the garbage collection point of view.

Here are both codes, the POINT class solution and the POINT structure one. Can somebody help me to make the structure version to work?

Thanks in advance.

//Class version (working)
Expand|Select|Wrap|Line Numbers
  1. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  2. static extern IntPtr SendMessage(IntPtr hWnd, int msg, POINT wParam, IntPtr lParam);
  3.  
  4. private const int WM_USER = 0x400;
  5. private const int EM_POSFROMCHAR = WM_USER + 38;
  6.  
  7. [StructLayout(LayoutKind.Sequential)]
  8. internal class POINT
  9. {
  10.     public int x, y;
  11.  
  12.     public POINT()
  13.     {
  14.     }
  15.  
  16.     public POINT(int x, int y)
  17.     {
  18.         this.x = x;
  19.         this.y = y;
  20.     }
  21. }
  22.  
  23. public override Point GetPositionFromCharIndex(int index)
  24. {
  25.     POINT wParam = new POINT();
  26.     SendMessage(this.Handle, EM_POSFROMCHAR, wParam, (IntPtr)index);
  27.     return new Point(wParam.x, wParam.y);
  28. }
//Struct version (Not working: Attempt to read or write protected memory error)
Expand|Select|Wrap|Line Numbers
  1. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  2. static extern IntPtr SendMessage(IntPtr hWnd, int msg, ref POINT wParam, IntPtr lParam);
  3.  
  4. private const int WM_USER = 0x400;
  5. private const int EM_POSFROMCHAR = WM_USER + 38;
  6.  
  7. [StructLayout(LayoutKind.Sequential)]
  8. private struct POINT
  9. {
  10.     public int x, y;
  11.  
  12.     public POINT(int x, int y)
  13.     {
  14.         this.x = x;
  15.         this.y = y;
  16.     }
  17. }
  18.  
  19. public override Point GetPositionFromCharIndex(int index)
  20. {
  21.     POINT wParam = new POINT();
  22.     SendMessage(this.Handle, EM_POSFROMCHAR, ref wParam, (IntPtr)index);
  23.     return new Point(wParam.x, wParam.y);
  24. }
Dec 19 '07 #1
1 3261
Well, I finally found my problem. It was a mistake on the GetCharIndexFromPosition(Point pt) than I'm also overriding... I was sending a message passing the point by val instead of by ref.

Thanks anyway.
Dec 19 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Jacek | last post by:
Hello I have to send a string between two applications a sender (VB) and receiver (VC++) on local machine. Using SendMessage I can not send a string, using messagequeue I can send a string, but...
3
by: mark kurten | last post by:
I have a soap message that looks like this. s = s & " <soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'" s = s & " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'" s =...
1
by: DaleMan | last post by:
I'm looking for some help on how to dial a phone number in my application and send a text message to the portable machine that answers the call (cell phone, Palm, etc..). Anyone know of any sites...
0
by: Ken Foster | last post by:
In another message thread and also in a message thread on a completely different board, there have been references to BeginSend/EndSend not sending a full message all the time. I know EndReceive...
0
by: David Dvali | last post by:
Hello. I have a problem with sending Unicode text in mail message. So what I do: First of all I have some template file like this: ================================= <html> <head><title>Test...
1
by: Paul Wilson | last post by:
Does anyone out there know how to send a message over to another user (PC) in the network. I just need to be able to send a message (no need of receiving any messages). A very simple sample of...
1
by: Sangamesh | last post by:
Hello I hava a problem while sending a text message to mobile thru internet the characters which are after a # symbol are not sent to the respective number. I want to know the reason behind this and...
2
by: martin DH | last post by:
Hello all, I'm looking to send an email message from my database at the "click of a button." I've got that part worked out - the only thing is, it sends text only and I would like the message to...
0
by: abhishekForgotten | last post by:
Looking for some guidance on whether SMPP v3.4 will support greek messages or not ? If not how i can recive/send greek message via smpp v3.4, without decreasing length of msg (160) , I know if i...
0
by: phpuser123 | last post by:
I am trying to send a message from 1 phone to another and came up with the following codes .. Main class:: public void commandAction(Command command, Displayable displayable) { ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.