Solved

Dll Access Violation

Posted on 2003-12-04
2
581 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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