PierreAlyon
asked on
Comport in a DLL
Hello,
We are trying to build a DLL with the Comport component.
It just doesn’t work :(
I get an « Access violation error » as soon as I set any of the Comport parameters.
I have built a very simple example for testing purpose and I get the exact same error.
Please see my code and help me to fix problem.
It looks like if the DLL behaves as if it had all the memory of the computer at disposal.
Environment is : Windows 7, Delphi 7, Comport 3.1
We are trying to build a DLL with the Comport component.
It just doesn’t work :(
I get an « Access violation error » as soon as I set any of the Comport parameters.
I have built a very simple example for testing purpose and I get the exact same error.
Please see my code and help me to fix problem.
It looks like if the DLL behaves as if it had all the memory of the computer at disposal.
Environment is : Windows 7, Delphi 7, Comport 3.1
program Project1;
uses
ShareMem,
Forms,
testDLL in 'testDLL.pas' {Form1};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
Unit unit testDLL;
interface
uses
ShareMem,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TShowPort = procedure;stdcall;
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
ShowPort: TShowPort;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
DLLModule:THandle;
s:string;
begin
DLLModule := LoadLibrary('mydll.dll');
@ShowPort := nil;
@ShowPort := GetProcAddress(DLLModule, 'ShowPort');
ShowPort;
end;
end.
DLLlibrary mydll;
uses
ShareMem,SysUtils,Dialogs,CPort,
Classes;
var
ComPort1:Tcomport;
{$R *.res}
procedure ShowPort;stdcall;
begin
ShowMessage('address COMMPORT:'+IntToStr(Integer(@ComPort1)));
ShowMessage('adresse COMMPORT BAUDRATE:'+IntToStr(Integer(@ComPort1.BaudRate)));
ComPort1.BaudRate:=br2400; // <== access violation is here
end;
exports ShowPort;
begin
end.
Where do you initialize your comport object?
ASKER
I tried to initialize it in the DLL like this
procedure ShowPort;stdcall;
begin
ShowMessage('address COMMPORT:'+IntToStr(Integer(@ComPort1)));
ShowMessage('adresse COMMPORT BAUDRATE:'+IntToStr(Integer(@ComPort1.BaudRate)));
ComPort1:=ComPort1.Create(nil);
ComPort1.BaudRate:=br2400; // <== access violation is here
end;
but it does not work for the same reason.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes it works, thanks you !
I will remove the showmessage's.
Now i have another problem : how can I use RxChar event without access to the object inspector in my DLL?
I tried
I will remove the showmessage's.
Now i have another problem : how can I use RxChar event without access to the object inspector in my DLL?
I tried
Comport1.rxchar:= ComPort1RxChar;
with ComPort1RxChar declared like this : procedure ComPort1RxChar(Sender: TObject; InQue: Cardinal);virtual;
but it doesn't work.
must set which events you want to use like:
... adnremove virtual keyword too.
Comport1.Events := [evRxChar]; //there are more if you need
... adnremove virtual keyword too.
ASKER
I think that
Maybe I have to indicate the routine associated with the event?
Comport1.Events := [evRxChar];
isn't sufficient because I receive nothing on my comport component. Maybe I have to indicate the routine associated with the event?
did you send something before waiting to receive?
you must set correct settings:
you must set correct settings:
FComPort.BaudRate:=br9600;
FComPort.StopBits:=sbOneStopBit;
FComPort.DataBits:=dbEight;
FComPort.Port:='COM1';
FComPort.Parity.Bits:=prNone;
FComPort.FlowControl.FlowControl := fcHardware;
//or FComPort.FlowControl.FlowControl := fcNone;
...
//dtr/rts...
ASKER
I send an ENQ before waiting to receive and I set all settings of ComPort component.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for your help but I finally put my ComPort component into a form to create the RxChar event with the object inspector and it works.