Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6926073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:52:15+00:00 2026-05-27T10:52:15+00:00

I get an intermittent error when calling Message.GetLParam, getting messages sent between processes. I

  • 0

I get an intermittent error when calling Message.GetLParam, getting messages sent between processes.
I have two processes, both written in C# (.Net 3.5). I am using the Win32 function SendMessage() to send data from one process (the source) to the other (the target). The target process’s main window (it’s a Windows Forms app) overrides the WndProc() function to receive messages.
The source process locates the other by using the Process.GetProcessesByName() function, then using the Process.MainWindowHandle to get the window handle that I want to send the message to. The code of the source looks like this:

Process[] procs = Process.GetProcessesByName("MyTargetProcess");
if (procs != null 
    && procs.Length > 0)
{
    IntPtr win = procs[0].MainWindowHandle;
    var someData = new Win32.COPYDATASTRUCT   // This is a struct that I defined
    {
    // Initialize fields of the struct
    };
    Win32.SendMessage(win,
        Win32.MyMsgCode,    // my custom message
        IntPtr.Zero,    // wParam = 0
        ref someData);
}

The target process code looks like this:

protected override void WndProc(ref System.Windows.Forms.Message m)
{
    if (m.Msg == Win32.MyMsgCode)
    {
    Win32.COPYDATASTRUCT ds;
        try
        {
            ds = (Win32.COPYDATASTRUCT)m.GetLParam(typeof(Win32.COPYDATASTRUCT));
        }
        catch (Exception ex)
        {
            log.ErrorFormat("Got exception in WndProc", ex);
        }
        // Do something with the message
        ....
}

Win32 is a static class I defined that gets all my P/Invoke definitions.
I do not understand why I am catching an AccessViolationException in WndProc.
Does anybody have an idea why? and why it only happens some of the time?

Thanks for your attention!

——————————– EDIT ——————————————
Another thing that baffles me: the COPYDATASTRUCT is declared as

public static readonly int WM_COPYDATA = 0x004a;
[StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT
{
    // Specifies data to be passed to the receiving application.
    public string dwData;
    // Specifies the size, in bytes, of the data pointed to by the lpData member. 
    public int cbData;
    // Pointer to data to be passed to the receiving application. This member can be NULL.                 
    public string lpData;
}

It is initialized like this:

string payload = " some data ";
var someData = new Win32.COPYDATASTRUCT   // This is a struct that I defined
{
    dwData = "bogusData",
    cbData = sizeof(char) * payload.Length,
    lpData = payload
};

And in target code, I always receive dwData = null.

———————– 2nd edit ————————————–
I just tried with adding the zero terminator, and I still get the error.
If I change the marshalling code to do my own marshalling as in

IntPtr pcds = Marshal.AllocHGlobal(Marshal.SizeOf(someData));
Marshal.StructureToPtr(someData, pcds, true);
Win32.SendMessage(win, (uint)Win32.WM_COPYDATA, IntPtr.Zero, pcds);

Then the error happens ALL THE TIME! However, if I repeat the GetLParam() call in the catch block, it succeeds almost all the time, on the 2nd try.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:52:16+00:00Added an answer on May 27, 2026 at 10:52 am

    The thing that jumps out at me is that cbData is set incorrectly. You need to account for the zero-terminator at the end of the string.

     cbData = sizeof(char) * (payload.Length+1)
    

    That would certainly explain the error. When you send the message, the WM_COPYDATA marshalling would not copy the zero terminator and so the recipient would then read beyond the end of the buffer into uninitialized values.

    I also wonder about sizeof(char). Are you calling the Unicode version of SendMessage? If not then I’d expect to see access violations in the sending code.

    You should also be wary that the recipient application opens itself to buffer overruns. Because it ignores the value of cbData and treats the lpData field as a null-terminated pointer it may be possible for an attacker to force your app to execute arbitrary code. To defend against this, you should copy the data, cbData bytes of it, into a byte array and then convert to a string.

    The other issue is that you need to declare dwData as UIntPtr. The way you have defined the struct, the recipient code treats dwData as a pointer to a string which will provoke AVs because you have crossed process boundaries.

    I’d also point out that cbData is unsigned and should be uint but that is a benign error here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get an odd intermittent error when trying to write a Diagnostics.Trace message from
I get an Access is Denied error message when I use the strong name
I have a server that's returning an unexpected, intermittent error and wondering if anyone
We get the following error; The request was aborted: Could not create SSL/TLS secure
I get the following error when running my Visual Studio 2008 ASP.NET project (start
I get the following error when building my Windows Forms solution: LC.exe exited with
I get this error: Can't locate Foo.pm in @INC Is there an easier way
I get the following error when trying to run the latest Cygwin version of
When running a stored procedure, we're getting the error 297 The user does not
A site I am working on displays an intermittent runtime error but only in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.