Using ::SendMessage in C#
-
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
-
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
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 thatForm
derives fromControl
, so it has a handle too). Notice that I also used theout
keyword (not theOutAttribute
, 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 theES_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-----
-
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 thatForm
derives fromControl
, so it has a handle too). Notice that I also used theout
keyword (not theOutAttribute
, 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 theES_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-----
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
-
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
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