Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Using ::SendMessage in C#

Using ::SendMessage in C#

Scheduled Pinned Locked Moved C#
csharpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Andreas Philipson
    wrote on last edited by
    #1

    I need to get the following code working in C#:

    char szText[255];
    ::SendMessage(hwndCurr, WM_GETTEXT, 255, (LPARAM)szText);
    m_strPassword = szText;

    And I've defined the SendMessage function

    const int WM_GETTEXT = 0x000D;

    [DllImport("user32.dll")]
    static extern int SendMessage(IntPtr window, int message, int wparam, [Out]long lparam);

    Does anybody know what I should do to make this work? Andreas Philipson

    H 1 Reply Last reply
    0
    • A Andreas Philipson

      I need to get the following code working in C#:

      char szText[255];
      ::SendMessage(hwndCurr, WM_GETTEXT, 255, (LPARAM)szText);
      m_strPassword = szText;

      And I've defined the SendMessage function

      const int WM_GETTEXT = 0x000D;

      [DllImport("user32.dll")]
      static extern int SendMessage(IntPtr window, int message, int wparam, [Out]long lparam);

      Does anybody know what I should do to make this work? Andreas Philipson

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      Define it like so:

      const int WM_GETTEXT = 0x000D;
      [DllImport("user32.dll", CharSet=CharSet.Ansi)]
      static extern int SendMessage(IntPtr hWnd, int msg, int wParam, out char[] lParam);

      You can get the HWND from controls' Handle property (and remember that Form derives from Control, so it has a handle too). Notice that I also used the out keyword (not the OutAttribute, which you could still use). This is necessary to get this to work in .NET. Call it like so:

      char[] text = new char[255];
      SendMessage(myTextBox.Handle, WM_GETTEXT, 255, out text);
      password = text;

      Now, if you're some cracker trying to get a password from another application, keep in mind that calls to WM_GETTEXT from another process for Edit controls (TextBox controls in .NET) with the ES_PASSWORD style set return an empty string (or NULL - I don't remember)...and get a life! :mad: If you're not a cracker, ignore the above statement except the bit about password fields in other processes! :)

      -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

      A 1 Reply Last reply
      0
      • H Heath Stewart

        Define it like so:

        const int WM_GETTEXT = 0x000D;
        [DllImport("user32.dll", CharSet=CharSet.Ansi)]
        static extern int SendMessage(IntPtr hWnd, int msg, int wParam, out char[] lParam);

        You can get the HWND from controls' Handle property (and remember that Form derives from Control, so it has a handle too). Notice that I also used the out keyword (not the OutAttribute, which you could still use). This is necessary to get this to work in .NET. Call it like so:

        char[] text = new char[255];
        SendMessage(myTextBox.Handle, WM_GETTEXT, 255, out text);
        password = text;

        Now, if you're some cracker trying to get a password from another application, keep in mind that calls to WM_GETTEXT from another process for Edit controls (TextBox controls in .NET) with the ES_PASSWORD style set return an empty string (or NULL - I don't remember)...and get a life! :mad: If you're not a cracker, ignore the above statement except the bit about password fields in other processes! :)

        -----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----

        A Offline
        A Offline
        Andreas Philipson
        wrote on last edited by
        #3

        Thanks for the help! I'll try it when I get home. And regarding my use of this code - I'm trying to create a Password Manager which allows Drag&Drop to any edit control. Andreas Philipson

        A 1 Reply Last reply
        0
        • A Andreas Philipson

          Thanks for the help! I'll try it when I get home. And regarding my use of this code - I'm trying to create a Password Manager which allows Drag&Drop to any edit control. Andreas Philipson

          A Offline
          A Offline
          Andreas Philipson
          wrote on last edited by
          #4

          After changing the solution to

          [DllImport("user32.dll", CharSet=CharSet.Ansi)]
          static extern int SendMessage(IntPtr hWnd, int msg, int wParam, [Out]StringBuilder lParam);

          It works perfectly! Thanks again! Andreas Philipson

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups