如何在DLL中封装以太网通讯程序,使用了UDP等网络通讯控件?
正在制作一个电脑与考勤机通讯的程序,考勤机是以太网接口的,通讯部分的功能已完成,但是太零散了,想将通讯部分封装成一个DLL,可以随时调用,又可以为用户提供二次开发的机会。但是通讯程序中使用了三个INDY网络通讯的控件,如UDPCLIENT UDPSERVER IDANTIFREEZE等,如果想制作成DLL的话,如何做才能全部封状进去啊?有些程序都在这些控件的事件里的呀。
在线等,急急急!
请详细说明制作过程
我是第一次制作DLL,没有经验。所以请尽量说的简单点。
[解决办法]
个人建议:直接使用WinApi编写吧,封装到Dll里调用比较麻烦。
[解决办法]
我昨天也 碰到同样的问题,UDPSOCKET做好封进DLL时,用程序调用DLL就会出错,但是没有实质性的提示,郁闷啊
[解决办法]
主程序可以向DLL提供一个接口,这样DLL通过调用接口函数与主程序进行交互,就解决了事件的问题。
[解决办法]
unit MainUnit;interfaceuses Windows, SysUtils, Forms, SPComm, Messages;type TDllComm = class(TComm) private procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer; BufferLength: Word); end;const ERR_SC_SUCCESS = 0; ERR_SC_NOTACTIVE = 1; ERR_SC_BUSY = 2; ERR_SC_TIMEOUT = 3;var IsActive: Boolean = False; IsBusy: Boolean = False; Received: Boolean; RcvBuf: PChar; BufLen: PInteger; Comm: TDllComm; ReadTimeout: DWord;type TReceiveProc = function (RcvBuffer: PChar; Len: Integer): Integer;function OpenComm(Com: DWord; Bandrate: DWord): Boolean; stdcall;procedure CloseComm(); stdcall;function SendCommCmd(Command: PChar; RcvBuffer: PChar; var Len: Integer): Integer; stdcall;procedure SetCommTimeout(Timeout: DWord); stdcall;function SendData(Data: PChar; Len: Integer): Integer;procedure SetReceiveProc(OnReceive: TReceiveProc); stdcall;implementationvar ReceiveProc: TReceiveProc;procedure TDllComm.Comm1ReceiveData(Sender: TObject; Buffer: Pointer; BufferLength: Word);begin if IsBusy then begin CopyMemory(RcvBuf, Buffer, BufferLength); BufLen^ := BufferLength; Received := True; end; if Assigned(ReceiveProc) then ReceiveProc(Buffer, BufferLength);end;function OpenComm(Com: DWord; Bandrate: DWord): Boolean; stdcall;begin if IsActive then CloseComm(); ReadTimeout := 30000; Comm := TDllComm.Create(nil); Comm.CommName := 'COM' + IntToStr(Com); Comm.OnReceiveData := Comm.Comm1ReceiveData; Comm.BaudRate := Bandrate; Comm.Inx_XonXoffFlow := False; Comm.Outx_XonXoffFlow := False; try Comm.StartComm; Sleep(200); IsActive := True; except IsActive := False; end; Result := IsActive;end;procedure CloseComm(); stdcall;begin if not IsActive then Exit; Comm.StopComm; IsActive := False; Comm.Free;end;function SendCommCmd(Command: PChar; RcvBuffer: PChar; var Len: Integer): Integer; stdcall;var i: DWord;begin if not IsActive then begin Result := ERR_SC_NOTACTIVE; Exit; end; if IsBusy then begin Result := ERR_SC_BUSY; Exit; end; IsBusy := True; try Received := False; RcvBuf := RcvBuffer; BufLen := @Len; Comm.WriteCommData(Command, Len); i := GetTickCount(); while (not Received) and IsActive do begin if GetTickCount() - i >= ReadTimeout then begin Result := ERR_SC_TIMEOUT; Exit; end; Application.ProcessMessages; end; if not IsActive then begin Result := ERR_SC_NOTACTIVE; Exit; end; Result := ERR_SC_SUCCESS; finally IsBusy := False; end;end;procedure SetCommTimeout(Timeout: DWord); stdcall;begin ReadTimeout := Timeout;end;function SendData(Data: PChar; Len: Integer): Integer;begin if not IsActive then begin Result := ERR_SC_NOTACTIVE; Exit; end; if IsBusy then begin Result := ERR_SC_BUSY; Exit; end; IsBusy := True; try Comm.WriteCommData(Data, Len); Result := ERR_SC_SUCCESS; finally IsBusy := False; end;end;procedure SetReceiveProc(OnReceive: TReceiveProc);begin ReceiveProc := OnReceive;end;end.