How to Run Excel as a DDE Server
- 1). Open Visual C++ 6.0 and start a "New Project."
- 2). Click on "Win32Application" to select it as the project type, then name it "DdemlSvr." Select "An Empty Project" when asked for the type of project you want.
- 3). Add a file called "main.cpp." to your project.
- 4). Copy the following code:
#include <windows.h>
#include <stdio.h>
#include <ddeml.h>
// Globals...
HSZ g_hszAppName;
HSZ g_hszTopicName;
HSZ g_hszItemName;
int g_count = 0;
DWORD g_idInst = 0;
// Declarations:
HDDEDATA EXPENTRY DdeCallback(UINT type, UINT fmt, HCONV hConv, HSZ hsz1, HSZ hsz2, HDDEDATA hData, DWORD dwData1, DWORD dwData2);
// WinMain()..
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// Initialize DDEML...
if(DdeInitialize(&g_idInst, DdeCallback, APPCLASS_STANDARD, 0)) {
MessageBox(NULL, "DdeInitialize() failed", "Error", MB_SETFOREGROUND);
return -1;
}
// Create string handles...
g_hszAppName = DdeCreateStringHandle(g_idInst, "DdemlSvr", NULL);
g_hszTopicName = DdeCreateStringHandle(g_idInst, "MyTopic", NULL);
g_hszItemName = DdeCreateStringHandle(g_idInst, "MyItem", NULL);
if( (g_hszAppName == 0) || (g_hszTopicName == 0) || (g_hszItemName == 0) ) {
MessageBox(NULL, "DdeCreateStringHandle() failed", "Error", MB_SETFOREGROUND);
return -2;
}
// Register DDE server
if(!DdeNameService(g_idInst, g_hszAppName, NULL, DNS_REGISTER)) {
MessageBox(NULL, "DdeNameService() failed!", "Error", MB_SETFOREGROUND);
return -3;
}
// Create a timer to simulate changing data...
SetTimer(0,0,1,0);
// Message loop:
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
// On WM_TIMER, change our counter, and update clients...
if(msg.message == WM_TIMER) {
g_count++;
DdePostAdvise(g_idInst, g_hszTopicName, g_hszItemName);
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
// Our DDE Callback function...
HDDEDATA EXPENTRY DdeCallback(UINT wType, UINT fmt, HCONV hConv, HSZ hsz1, HSZ hsz2, HDDEDATA hData, DWORD dwData1, DWORD dwData2) {
switch (wType) {
// ----------------------------------------------------------------
case XTYP_CONNECT:
// Client is trying to connect. Respond TRUE if we have what they want...
if ((!DdeCmpStringHandles(hsz1, g_hszTopicName)) &&
(!DdeCmpStringHandles(hsz2, g_hszAppName)))
return (HDDEDATA)TRUE; // SERVER supports Topic|Service
else
return FALSE; // SERVER does not support Topic|Service
// ----------------------------------------------------------------
case XTYP_ADVSTART:
// Client starting advisory loop.
// Say "ok" if we have what they are asking for...
if((!DdeCmpStringHandles(hsz1, g_hszTopicName)) &&
(!DdeCmpStringHandles(hsz2, g_hszItemName)))
return (HDDEDATA)TRUE; // SERVER supports Topic|Service
else
return FALSE; // SERVER does not support Topic|Service
// ----------------------------------------------------------------
case XTYP_ADVREQ:
// Client wants our data. Since this is specific to Excel, we'll
// go ahead and assume they want XlTable-formatted data. For a
// generic DDE server, you might want to handle various formats
// specified by the passed in fmt parameter.
if(!DdeCmpStringHandles(hsz1, g_hszTopicName) &&
!DdeCmpStringHandles(hsz2, g_hszItemName)) {
short xltableData[100];
// tdtTable record...
xltableData[0] = 0x0010; // tdtTable
xltableData[1] = 4; // 2 short ints following
xltableData[2] = 1; // # rows
xltableData[3] = 1; // # cols
// tdtInt record...
xltableData[4] = 0x0006;
xltableData[5] = 2;
xltableData[6] = (short)g_count;
return DdeCreateDataHandle(g_idInst, (UCHAR*)xltableData, 2*7, 0, g_hszItemName, fmt, 0);
}
// ----------------------------------------------------------------
default:
return (HDDEDATA)NULL;
}
} - 5). Paste the code into the file "main.cpp" that you created.
- 6). Compile and then start up the project.
- 1). Press "Ctrl" + "Shift" + "Esc" to bring up the Windows Task Manager, then select the "Processes" tab.
- 2). Ensure that "DdemlSvr.exe" is one of the applications running on the list.
- 3). Start up Microsoft Office Excel and type "=DdemlSvr|MyTopic!MyItem" into a cell. The cell should now contain the increasing value of the g_count.
- 1). Run the "DdemlSvr.exe" in a Windows NT 4.0 machine that is networked. Then start "DdeShare" from the command line.
- 2). Click "Shares" from the menu that appears, then "DDE Shares," followed by "Add a Share."
- 3). Enter the following values under "Application Name":
Share Name: MyShare$
Old Style: DdemlSvr.DDE
New Style: DdemlSvr.OLE
Static: DdemlSvr - 4). Enter "MyTopic" into the three boxes under "Topic Name." Select "OK."
- 5). Click on "MyShare$," then "Trust Share," then the "Initiate to Application Enable" checkbox. Select "OK" in all of the dialog boxes and then quit "DdeShare."
- 6). Run Excel on a different Windows NT 4.0 machine, but still on the same network.
- 7). Copy the following:
='\\SERVERNAME\NDDE$'|'MyShare$'!MyItem - 8). Paste what you copied into a cell in Excel. Replace "SERVERNAME" with the name of your network server machine. You should see a rapidly increasing number in the cell.
Build a DDE Server
Test Your Server
Use the Server Remotely
Source...