DDEConnect在Unicode上失败

时间:2010-07-29 07:28:22

标签: c++ windows

为什么DdeConnect在Unicode上失败但仍在使用ANSI?

1)打开Excel并输入一些虚拟数据

2)创建一个示例代码,通过DDE从Excel读取值

3)ANSI =成功,Unicode =失败

以下示例代码。我每次都在DDE连接失败。

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 个答案:

答案 0 :(得分:1)

您是否在Unicode模式下进行DDEInitialize? DDEInitialize和DDEConnect模式必须匹配。因此,如果尚未定义UNICODE,则必须在CP_WINUNICODE模式下指定DDEConnect之前显式调用DDEInitializeW。这就是我阅读文档的方式。