Solved

Dll Access Violation

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 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

22 Experts available now in Live!

Get 1:1 Help Now