Receiving strangers strings in target window from PostMessage api

149 views Asked by At

I have a Remote Administration Tool, and I'm with a trouble when receives strings in any window with PostMessage api. The strings are sent through of TextBox componente in Server application.

For example, the word Coder in StackOverflow window stays like this:

enter image description here

and my code that I'm using is this:

Server application

       private void tEnviarTexto_Click(object sender, EventArgs e)
        {

            String message = String.Format("<|MSG|>{0}", tEnviarMSG.Text);
            ListViewItem item = listView_clients.FocusedItem;
            String clientId = item.Name;
            RemoteClient client = this.remoteClientManager.GetClientById(clientId);
            if (client != null)
            {
                try
                {
                    client.mreMessage.WaitOne();
                    client.queueMessage.AddMessage(message);
                    tEnviarMSG.Text = "";
                }

                catch (Exception ex) { MessageBox.Show(ex.ToString()); }
            }
        }

Client application

       public void EnviatextoHWND(string texto)
        {
            foreach (char caractere in texto)
            {
                int charValue = caractere;
                string hexValue = charValue.ToString("X");
                IntPtr val = new IntPtr((Int32)caractere);

                string valor = val.ToString();

                if (valor == "66") // Letra B no Operador
                {
                    PostMessage(WindowHandle, WM_KEYDOWN, new IntPtr(VK_BACK), new IntPtr(0));
                    PostMessage(WindowHandle, WM_KEYUP, new IntPtr(VK_BACK), new IntPtr(0));

                }
                else if (valor == "83") // Letra S no Operador
                {
                    PostMessage(WindowHandle, WM_KEYDOWN, new IntPtr(VK_SPACE), new IntPtr(0));
                    PostMessage(WindowHandle, WM_KEYUP, new IntPtr(VK_SPACE), new IntPtr(0));

                }
                else
                {

                    PostMessage(WindowHandle, WM_KEYDOWN, val, new IntPtr(0));
                    PostMessage(WindowHandle, WM_CHAR, val, new IntPtr(0));
                    PostMessage(WindowHandle, WM_KEYUP, val, new IntPtr(0));

                } 
            }
        }

and usage:

public void CommunicationProc()
    {

      while (this.Connected)
        {
         else if (message.IndexOf("<|MSG|>") == 0)
          {
            String[] strSplit = value.Split(MenuRemoteClient.separator, StringSplitOptions.None);
            string msg = strSplit[0]; // content from a TextBox on Server application

                   if (this.remotedNav)
                     {
                       Thread SendTextoHWND = new Thread(() => EnviatextoHWND(msg));
                           SendTextoHWND.Start();
                            }
                        }
                }
             }

So, how solve it?

Any suggestions or guidance here is appreciated.

1

There are 1 answers

0
AudioBubble On BEST ANSWER

Fixed:

             //   PostMessage(WindowHandle, WM_KEYDOWN, val, new IntPtr(0));
                PostMessage(WindowHandle, WM_CHAR, val, new IntPtr(0));
            //    PostMessage(WindowHandle, WM_KEYUP, val, new IntPtr(0));