Home > Net >  Send Text to Another Application C# WPF Runnable on Windows
Send Text to Another Application C# WPF Runnable on Windows

Time:09-22

I have simple TCP/IP Server Windows and Client Android. How I can send received text to another appilication for example notepad or excel cell, any window application with input field. Data Received in background

 private void Events_DataRecieved(object sender, SimpleTcp.DataReceivedEventArgs e)
    {
        var ipandPort = e.IpPort;
        var data = Encoding.UTF8.GetString(e.Data);
        Console.WriteLine(data);  
        Send(data);
    }

I try this

 [DllImport("user32.dll")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);

    public void Send(String data)
    {
        System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName("notepad"); //search for process notepad
        if (p.Length > 0) //check if window was found
        {
            SetForegroundWindow(p[0].MainWindowHandle); //bring notepad to foreground
        }

        SendKeys.SendWait(data); //send key to notepad
    }

How I can send text to any application runnuble on PC after DataRecieved. Like https://barcodetopc.com/ here Thx for help.

CodePudding user response:

As far as I can see you are using the SimpleTcp Nuget package. You can use several 3rd party applications to control the data exchange. Examples of these applications are Hercules or Docklight.

You can create a server in Hercules with your own IP address and the port you specify. Just come to the server tab and say Listen.

In C# application, you can first create a Client.

string IpAddress = "192.168.1.40"; // Example Ip Address
string Port = "23";

public SimpleTcpClient tcpClient = new SimpleTcpClient(Ip, Convert.ToInt32(Port), false, null, null);
tcpClient.Connect();

if (tcpClient.IsConnected)
{
    tcpClient.Events.DataReceived  = Events_DataReceived;
    tcpClient.Events.Connected  = Events_Connected;
    tcpClient.Events.Disconnected  = Events_Disconnected;
}

If you want to listen to messages sent to you

private void Events_DataRecieved(object sender, SimpleTcp.DataReceivedEventArgs e)
{
        string IpPort = e.IpPort;
        var data = Encoding.UTF8.GetString(e.Data);
        Console.WriteLine(data);
}

If you want to send a message

byte[] message = new byte[] {0x00, 0x01, 0x02, 0x03};
private void EthernetSend()
{
    
    tcpClient.Send("Message"); // If you want to send a string expression
    tcpClient.Send(message); // If you want to send a hexadecimal expression
}

CodePudding user response:

Solved problem by using Clipboard

    public void Send2(String data)
    {

        Thread thread = new Thread(() => System.Windows.Clipboard.SetText(data));
        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();
        thread.Join();

        SendKeys.SendWait("^v");

    }
  • Related