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 7966087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:19:39+00:00 2026-06-04T06:19:39+00:00

I have an example of winapi code: struct CommunicationInfo { long internalMsg; const TCHAR

  • 0

I have an example of winapi code:

struct CommunicationInfo {
    long internalMsg;
    const TCHAR * srcModuleName;
    void * info; 
  };

…

const TCHAR* szText = _T("Hello from my plugin!\n(test message)");
    CommunicationInfo ci = { 0x0401, cszMyPlugin, (void *) szText };
    ::SendMessage( hNppWnd, 0x111, (WPARAM) _T("NppExec.dll"), (LPARAM) &ci );

I want make the same call from .net and i wrote such wrapper:

[StructLayout(LayoutKind.Sequential)]
    public struct CommunicationInfo
    {
        public Int64 internalMsg;
        [MarshalAs(UnmanagedType.LPWStr)]
        public StringBuilder srcModuleName;
        [MarshalAs(UnmanagedType.LPWStr)]
        public StringBuilder data;
    };

…

[DllImport("user32")]
public static extern IntPtr SendMessage(IntPtr hWnd, 
    NppMsg Msg, IntPtr wParam, 
    [MarshalAs(UnmanagedType.Struct)] CommunicationInfo communicationInfo);

…

SendMessage(hNppWnd, 0x111, 
    Marshal.StringToHGlobalUni("NppExec.dll"), 
    new CommunicationInfo 
    { 
        data = new StringBuilder("test test"),
        internalMsg = 0x0401,
        srcModuleName = new StringBuilder("ModuleName")
    });

But this code doesn’t work. Where did I make a mistake ?

  • 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-06-04T06:19:40+00:00Added an answer on June 4, 2026 at 6:19 am

    As Viktor points out, C/C++ long is 32 bits in size so needs to be matched with C# int. On top of that, the passing of the struct is not handled correctly. In addition the call to StringToHGlobalUni leaks since you never call FreeHGlobal.

    I’d probably handle the marshalling something like this:

    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
    public struct CommunicationInfo
    {
        public int internalMsg;
        public string srcModuleName;
        public string data;
    };
    ....
    [DllImport("user32")]
    public static extern IntPtr SendMessage(
        IntPtr hWnd, 
        uint Msg, 
        [MarshalAs(UnmanagedType.LPWStr)] string wParam, 
        ref CommunicationInfo communicationInfo
    );
    ....
    CommunicationInfo communicationInfo = new CommunicationInfo 
    { 
        internalMsg = 0x0401,
        srcModuleName = "ModuleName",
        data = "test test"
    };
    SendMessage(hNppWnd, 0x111, "NppExec.dll", ref communicationInfo);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Anyone have example Rx code that shows how to execute an action due to
I have an example in C# code, but it is using streamWriter . It
I have an example of some code that I see often in websites that
What if I have a .NET library which has WinApi calls (ADO.NET, for example),
does any of you have example code (or a link to it) of how
I have example of code below. <script type=text/javascript src=assets/scripts/somescript.php>. </script> So, will my browser
I have the following code: f=open('data.txt', 'w') conn = urllib2.urlopen('http://example.com') page_html = conn.read() data=BeautifulSoup(page_html)
I have example.org and foo.example.org pointing to the same directory, /var/www/html/ , and want
I have example: for line in IN.readlines(): line = line.rstrip('\n') mas = line.split('\t') row
If I have example.com/dir and dir is basically a folder in the example.com server,

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.