我需要在我的 delphi XE3 应用程序中使用 DLL,我收到了一个用 c++ 编写的演示应用程序,它显示了如何调用 DLL。
我已经成功调用了 DLL 和 dll 的 Initialize 方法,但是我没有从 DLL 得到任何消息。
这是 C++ 源代码:
创建消息处理程序:
#define WM_POSSTATE WM_APP+1
#define ON_WM_POSSTATE() \
{ WM_POSSTATE, 0, 0, 0, AfxSig_vwl, \
(AFX_PMSG)(AFX_PMSGW)(void (AFX_MSG_CALL CWnd::*)(UINT, LPARAM))&OnPOS },
Initialize 方法的定义:
typedef int (INITIALIZE)( char * cPort , UINT Msg, HWND *hWnd_p );
INITIALIZE *Initialize;
HMODULE hPosDll;
加载 DLL:
if( (hPosDll = LoadLibrary( "posdll.dll" ) ) == NULL )
{
MessageBox( "Error: can not open posdll.dll",NULL,MB_OK);
exit(1);
}
if( (::Initialize = (INITIALIZE*)GetProcAddress( hPosDll, "Initialize" )) == NULL )
{
MessageBox( "Error: can not find function",NULL,MB_OK);
FreeLibrary( hPosDll );
exit(1);
}
调用初始化方法:
res=::Initialize('com3', WM_POSSTATE , &m_hWnd);
if( res )
{
MessageBox( "Error opening comms",NULL,MB_OK);
FreeLibrary( hPosDll );
exit(1);
}
在 Initialize 调用之后,从 DLL 到应用程序的消息由 OnPos 方法处理:
void CPosDemoDlg::OnPOS(UINT Result, LPARAM Param )
{
DoStuff;
}
在德尔福我到目前为止:
unit UFrmMain;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, MCPOSDLL;
const
WM_POSSTATE = WM_APP + 1;
MCDLL = 'posdll.dll';
type
TInitialize = function(cport: PAnsiChar; Msg: Integer; Handle: HWND):Integer; stdcall;
TFrmMain = class(TForm)
EdCOMPort: TEdit;
BtnConnect: TButton;
LblCOMPort: TLabel;
procedure ON_WM_POSSTATE(var Msg: TMessage); message WM_POSSTATE;
procedure BtnConnectClick(Sender: TObject);
private
DLLHandle: THandle;
public
{ Public declarations }
end;
var
FrmMain: TFrmMain;
implementation
{$R *.dfm}
{-------------------------------------------------------------------------------}
procedure TFrmMain.BtnConnectClick(Sender: TObject);
var
MCInitialize: TInitialize;
Res: Integer;
begin
DLLHandle := LoadLibrary(PChar(MCDLL));
if DLLHandle <> 0 then
begin
@MCInitialize := getProcAddress(DLLHandle, 'Initialize');
if @MCInitialize <> NIL then
begin
Res := MCInitialize(PAnsiChar('com3'), WM_POSSTATE, Self.Handle);
if Res <> 0 then
begin
MessageDlg('Error opening comms', mtWarning, [mbOK], 0);
end;
end;
end
else
begin
MessageDlg('posdll.dll could not be located.', mtWarning, [mbOK], 0);
end;
end;
{-------------------------------------------------------------------------------}
procedure TFrmMain.ON_WM_POSSTATE(var Msg: TMessage);
begin
showmessage('Message received');
end;
end.
使用此解决方案,我可以激活连接到 com3 的设备,但永远不会触发消息处理程序。
我认为&m_hWnd
在 C++ 中使用 Initialize 方法发送的内容与Self.Handle
我在 Delphi 中发送的内容不同。
谁能帮我吗?提前致谢。