• Login
Community
  • Login

C# Plugin - Characters missing in message box

Scheduled Pinned Locked Moved Notepad++ & Plugin Development
7 Posts 4 Posters 596 Views
Loading More Posts
  • 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.
  • S
    superfestung
    last edited by Dec 14, 2024, 2:51 PM

    Hello Plugin Community,

    im starting fresh with the C# Plugin from: https://github.com/molsonkiko/NppCSharpPluginPack

    I want to read the marked characters in the editor, here “Notepad”, and use it later for searching text in a XML file. For testing i used a message box for displaying the string, but every second character is missing. Before that i didnt use the UTF8 conversion and directly passed the result of Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_GETSELTEXT, 0, strSearchVariable); to the message box, which than showed some chinese looking like characters.

    Any ideas what i am missing?
    And yes, i need to learn more C# programming ;-)

    b50d8e16-b113-4c1f-be82-82df4a490d59-image.png
    My C# Code:

     static void HelpMe()
     {
        
         int start = 0, end = 0;
         int bufferSize = 0;
    
         IntPtr intSelectionStart = Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_GETSELECTIONSTART, 0, 0);
         IntPtr intSelectionEnd = Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_GETSELECTIONEND, 0, 0);
       
         start = intSelectionStart.ToInt32();
         end = intSelectionEnd.ToInt32();
    
         bufferSize = start < end ? end - start : start - end;
    
         if (bufferSize > 0)
         {
             var strSearchVariable = new StringBuilder(bufferSize + 1);
    
             Win32.SendMessage(PluginBase.GetCurrentScintilla(), SciMsg.SCI_GETSELTEXT, 0, strSearchVariable);
    
             byte[] utf8_Bytes = new byte[strSearchVariable.Length];
             
             for (int i = 0; i < strSearchVariable.Length; ++i)
             {
                 utf8_Bytes[i] = (byte)strSearchVariable[i];                    
             }
            
             string strTest = Encoding.UTF8.GetString(utf8_Bytes, 0, utf8_Bytes.Length);
    
             MessageBox.Show(strTest, "Search Variable Display", MessageBoxButtons.OK, MessageBoxIcon.Information);
             
         }
         else
         {
             MessageBox.Show("No Characters selected!", "Search Variable Display", MessageBoxButtons.OK, MessageBoxIcon.Warning);
         }
     }
    
    C 1 Reply Last reply Dec 14, 2024, 4:46 PM Reply Quote 0
    • C
      Coises @superfestung
      last edited by Coises Dec 14, 2024, 7:20 PM Dec 14, 2024, 4:46 PM

      @superfestung said in C# Plugin - Characters missing in message box:

      Any ideas what i am missing?

      I don’t know C#, but until someone comes along who does, I can at least point out that somewhere along the line, you are reading UTF-8 into something that expects UTF-16.

      I suspect that is happening when you use StringBuilder; it looks like that creates a UTF-16 buffer. You’re then sending a message to Scintilla, which returns UTF-8. Then you’re reading the characters (UTF-16, two bytes each) from StringBuilder into the bytes of UTF-8.

      My guess is that there is probably a sensible wrapper for Scintilla included somewhere in the C# plugin pack, so you don’t have to do everything with bare SendMessage calls, but I don’t follow how to read the source.

      If not, I think you need to pass the address of the utf8_Bytes buffer to SendMessage, rather than using StringBuilder; but I don’t know how that’s done in C#.

      Edit to add:

      While this isn’t your immediate problem, you’ll also need to consider that the data returned by Scintilla isn’t necessarily UTF-8; it could also be in the active system codepage. You will need to use SCI_GETCODEPAGE to know that. In C++, I use MultiByteToWideChar to convert between Scintilla’s representation and Windows’ wide characters.

      1 Reply Last reply Reply Quote 1
      • M
        Mark Olson
        last edited by Dec 14, 2024, 5:49 PM

        @superfestung
        I’m the maintainer of the plugin pack, but I’m out of town and can’t reach my computer until tomorrow evening. If you think this is a bug, consider opening an issue in the GH repo and I’ll look into it.

        1 Reply Last reply Reply Quote 0
        • M
          Mark Olson
          last edited by Dec 14, 2024, 6:04 PM

          @superfestung
          To convert a StringBuilder to a string, use its ToString method. And don’t cast a char to a byte. This is all basic C#, so read the docs.

          1 Reply Last reply Reply Quote 1
          • S
            superfestung
            last edited by Dec 14, 2024, 8:38 PM

            Thank you all for the answers!

            I found a working solution for me, much simpler than i thought, when you know it.
            @Coises You were right, i do not need the message calls.

                    static void HelpMe()
                    {       
                        int start = Npp.editor.GetSelectionStart();
                        int end = Npp.editor.GetSelectionEnd();
            
                        int bufferSize = start < end ? end - start : start - end;
            
                        if (bufferSize > 0)
                        {
                            string strTest = Npp.editor.GetSelText();
                            MessageBox.Show(strTest, "Search Variable Display", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            MessageBox.Show("No Characters selected!", "Search Variable Display", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        }
                    }
            
            A 1 Reply Last reply Dec 15, 2024, 12:49 AM Reply Quote 0
            • A
              Alan Kilborn @superfestung
              last edited by Dec 15, 2024, 12:49 AM

              @superfestung

              I’m not a guru of C#, but I’d think it would be simpler to do:

                      static void HelpMe()
                      {       
                                  string strTest = Npp.editor.GetSelText();
                                  if (strTest.Length > 0)
                                  {
                                      MessageBox.Show(strTest, "Search Variable Display", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                  }
                                  else
                                  {
                                      MessageBox.Show("No Characters selected!", "Search Variable Display", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                  }
                      }
              S 1 Reply Last reply Dec 15, 2024, 10:21 AM Reply Quote 3
              • S
                superfestung @Alan Kilborn
                last edited by Dec 15, 2024, 10:21 AM

                @Alan-Kilborn
                Yes! Now its a beauty…

                1 Reply Last reply Reply Quote 0
                5 out of 7
                • First post
                  5/7
                  Last post
                The Community of users of the Notepad++ text editor.
                Powered by NodeBB | Contributors