tags:

views:

29

answers:

1

Hello,

Why is it that DdeConnect is failing on Unicode but yet working on ANSI?

1) Open Excel and enter some dummy data

2) Create a sample code to read value from Excel via DDE

3) ANSI = Success, Unicode = FAIL

Sample code below. I am getting DDE Connection Failed everytime.

WCHAR szApp[] = L"Excel";
    WCHAR szTopic[] = L"C:\\Test.xlsx";
    char szItem1[] = "R1C1";  char szDesc1[] = "Current Value: ";
DWORD idInst=0;
  UINT iReturn;
            iReturn = DdeInitialize(&idInst, (PFNCALLBACK)DdeCallback, 
                APPCLASS_STANDARD | APPCMD_CLIENTONLY, 0 );
            if (iReturn!=DMLERR_NO_ERROR)
            {
                printf("DDE Initialization Failed: 0x%04x\n", iReturn);
                Sleep(1500);
                return 0;
            }
HSZ hszApp, hszTopic;
    HCONV hConv;
    hszApp = DdeCreateStringHandle(idInst, (LPCWSTR)szApp, 0);
    hszTopic = DdeCreateStringHandle(idInst, (LPCWSTR)szTopic, 0);
    hConv = DdeConnect(idInst, hszApp, hszTopic, NULL);
DdeFreeStringHandle(idInst, hszApp);
    DdeFreeStringHandle(idInst, hszTopic);
    if (hConv == NULL)
    {
        printf("DDE Connection Failed.\n");
        Sleep(100); DdeUninitialize(idInst);
        return 0;
    }
+1  A: 

Did you DDEInitialize in Unicode mode? the DDEInitialize and DDEConnect modes have to match. So if you haven't defined UNICODE, you'd have to explicitly call DDEInitializeW before specifying a DDEConnect in CP_WINUNICODE mode. That's how I read the docs.

Bob Moore
Hello, does the server have to support UNICODE too?
Well, we've opened a can of worms here. A little googling shows that Excel supports a format called xltable for DDE. I think you need to hit the Excel docs and see if they've added an option to force xltable OFF and unicode ON. I don't know enough about Excel to help with that.
Bob Moore