我是從How to use Dynamic Data Exchange (DDE) with Word and Excel from Visual C++的例子,我以某種方式設法能夠從Excel中檢索值。如何通知動態數據交換(DDE)的任何更新?
現在的問題是,在這個例子中,我必須再次運行該應用程序以獲得必要的更新。我如何做到這一點,如果在Excel中有更新,我的應用程序將被通知? DdeAdvise?
在此先感謝! Excel只是一個例子!不,我需要DDE而不是COM,即使它老了。
// DDEExample.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include "ddeml.h"
#define PAUSE system("pause")
HDDEDATA CALLBACK DdeCallback(UINT uType, UINT uFmt, HCONV hconv,
HSZ hsz1, HSZ hsz2, HDDEDATA hdata,
ULONG_PTR dwData1, ULONG_PTR dwData2)
{
printf("uType: %d", uType);
switch(uType)
{
case XTYP_REQUEST:
printf("XTYP_REQUEST\n");
break;
}
return 0;
}
void DDERequest(DWORD idInst, HCONV hConv, char* szItem, char* sDesc)
{
HSZ hszItem = DdeCreateStringHandle(idInst, szItem, 0);
HDDEDATA hData = DdeClientTransaction(NULL,0,hConv,hszItem,CF_TEXT,
XTYP_REQUEST,5000 , NULL);
if (hData==NULL)
{
printf("Request failed: %s\n", szItem);
}
else
{
char szResult[255];
DdeGetData(hData, (unsigned char *)szResult, 255, 0);
printf("%s%s\n", sDesc, szResult);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
char szApp[] = "EXCEL";
char szTopic[] = "C:\\Test.xlsx";
char szCmd1[] = "[APP.MINIMIZE()]";
char szItem1[] = "R1C1"; char szDesc1[] = "A1 Contains: ";
UINT uiResult;
DWORD m_dwDDEInstance = 0;
uiResult = DdeInitialize(&m_dwDDEInstance, (PFNCALLBACK) &DdeCallback, APPCLASS_STANDARD|APPCMD_CLIENTONLY, 0);
if(uiResult != DMLERR_NO_ERROR)
{
printf("DDE Initialization Failed: 0x%04x\n", uiResult);
return FALSE;
}
printf("m_dwDDEInstance: %u\n", m_dwDDEInstance);
//PAUSE;
HSZ hszApp, hszTopic;
HCONV hConv;
hszApp = DdeCreateStringHandle(m_dwDDEInstance, szApp, 0);
hszTopic = DdeCreateStringHandle(m_dwDDEInstance, szTopic, 0);
hConv = DdeConnect(m_dwDDEInstance, hszApp, hszTopic, NULL);
DdeFreeStringHandle(m_dwDDEInstance, hszApp);
DdeFreeStringHandle(m_dwDDEInstance, hszTopic);
if(hConv == NULL)
{
printf("DDE Connection Failed.\n");
}
DDERequest(m_dwDDEInstance, hConv, szItem1, szDesc1);
DdeDisconnect(hConv);
DdeUninitialize(m_dwDDEInstance);
PAUSE;
}
有一些令人信服的理由使用DDE而不是自動化? – 2010-07-22 06:19:42
@Marcelo Cantos 你好,謝謝你的回覆。 DDE是通信應用程序提供的唯一選項,我認爲DDE對於應用程序通信而言是相當「開放」的。 – user303907 2010-07-22 06:30:32
@ user303907:DDE是一個錯誤的混亂。不要使用它。研究其他可能的解決方案,例如OLE。否則,我不認爲Office有用戶輸入通知。 – Dummy00001 2010-07-22 09:18:30