c# - Read RTF of RichTextBox in Another Application -


i'm having difficulties in getting rtf of richtextbox application.

for now, lets work wordpad.

i can handle text area in wordpad fine. , can plain text text area using sendmessage , wm_gettext. good.

however, need rtf other application. in docs, see em_streamout should used editstream structure.

here code have far.

private const uint wm_user = 0x0400; private const uint em_streamout = wm_user + 74; private const uint sf_rtf = 2;  [dllimport("user32.dll", entrypoint="getforegroundwindow")] private static extern intptr getforegroundwindow();  [dllimport("user32.dll", entrypoint="getwindowthreadprocessid", setlasterror=true)] private static extern intptr getwindowthreadprocessid(intptr hwnd, out intptr lpdwprocessid);  [dllimport("user32.dll", entrypoint="getguithreadinfo", setlasterror=true)] private static extern bool getguithreadinfo(intptr hthreadid, ref guithreadinfo lpgui);  [dllimport("user32.dll", entrypoint = "sendmessage", charset = charset.auto)] private static extern intptr sendmessage(intptr hwnd, uint msg, uint wparam, ref editstream lparam);  private delegate int editstreamcallback(memorystream dwcookie, intptr pbbuff, int cb, out int pcb);  private static int editstreamproc(memorystream dwcookie, intptr pbbuff, int cb, out int pcb) {     pcb = cb;     byte[] buffer = new byte[cb];     marshal.copy(pbbuff, buffer, 0, cb);     dwcookie.write(buffer, 0, cb);     return 0; }  [structlayout(layoutkind.sequential)] private class editstream {     public memorystream dwcookie;     public uint dwerror;     public editstreamcallback pfncallback; }  private struct rect {     public int ileft;     public int itop;     public int iright;     public int ibottom; }  private struct guithreadinfo {     public int cbsize;     public int flags;     public intptr hwndactive;     public intptr hwndfocus;     public intptr hwndcapture;     public intptr hwndmenuowner;     public intptr hwndmovesize;     public intptr hwndcaret;     public rect rectcaret; }  public static string getrtffromactivewindowelement() {     try     {         intptr windowhwnd = getforegroundwindow();         intptr lpdwprocessid;         intptr threadid = getwindowthreadprocessid(windowhwnd, out lpdwprocessid);          guithreadinfo lpgui = new guithreadinfo();         lpgui.cbsize = marshal.sizeof(lpgui);          getguithreadinfo(threadid, ref lpgui);          string result = string.empty;         using (memorystream stream = new memorystream())         {             editstream editstream = new editstream();             editstream.pfncallback = new editstreamcallback(editstreamproc);             editstream.dwcookie = stream;              sendmessage(lpgui.hwndfocus, em_streamout, sf_rtf, ref editstream);              stream.seek(0, seekorigin.begin);             using (streamreader reader = new streamreader(stream))             {                 result = reader.readtoend();             }         }          return result;     }     catch (exception e)     {         console.write(e.message);         return null;     } } 

when call getrtffromactivewindowelement method, other application trying read (wordpad) crashes. getting option debug other program , saw memory access violation. however, cannot duplicate seeing error message more. in current state, other application locks , crashes no error message.

just note: wordpad easy application test with. i've done own simple winforms app has richtextbox in , same problem exists.

after solving issue, able write rtf other application.

suggestions?


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -