为什么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;
    }

最佳答案

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

10-02 10:16