[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Dll Access Violation

Posted on 2003-12-04
2
Medium Priority
?
603 Views
Last Modified: 2010-05-18
I've created  a small Dll, code below;

library rsSecureFuncts;

uses
  FastShareMem, SysUtils, IdHash, IdHashMessageDigest;

{$R *.res}

{ Create & Return MD5 Hash value }
function rsReturnMD5(Value: String): String; stdcall;
Var
  Digest: T4x4LongWordRecord;
  S,
  S1: String;
  i: Integer;
begin

  SetLength(S, 16);
  with TIdHashMessageDigest5.Create do
  begin
    Digest := HashValue('roland');
    Move(Digest, S[1], 16);
    for i := 1 to Length(S) do begin
      S1 := S1 + Format('%02x', [Byte(S[i])]);
    end;

    Result := S1;
    Free;
  end;

end;

exports
  rsReturnMD5 name 'rsReturnMD5';

begin

end.

i then compile it then put in in a test project folder, with the code for the test project being;

unit Unit1;

interface

uses
  FastShareMem, Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  function rsReturnMD5(Value: String): String; stdcall;

var
  Form1: TForm1;

function rsReturnMD5(Value: String): String; external 'rsSecureFuncts.dll' name 'rsReturnMD5';

implementation

{$R *.dfm}

uses
IdHash, IdHashMessageDigest;




procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(rsReturnMD5(edit1.Text));
end;

end.

Now when i run the test project it seems to work fine, but when i close the test project i get about 30 dialog errors, with the final one saying an access violation occured in rsSecureFuncts.dll.... but i cant work out what i've done wrong.
the code works fine if i take it out of the dll and put everything in the project, but i really want to use dlls.

Any ideas?

Thanks

Roland

0
Comment
Question by:rpter
2 Comments
 
LVL 2

Accepted Solution

by:
xxflip earned 1000 total points
ID: 9875092
I get the same problem as you, here's what I did.

I removed the sharemem from the use caluse in both projects (which was the cause of the error) and used PChar instead of String to pass the values between the app and the dll.

it's working fine.


-------------------  Your code with my changes ---------------------------
-------------------  rsSecureFuncts.dll ---------------------------
library rsSecureFuncts;

uses
  SysUtils,
  IdHash,
  IdHashMessageDigest;

{$R *.res}

{ Create & Return MD5 Hash value }
function rsReturnMD5(Value: PChar): PChar; stdcall;
Var
 Digest: T4x4LongWordRecord;
 S,
 S1: String;
 i: Integer;
begin

 SetLength(S, 16);
 with TIdHashMessageDigest5.Create do
 begin
   Digest := HashValue('roland');
   Move(Digest, S[1], 16);
   for i := 1 to Length(S) do begin
     S1 := S1 + Format('%02x', [Byte(S[i])]);
   end;
   Result := PChar(S1);
   Free;
 end;

end;

exports
 rsReturnMD5 name 'rsReturnMD5';

begin

end.


-------------------  UNIT1.PAS ---------------------------
unit Unit1;

interface

uses
  Forms, StdCtrls, Classes, Controls, Dialogs;

type
 TForm1 = class(TForm)
   Edit1: TEdit;
   Button1: TButton;
   procedure Button1Click(Sender: TObject);
 private
   { Private declarations }
 public
   { Public declarations }
 end;

function rsReturnMD5(Value: PChar): PChar; stdcall;

var
 Form1: TForm1;

function rsReturnMD5(Value: PChar): PChar; external 'rsSecureFuncts.dll' name 'rsReturnMD5';

implementation

{$R *.dfm}

uses
IdHash, IdHashMessageDigest;

procedure TForm1.Button1Click(Sender: TObject);
begin
 ShowMessage(rsReturnMD5(PChar(edit1.Text)));
end;

end.
-------------------  END ---------------------------
0
 

Author Comment

by:rpter
ID: 9881949
thanks that worked... altough i have a similar dll that uses strings with sharemem and that seems to work fine!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Loops Section Overview
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question