Solved

Dll Access Violation

Posted on 2003-12-04
2
573 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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now