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

  • SEARCH
  • Home
  • 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 7697995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:08:24+00:00 2026-05-31T22:08:24+00:00

I’m trying to use the RegisteredWindowMessage API function to send text from one application

  • 0

I’m trying to use the RegisteredWindowMessage API function to send text from one application to another, and I have the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace Common
{
    public static class RegisteredMsg
    {
        private const string     MyMessage         = "9C7EDA65363F4fdaAF32";
        private static IntPtr    m_targetWindow    = new IntPtr(0xFFFF);
        private static object    m_object          = new object();
        private static HandleRef m_handleRef;
        private static HandleRef m_handleRef;
        public  static uint      RegisteredMessage 
        {
            get { return m_regMsg; }
            private set 
            { 
                m_regMsg = RegisterWindowMessage(SynchroMessage); 
            }
        }

        //-----------------------------------------------------------------
        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
        static extern bool PostMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        //-----------------------------------------------------------------
        [DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
        static extern uint RegisterWindowMessage(string lpString);

        //-----------------------------------------------------------------
        static RegisteredMsg()
        {
            m_handleRef = new HandleRef(m_object, m_targetWindow);
        }

        //-----------------------------------------------------------------
        public static void PostUpdateMsg(string text)
        {
            IntPtr    lpData    = Marshal.StringToHGlobalAuto(text);
            IntPtr    lpLength  = new IntPtr(text.Length);
            if (!PostMessage(m_handleRef, RegisteredMessage, lpData, lpLength))
            {
                throw new Exception("Could not post message.");
            }
        }

        //-----------------------------------------------------------------
        public static string GetMessageText(Message msg)
        {
            string text = "";
            int length = msg.LParam.ToInt32();
            text       = Marshal.PtrToStringAuto(msg.WParam, length);
            Marshal.FreeHGlobal(msg.WParam);
            return text;
        }
    }
}

Posting the message works find, but when the receiving application calls GetMessageText, the string contains “\0\0\0\0” (which is NOT what the sending application sent).

I’m calling it like this:

RegisteredMsg.PostUpdateMsg("test");

and receiving it like this:

protected override void WndProc(ref Message msg)
{
    base.WndProc(ref msg);
    if (Convert.ToUInt32(msg.Msg) == RegisteredMsg.RegisteredMessage)
    {
        string text = RegisteredMsg.GetMessageText(msg);
    }
}

EDIT #0

I also tried it this way, and all of the bytes in the received array are ‘\0’:

//-------------------------------------------------------------------------
public static void PostUpdateMsg(string text)
{
    byte[] array  = StringToByteArray(text);
    IntPtr lpData = Marshal.AllocHGlobal(array.Length);
    Marshal.Copy(array, 0, lpData, array.Length);
    IntPtr lpLength = new IntPtr(text.Length);
    if (!PostMessage(m_handleRef, RegisteredMessage, lpData, lpLength))
    {
        throw new Exception("Could not post message.");
    }
}

//-------------------------------------------------------------------------public static string GetMessageText(Message msg)
{
    string text   = "";
    int    length = msg.LParam.ToInt32();
    byte[] array  = new byte[length];
    Marshal.Copy(msg.WParam, array, 0, length);
    text          = RegisteredMsg.ByteArrayToString(array);
    return text;
}

EDIT #1

I also called this method from PostUpdateMessage, just to make sure what I was sending was what I thought I was sending:

private static void TestIntPtr(IntPtr ptr, int length)
{
    string text = "";
    byte[] array = new byte[length];
    Marshal.Copy(ptr, array, 0, length);
    text       = ByteArrayToString(array);  // <<------------
}

When the indicated line is executed, the text variablle is indeed = “test”, so I’m doing it right on the sending side. It looks like the memory is getting cleared before it gets to the receiving application.

EDIT #2

I also tried making the IntPtr (pointing to the string I want to send) global to its parent class to make sure it would live long enough to be viable at the other end. No joy there, either.

EDIT #3

I also reverted back to using the StringToHGlobalAuto, and ran the “is it still okay in the sending app” test (see Edit #1 above), and that test proved that the way I was building the IntPtr was fine as well.

  • 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-31T22:08:25+00:00Added an answer on May 31, 2026 at 10:08 pm

    This cannot work by design. A pointer is only valid in the process that created it. Every process gets its own chunk of virtual memory. Retrieving the pointed-to memory content requires ReadProcessMemory(). Or you can allocate memory in the target process with VirtualAllocEx() and write to it with WriteProcessMemory(). Windows supports the WM_COPYDATA message to take care of this for you.

    This is all rather low-level and painful. There are much better IPC mechanisms available. The ones that work well in .NET are sockets, pipes, WCF.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to understand how to use SyndicationItem to display feed which is
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a reasonable size flat file database of text documents mostly saved 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.