DDE Execute

The following example sends command to DDE Server. You can use our DDE Server example.

final String SERVICE = "MyServer";
final String TOPIC = "MyTopic";

try
{
    final DDEClientConversation conversation = new DDEClientConversation();
    // We can use UNICODE format if server prefers it
    //conversation.setTextFormat(ClipboardFormat.CF_UNICODETEXT);

    System.out.println("Connecting...");
    conversation.connect(SERVICE, TOPIC);

    System.out.println("Please, enter commands or 'exit' to quit");
    try
    {
        byte[] buff = new byte[1024];
        int ret_read = 0;
        do
        {
            ret_read = System.in.read(buff);
            if (ret_read > 0)
            {
                String data = new String(buff, 0, ret_read);
                data = data.trim();

                if (data.equals("exit"))
                    break;

                System.out.println("Sending:" + data);
                conversation.execute(data);
            }
        }
        while ((ret_read > 0));
    }
    finally
    {
        conversation.disconnect();
    }
}
catch (DDEMLException e)
{
    System.out.println("DDEMLException: 0x" + Integer.toHexString(e.getErrorCode()) + " " + e.getMessage());
}
catch (DDEException e)
{
    System.out.println("DDEClientException: " + e.getMessage());
}
catch (Exception e)
{
    System.out.println("Exception: " + e);
}

Download ExecuteExample.java
Download all examples and binaries

Command line to run example:

java -cp .;pretty-tools-JDDE-2.0.4.jar ExecuteExample
		

Why my command is not executed by DDE Server ?

Some times 3rd party DDE Servers ignores command that was sent from java code, or it may return error, but why this happens? The answer is hidden in implementation of DDE Server that cannot correctly detect data type of you command, because data type is not provided for XTYP_EXECUTE transaction.

If you do not know expected format then you need to guess it trying the following:
1. Try to send non-unicode command (probably you already tried it)

final DDEClientConversation conversation = new DDEClientConversation();
conversation.connect(SERVICE, TOPIC);
conversation.execute("my command");
2. Try to send unicode command
final DDEClientConversation conversation = new DDEClientConversation();
conversation.setTextFormat(ClipboardFormat.CF_UNICODETEXT);
conversation.connect(SERVICE, TOPIC);
conversation.execute("my command"); 
3. Try to send command as byte array
final DDEClientConversation conversation = new DDEClientConversation();
conversation.connect(SERVICE, TOPIC);
conversation.execute("my command".getBytes(/*you can try here also different encoding*/));