interop - C# - How can I get a text range from WordPad by sending the EM_GETTEXTRANGE message? -
i'm having trouble getting text range running instance of wordpad. have gotten following windows messages work wordpad without problem: wm_gettext, wm_gettextlength, em_replacesel, em_getsel, , em_setsel. i'm not having luck em_gettextrange message though.
in c# test app have code runs @ startup looks running instance of wordpad, searches it's child windows window class name richedit50w. window send messages to. again, of messages have sent window work fine except em_gettextrange. after sending em_gettextrange, marshal.getlastwin32error returns 5, msdn says error_access_denied. below of interop code. can please me solve problem? thanks!
const uint wm_user = 0x0400; const uint em_gettextrange = wm_user + 75;
[structlayout(layoutkind.sequential)] struct charrange { public int min; public int max; } [structlayout(layoutkind.sequential, charset = charset.unicode)] struct textrange { public charrange charrange; [marshalas(unmanagedtype.lpwstr)] public string text; } [dllimport("user32", charset = charset.unicode, setlasterror = true)] extern static int sendmessage(intptr hwnd, uint msg, int wparam, ref textrange lparam); public static string gettextrange(intptr wnd, int min, int max) { textrange textrange = new textrange(); textrange.charrange.min = min; textrange.charrange.max = max; textrange.text = new string('\0', max - min); int length = sendmessage(wnd, em_gettextrange, 0, ref textrange); int error = marshal.getlastwin32error(); return error == 0 ? textrange.text : string.empty; }
i found answer own problem. when calling sendmessage targeting window in process, parameters have allocated in target process memory messages >= wm_user. needed can done pinvoking functions virtualallocex, virtualfreeex, readprocessmemory, , writeprocessmemory. brought in question @ how use em_gettextrange writeprocessmemory , readprocessmemory, didn't think applicable doing because didn't understand problem.
Comments
Post a Comment