C ++和Delphi之间的通信 [英] Communication between C++ and Delphi

查看:161
本文介绍了C ++和Delphi之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我试着在这里解释一下我的问题。



我有一个DLL,它是用C ++编写的。它监视所有打印对象的传入打印作业(通过线程),然后从打印详细信息中获取一些信息。到这里可以。现在我必须将此信息发送给加载此C ++ DLL的delphi项目。



我真的很无能为力如何实现这种沟通



让c ++代码具有以下功能:

初始化或开始监控打印对象(启动线程)

如果有任何打印作业,提取asome信息

现在我必须发送将此信息提供给等待它的Delphhi代码。



让Delphi代码如下:

[DELPHI CODE]

加载DLL;

等待消息

卸载DLL



可以任何一个给我任何整体的想法。无需告诉我代码,但需要一些机制。



更新:

使用共享内存是解决方案吗?

谢谢。

Hi, Let me try to explain my problem here.

I have a DLL, it is written in C++. It monitors all print objects for incoming print jobs (Through a Thread)and then fetch some information from print details. Upto here it is OK. Now I have to send this information to delphi project who loaded this C++ DLL.

I am really clueless how can I make this communication happen

Let the c++ code has following functionality:
Initialize or start monitoring print objects (starts thread)
if any print job comes, asome information is fetched
Now I have to send back this info to that Delphhi code who is waiting for it.

Let the Delphi code is like below:
[DELPHI CODE]
Load DLL;
Wait for message
Unload DLL

Can any one give me any overall all idea. No need to tell me the code but some mechanism is needed.

update:
Is use of share memory is that solution?
Thanks.

推荐答案

如果您有两个部分的源代码,C ++和Delphi并且您使用C ++ Builder / Rad Studio构建了这两个部分,那么您可以从Delphi调用C ++代码,反之亦然。

在这种情况下,你可以通过回调做你想做的事。您将Delphi函数的地址作为函数指针参数传递给C ++。然后在C ++有要发回的消息时使用该指针调用Delphi函数。

这可能听起来很复杂,但它比共享内存简单得多。

您的C ++ Builder / Rad Studio应该有调用另一种代码的样本,在C ++函数指针中添加一些谷歌研究,你应该能够使它工作。
If you have the source to both parts, C++ and Delphi and you build both with C++ Builder/Rad Studio then you can call the C++ code from the Delphi and the other way around too.
In that case you could do what you want with a callback. You pass the address of a Delphi function into the C++ as a function pointer parameter. Then call the Delphi function using that pointer when the C++ has a message to send back.
It may sound complicated but it''s a lot simpler than shared memory.
Your C++ Builder/Rad Studio should have samples for calling one kind of code from the other, add a little google research into C++ function pointers and you should be able to make it work.


假设你使用的是Visual C ++,你可以使用COM,这是一个非常好的教程:为VS2012编写简单COM / ATL DLL的初学者教程 [ ^ ]



Delphi适用于COM,因此访问COM对象应该非常简单。



最好的问候

Espen Harlinn
Assuming you''re using Visual C++, you could use COM, here is a pretty nice tutorial: A Beginner Tutorial for Writing Simple COM/ATL DLL For VS2012[^]

Delphi works pretty well with COM, so accessing the COM object should be quite easy.

Best regards
Espen Harlinn


C++ DLL: "MyDll.dll"
/////////////////////////////////////////////////////
typedef HRESULT (CALLBACK * CALLBACKPROC)(DWORD wEvent,LONG_PTR arg,PVOID pParam);
/////////////////////////////////////////////////////
CALLBACKPROC g_pCallback = NULL;
LPVOID g_pCallbackParam = NULL;
/////////////////////////////////////////////////////
EXTERN_C VOID __declspec(dllexport) WINAPIV SetCallback(CALLBACKPROC _proc,LPVOID pParam)
{
g_pCallback = _proc;
g_pCallbackParam = pParam;
}
/////////////////////////////////////////////////////
#define SOMEDLLMETHOD_EVENT_ID 0x01
/////////////////////////////////////////////////////
EXTERN_C VOID __declspec(dllexport) WINAPIV SomeDllMethod()
{
if (g_pCallback) g_pCallback(SOMEDLLMETHOD_EVENT_ID,(LONG_PTR)"some argument",g_pCallbackParam);
}
/////////////////////////////////////////////////////
// Delphi wrapper with dynamic DLL linkage:
/////////////////////////////////////////////////////
type
TDllWrapper = class
protected
FOnDllEvent: TNotifyEvent;
m_hDll: HMODULE;
protected
function OnCallbackEvent(wEvent:DWORD;arg:Pointer):HRESULT; virtual;
public
procedure CallSomeDllMethod();
public
constructor Create;
destructor Destroy;
published
property OnDllEvent: TNotifyEvent read FOnDllEvent write FOnDllEvent;
end;
 
type
TCALLBACKPROC = function(wEvent:DWORD;arg:Pointer;pParam:Pointer):HRESULT stdcall;
type
TSetCallbackFunc = procedure(_proc:TCALLBACKPROC;pParam:Pointer) cdecl;
 
const SOMEDLLMETHOD_EVENT_ID =


这篇关于C ++和Delphi之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆