C向DDE服务器发送命令的示例

时间:2014-04-25 18:20:34

标签: c winapi dde

复制代码时遇到两个问题。我复制的代码如下所示。我在Windows XP上使用Code :: Blocks。

问题1:

在下面的代码中,我收到一条错误,上面写着“'MyDDECallBACK'未声明(首先在此函数中使用。”如果我将其替换为0,或注释掉该行,代码运行时没有错误(只要我也注释了问题2)。我不知道在哪里声明这个,因为示例没有声明它,或者我可能不确定这是什么。它似乎也不喜欢err = true,除非我大写真实?

if(DMLERR_NO_ERROR != DdeInitialize(&idInst, MyDDECallBack, APPCMD_CLIENTONLY, 0))
  err = true;

问题2:

用于代码末尾的“..”是什么?另外,如果我将其注释掉(连同问题1),代码就会运行。

再次感谢您的帮助!

// The following code fragment uses Windows DDEML APIs
// to send a move command to the Nucleus DDE Server
// and receives a status string.
//
// Variables and initial values.
HCONV hConv = NULL;

DWORD idInst = 0;
HSZ hszService = NULL;
HSZ hszTopic = NULL;
HSZ hszCmd = NULL;
BOOL err = FALSE;
HDDEDATA transResult = NULL;
DWORD dwResult = 0;
char result[300];
// Need long timeout value to allow the station to move and
// respond.
const long TIMEOUT = 60000;
// Initialize the DDEML environment. Create an Instance
// that is used in many other calls to DDE.
if(DMLERR_NO_ERROR != DdeInitialize(&idInst, MyDDECallBack,
                                    APPCMD_CLIENTONLY, 0))
  err = true;
// Create String handles for the server name, topic and item.
if(!err)
{
  hszService = DdeCreateStringHandle(idInst, "EDMAIN",
                                     CP_WINANSI);
  hszTopic = DdeCreateStringHandle(idInst, "CMI Commands",
                                   CP_WINANSI);
  hszCmd = DdeCreateStringHandle(idInst,
                                 ":MOVE:REL 2 100 100 NONE",
                                 CP_WINANSI);
  err = (hszService == NULL || hszTopic == NULL || hszCmd == NULL);
}
// Connect to the Nucleus DDE Server. (Open a conversation).
// Captain Picard would say, "Open a channel Mr. Wharf".
if(!err)
{
  hConv = DdeConnect(idInst, hszService, hszTopic, NULL);
  err = hConv == NULL;
  if(err)
    MessageBox(NULL, "Unable to make DDE connection.\n"
               "Make sure Nucleus is running.",
               "DDE CLIENT ERROR", MB_ICONSTOP);
}
// Send the command string to the server.
if (!err)
{
  transResult = DdeClientTransaction(NULL, 0, hConv, hszCmd,
                                     CF_TEXT, XTYP_REQUEST, TIMEOUT, NULL);
  // Read the result string. TransResult will be a
  // valid data handle if the client transaction above
  // was successful, and NULL if it was not. This must be
  // checked since calls to DdeGetData with a NULL handle
  // cause GPF’s.

  if(transResult)
    DdeGetData(transResult, (LPBYTE)result, sizeof(result), 0);
  // Display the result string.
  MessageBox(NULL, result, "RESULT", MB_ICONINFORMATION);
}
// Close the conversation.
if (hConv != NULL)
  DdeDisconnect( hConv );
// Delete the string handles.
if ((hszService != NULL) & (idInst != NULL))
  DdeFreeStringHandle( idInst, hszService );
if ((hszTopic != NULL) & (idInst != NULL))
  DdeFreeStringHandle( idInst, hszTopic );
if ((hszCmd != NULL) & (idInst != NULL))
  DdeFreeStringHandle( idInst, hszCmd );
// Clear out the DDEML environment.
if (idInst != NULL)
  DdeUninitialize(idInst);
..
// Since we are only doing requests from Nucleus, we don’t
// expect to get callbacks to this routine. Nevertheless,
// it is necessary to create a routine with no action.
HDDEDATA CALLBACK MyDDECallBack( UINT wType,
                                 UINT wFmt, HCONV HConv, 
                                 HSZ dataHandle1, HSZ dataHandle2,
                                 HDDEDATA data, DWORD myword1, 
                                 DWORD myword2)
{
  return NULL;
}

1 个答案:

答案 0 :(得分:0)

  1. 回拨是为了让您从服务中获得通知。删除此内容,您将无法收到您可能需要的通知。
  2. ..是示例代码方式的作者告诉你在这里放置你自己的逻辑 - 它在C或C ++中没有意义(但是3点...确实有函数声明中的含义)