简体   繁体   中英

Callback within Delphi application from within C++ DLL

we have a delphi application which contains a TXTextControl (v. 13.0). This application uses a C++ DLL (written with visual studio 2008) in order to process speech recognition. When the DLL is initialized, the DispatchInterface of the TXTextControl is passed from delphi application to the DLL and stored for later use. When an event of text recognition is received within the DLL code, the IDispatch interface reference to the TXTextControl is used to invoke the method setText on the the text control as follow:

  try{
    CString val(text.c_str());
    this->_txtInterface.SetSelLength(0);
    this->_txtInterface.SetSelText(val);
  }catch(...){
    LOG4CXX_FATAL(getLogger(), "COM exception!");
  }

  LOG4CXX_DEBUG(getLogger(), "OK");

where _txtInterface is the TXTextXontrol interface stored when DLL has been initialized.

But when the interface is invoked, nothing happen and the following loge instruction code is not executed. It seems that the IDispatch interface is no more valid.

Within delphi the idispatch interface is passed with following code during DLL initialization:

tmpRedit := TTxtextcontrol(tmpvocField.obj);
ires := gst.CreateNewDocument( tmpvocfield.DocID , tmpRedit.DefaultInterface , '');

where "tmpvocField.obj" is the reference to the TXTextControl within delphi.

What could be the problem? Is this a threading problem? I mean the TXTextControl is in thread 1 but the text recognition event is in a different thread (thread 2) so the idispatch is no more valid in the second thread?

Thanks for any response. Paolo.

In c++ there is no automatic reference counting like Delphi does for interfaces. You need to call AddRef to make sure the refcount is at least 1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM