简体   繁体   English

从C ++ DLL内部在Delphi应用程序中进行回调

[英]Callback within Delphi application from within C++ DLL

we have a delphi application which contains a TXTextControl (v. 13.0). 我们有一个包含TXTextControl(v。13.0)的delphi应用程序。 This application uses a C++ DLL (written with visual studio 2008) in order to process speech recognition. 此应用程序使用C ++ DLL(用Visual Studio 2008编写)来处理语音识别。 When the DLL is initialized, the DispatchInterface of the TXTextControl is passed from delphi application to the DLL and stored for later use. DLL初始化后,TXTextControl的DispatchInterface从delphi应用程序传递到DLL,并存储以供以后使用。 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: 当在DLL代码中收到文本识别事件时,对TXTextControl的IDispatch接口引用用于调用文本控件上的setText方法,如下所示:

  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. 其中_txtInterface是DLL初始化后存储的TXTextXontrol接口。

But when the interface is invoked, nothing happen and the following loge instruction code is not executed. 但是,当调用接口时,什么也不会发生,并且以下loge指令代码也不会执行。 It seems that the IDispatch interface is no more valid. 似乎IDispatch接口不再有效。

Within delphi the idispatch interface is passed with following code during DLL initialization: 在delphi中,在DLL初始化期间,使用以下代码传递idispatch接口:

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

where "tmpvocField.obj" is the reference to the TXTextControl within delphi. 其中“ tmpvocField.obj”是delphi中对TXTextControl的引用。

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? 我的意思是TXTextControl在线程1中,但是文本识别事件在另一个线程(线程2)中,所以idispatch在第二个线程中不再有效吗?

Thanks for any response. 感谢您的任何回复。 Paolo. 保罗

In c++ there is no automatic reference counting like Delphi does for interfaces. 在c ++中,没有像Delphi那样对接口进行自动引用计数。 You need to call AddRef to make sure the refcount is at least 1. 您需要调用AddRef以确保refcount至少为1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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