Solved

Dll Access Violation

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mobile Keyboard covers the display of the TMemo 3 88
Delphi XE10 Round Image 2 128
DBGrid or StringGrid ? 6 90
firemonkey Android Listview Sort items 7 15
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 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