Menu

Reading content of Npp using WM_GETTEXT

2008-12-20
2012-11-14
  • Andre Koller

    Andre Koller - 2008-12-20

    Hey,
    I'm intented to write a simple tool for making some of my taksks easier :). Therefore I need to get the content of the file im actually editing in Npp. So far, everything works... I'm able to find the correct handle etc. but unfortunately, the Npp "textbox" doesnt behave like a normal windows editbox. If I try to read the text, I only get garbage in my destination string. If there's only 1 letter in the Npp window, it is captured correctly but if there are more, something goes wrong and I get "????...". Strangely Spy++ shows the correct string but in my external application it seems to be handled in a different way.

    I tried it with VisualBasic 6.0 and C (Codeblocks). The function itself works since its able to to read other text boxes

    Does Npp (I'm using the ANSI version) use any special format for the box or does anyone have an idea how to fix this?

    Thanks in advance! :).

    Best regards,
    André

     
    • Alexander Iljin

      Alexander Iljin - 2008-12-20
       
    • Andre Koller

      Andre Koller - 2008-12-20

      Hmm, thanks for your answer... found SCI_GETCHARAT there. It works, if I read the content byte-by-byte using that command but that CANT be the solution. Especially as there is no equivalent for adding text to the npp window.
      Any other ideas?

      Best regards,
      André

       
      • Alexander Iljin

        Alexander Iljin - 2008-12-21

        Get: SCI_GETTEXTRANGE
        Add: SCI_INSERTTEXT
        Del: SCI_REPLACETARGET

         
      • Alexander Iljin

        Alexander Iljin - 2008-12-21
         
    • DV

      DV - 2008-12-21

      Also please note that the text you get from Scintilla can be:
      1) ANSI (one byte per character)
      2) UTF-8 (from one to three bytes per character)
      3) DBSC (two bytes per character? - actually, I don't know much about these encodings)

       
      • Andre Koller

        Andre Koller - 2008-12-21

        I think thats one of my problems...

        At first, SCI_GETTEXT doesnt work for me. It doesnt do anything (leaves the initialized string unchanged).
        If I use WM_GETTEXT instead, I get for the string "ttttt" written in npp (while reading out 20 characters - so enough:

        3f 3f 74 0
        ?  ?  t

        The t is the only *true* character, everything else is simply garbage.

        hard to do anything wrong there:
            char p[80] = "a";
            SendMessageA(hwndx, WM_GETTEXT, 20, p);
            printf("\n%s\n\n", p);

        The problem seems to be, that WM_gettext tries to read unicode characters even if they are not there and throws away garbage (while I need it...). Im really wondering how Spy++ reads the data :(

        Thank you
        Best regards,
        André

         
        • DV

          DV - 2008-12-22

          ???
          Here is a part of oficial documentation of Scintilla:

          >>>>
          SCI_GETTEXT(int length, char *text)
          This returns length-1 characters of text from the start of the document plus one terminating 0 character. To collect all the text in a document, use SCI_GETLENGTH to get the number of characters in the document (nLen), allocate a character buffer of length nLen+1 bytes, then call SCI_GETTEXT(nLen+1, char *text). If the text argument is 0 then the length that should be allocated to store the entire document is returned. If you then save the text, you should use SCI_SETSAVEPOINT to mark the text as unmodified.
          <<<<

          So, to get some text, you must do something similar to the following:

          const int BUF_SIZE = 1024; // for example
          char szText[BUF_SIZE];
          SendMessage(hScintillaWnd, SCI_GETTEXT, BUF_SIZE, (LPARAM) szText);

          Now, the text inside the szText buffer can be ANSI, UTF-8 or DBSC depending on current Scintilla's editing window's encoding (code page). You can get this encoding (code page) from the same hScintillaWnd using the SCI_GETCODEPAGE message.

          I recommend you to download latest sources of Notepad++. You'll find a subfolder "scintilla" there, and this subfolder contains a subfolder "doc" with a file "ScintillaDoc.html". And I HIGHLY recommend you to read this "ScintillaDoc.html" because it contains all the information you need. Also, you may be interested in source files "UniConversion.cpp/h" (in the "PowerEditor\src" subfolder) which have functions for such text transformation as ANSI<->UTF8, UTF8<->UCS2 (UCS2 is Windows'es WideChar Unicode) and so on.

           
  • aka

    aka - 2009-09-19

    HI there,

    I am trying to achieve the same thing i.e
    **send current windows text to a richEdit20A or Edit control of notepad**

    So sending text using **SendMessage** is working but as with André I too am getting ???? 's or non English characters. I am working with notepad++ 5.4.5 UNICODE.

    I have looked into encoding and SCI_GETCODEPAGE but as I am new to this I haven't succeeded getting a result.

    If anyone knows how to capture english text in notepad++ and send it to RichEdit20A AS plain English please help !!!

    Appreciate your anticipated response,
    Thanks,
    ohme

     
  • DV

    DV - 2009-09-20

    As an example, you can download sources of the plugin NppExec ver. 0.3.2. In the file &quot;NppExec.cpp&quot;, there is a function &quot;CNppExec::selSaveTo&quot; which reads the selected text from Notepad++ and saves it into desired file with desired encoding (ANSI, UTF-8 or UCS-2 LE). The code which actually reads the text starts from the line &quot;HWND hSci = ScintillaHandle();&quot;. The following variable &quot;pSciText&quot; will contain the selected text from Scintilla, and the variable &quot;pOutText&quot; will contain the same text converted to desired encoding.   
    Also you may be interested in function &quot;CNppExec::selSetText&quot; which sets (or replaces) the selected text in Notepad++.

     
  • aka

    aka - 2009-09-20

    HI dv,

    Thanks for replying. I have downloaded the plugin source and will check  the function right away. But as I mentioned that I am able to send the complete text of current window to the richEdit20A control of my other application using sendMessage() &amp; WM_SETTEXT but I just receive **????'s** or **little boxes** with a correct last character.

    So when I open notepad++, by default, under &quot;**Format menu**&quot; &quot;**Encode in ANSI**&quot; is selected. So doest this mean whatever I type in the current window is in ANSI encoding? And what encoding does controls like Notepad's **Edit** or **RichEdit20A** want the text in??

    I am on Win XP, SP3 using notepad++ 5.4.5(Unicode).

    Thanks a lot for guiding,
    ohme

     
  • aka

    aka - 2009-09-21

    ok now when I used that function you told me about and as soon as I #include &quot;encodings/SysUniConv.h&quot;

    I strat getting this error
    **1&gt;C:\Program Files\Microsoft Visual Studio 9.0\VC\include\TCHAR.h(797) : error C3861: 'wcstok_s': identifier not found**

    any idea what must be the reason?
    Thanks,
    ohme