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

时间:2015-11-03 12:10:48

标签: c++ windows winapi dde

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

来自官方docs

  

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

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

因此,根据XTYP_ADVDATA的{​​{3}},如果处理此消息,我需要返回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 /* ??? */;
}

1 个答案:

答案 0 :(得分:3)

在调用DdeInitialize()时,您只会收到您注册的消息类型(或者更准确地说,您不会过滤掉)。如果您只注册接收(或不忽略)XTYP_ADVDATA消息,那么您将收到所有消息,而您不必担心处理其他消息类型。您没有过滤掉的任何消息类型必须在回调中正确处理,相应地遵循每种消息类型的规则。

阅读DdeInitialize()的文档,注意其afCmd参数的说明。另请阅读有关DDE Basic Concepts的文档,特别是描述InitializationCallback function的部分。