Link to home
Start Free TrialLog in
Avatar of PierreAlyon
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

program Project1;
uses
  ShareMem,
  Forms,
  testDLL in 'testDLL.pas' {Form1};
{$R *.res}
begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.

Open in new window

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.

Open in new window

DLL
library 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.

Open in new window

Avatar of Ephraim Wangoya
Ephraim Wangoya
Flag of United States of America image

Where do you initialize your comport object?
Avatar of PierreAlyon
PierreAlyon

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;

Open in new window

 but it does not work for the same reason.
SOLUTION
Avatar of Geert G
Geert G
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Comport1.rxchar:= ComPort1RxChar;

Open in new window

with ComPort1RxChar declared like this :
 procedure ComPort1RxChar(Sender: TObject; InQue: Cardinal);virtual;

Open in new window

but it doesn't work.
must set which events  you want to use like:

Comport1.Events := [evRxChar]; //there are more if you need

Open in new window


... adnremove virtual keyword too.
I think that
Comport1.Events := [evRxChar]; 

Open in new window

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:
        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...

Open in new window

I send an ENQ before waiting to receive and I set all settings of ComPort component.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.