如何在DdeCallback函数中实现返回代码

How to implement return codes in DdeCallback function

本文关键字:实现 返回 代码 函数 DdeCallback      更新时间:2023-10-16

编写DdeCallback函数的正确方法是什么?更确切地说,我说的是返回代码。

来自官方文件:

返回值取决于事务类。了解更多信息有关返回值的信息,请参阅个别交易类型

例如,我的应用程序需要自己处理XTYP_ADVDATA消息,而忽略其他消息。

因此,根据XTYP_ADVDATA的文档,如果我处理了以下消息,我需要返回DDE_FACK

如果DDE回调函数处理此问题,则应返回DDE_FACK事务DDE_FBUSY(如果太忙而无法处理该事务),或DDE_FNOTPROCESSED(如果拒绝此事务)

但其他信息呢?在其他情况下,我应该返回什么?

//初始化

DWORD id_inst = 0;
UINT res = DdeInitializeA(
  &id_inst,
  (PFNCALLBACK)DdeCallback,
  APPCLASS_STANDARD | APPCMD_CLIENTONLY,
  0 // Reserved; must be set to zero
);
// XTYP_ADVSTART
HDDEDATA data = DdeClientTransaction(
  NULL,   // The beginning of the data the client must pass to the server. This parameter is required only if the wType parameter is XTYP_EXECUTE or XTYP_POKE. Otherwise, this parameter should be NULL
  0,      // The length, in bytes, of the data pointed to by the pData parameter
  conv,
  item,
  CF_TEXT,
  XTYP_ADVSTART,
  30000,  // The maximum amount of time, in milliseconds, that the client will wait for a response from the server application in a synchronous transaction
  NULL    // A pointer to a variable that receives the result of the transaction. An application that does not check the result can use NULL for this value
);

HDDEDATA CALLBACK DdeCallback(
  UINT uType,     // The transaction type
  UINT uFmt,      // The format atom of the data sent from the server
  HCONV hconv,    // A handle to the conversation
  HSZ hsz1,       // A handle to the topic name
  HSZ hsz2,       // A handle to the item name
  HDDEDATA hdata, // A handle to the data associated with the topic name and item name pair
  DWORD dwData1,  // Not used
  DWORD dwData2)  // Not used
{
  switch (uType)
  {
  case XTYP_ADVDATA:
    DWORD data_size = DdeGetData(hdata, NULL, 0, 0);
    std::unique_ptr<char[]> buf(new char[data_size]);
    DdeGetData(
      hdata,
      (BYTE *)buf.get(),
      data_size,
      0 // An offset within the DDE object. Data is copied from the object beginning at this offset
    );
    std::cout << "Data received: " << buf.get() << std::endl;
    return (HDDEDATA)DDE_FACK;
  }
  return /* ??? */;
}

在调用DdeInitialize()时,您只会收到您注册的消息类型(或者更准确地说,您不会过滤掉)。如果您只注册接收(或不忽略)XTYP_ADVDATA消息,那么您将只收到这些消息,并且不必担心处理其他消息类型。任何未过滤掉的消息类型都必须根据每个消息类型的规则在回调中正确处理。

阅读DdeInitialize()的文档,注意其afCmd参数的说明。另请阅读有关DDE基本概念的文档,特别是描述初始化和回调函数的部分。