Board index » delphi » Very Small DLL (code works as exe but not as dll)

Very Small DLL (code works as exe but not as dll)

Help (very small DLL),

I have written (first time) a small DLL, all it does (or doesn't in this
case) is extract an icon form a file and save it to disk.

It works fine in run in a EXE (normal Delphi v2 program), but as soon as I
transfer the code and create a DLL, then call it from another app (in this
case VB5), it crashes.

Code example below

library Code5;
uses

ShellAPI, Graphics, ExtCtrls,  SysUtils,  Classes;

{$H-}

procedure ExtractIconToFile (FileFrom, FileTo: string; IconNo: integer);
stdcall;
var
   TmpImage:            TIcon;
   FileFromPchar: array[0..255] of Char;
begin
   StrPCopy(FileFromPchar, FileFrom);
   TmpImage := TIcon.Create;
       TmpImage.Handle := ExtractIcon (hinstance, FileFromPchar, IconNo);
       TmpImage.SaveToFile (FileTo);
   TmpImage.Destroy;
end;

exports
 ExtractIconToFile      index 1;

begin
end.

 

Re:Very Small DLL (code works as exe but not as dll)


In article <01bc6b51$3e31b7e0$32027...@Win95-DH.wsatkins.co.uk>,
  "David Hitchen" <DHitc...@WsAtkins.co.uk> wrote:

Quote

> Help (very small DLL),

> I have written (first time) a small DLL, all it does (or doesn't in this
> case) is extract an icon form a file and save it to disk.

> It works fine in run in a EXE (normal Delphi v2 program), but as soon as I
> transfer the code and create a DLL, then call it from another app (in this
> case VB5), it crashes.

        What happens when you try to call the dll function from a
Delphi program? Seems like something you should try in order to
determine where the problem is...

Quote
> Code example below

> library Code5;
> uses

> ShellAPI, Graphics, ExtCtrls,  SysUtils,  Classes;

> {$H-}

> procedure ExtractIconToFile (FileFrom, FileTo: string; IconNo: integer);
> stdcall;

      Oops: Those parameters are Delphi strings. VB doesn't know anything
about Delphi strings - if you're writing a dll meant to be used by
non-Delphi programs you should use nothing but PChars for your strings.

       Then get it working when you call it from a Delphi program,
to make certain you got the PChar stuff right, and _then_ start
figuring out how to call it from your VB program. (My VB is several
versions out of date - the last time I called a dll from VB the
thing to do was to Declare the parameter as a String, making
certain to OMIT the "ByVal" part.)

David Ullrich

-------------------==== Posted via Deja News ====-----------------------
      http://www.dejanews.com/     Search, Read, Post to Usenet

Re:Very Small DLL (code works as exe but not as dll)


On 28 May 1997 10:21:48 GMT, "David Hitchen" <DHitc...@WsAtkins.co.uk>
wrote:

Quote
>Help (very small DLL),

>I have written (first time) a small DLL, all it does (or doesn't in this
>case) is extract an icon form a file and save it to disk.

>It works fine in run in a EXE (normal Delphi v2 program), but as soon as I
>transfer the code and create a DLL, then call it from another app (in this
>case VB5), it crashes.

>Code example below

>library Code5;
>uses

>ShellAPI, Graphics, ExtCtrls,  SysUtils,  Classes;

>{$H-}

>procedure ExtractIconToFile (FileFrom, FileTo: string; IconNo: integer);

                              ^^^^^^^^^^^^^^^^^^^^^^^^
This is probably your problem, Delphi/Object Pascal strings are VERY
DIFFERENT from VB strings; try using PChars instead.

[deletia]

HTH

Stephen Posey
slpo...@concentric.net

Re:Very Small DLL (code works as exe but not as dll)


Quote
DaDavid Hitchen wrote:

> Help (very small DLL),

> I have written (first time) a small DLL, all it does (or doesn't in this
> case) is extract an icon form a file and save it to disk.

> It works fine in run in a EXE (normal Delphi v2 program), but as soon as I
> transfer the code and create a DLL, then call it from another app (in this
> case VB5), it crashes.

> Code example below

> library Code5;
> uses

> ShellAPI, Graphics, ExtCtrls,  SysUtils,  Classes;

> {$H-}

> procedure ExtractIconToFile (FileFrom, FileTo: string; IconNo: integer);
> stdcall;
> var
>    TmpImage:            TIcon;
>    FileFromPchar: array[0..255] of Char;
> begin
>    StrPCopy(FileFromPchar, FileFrom);
>    TmpImage := TIcon.Create;
>        TmpImage.Handle := ExtractIcon (hinstance, FileFromPchar, IconNo);
>        TmpImage.SaveToFile (FileTo);
>    TmpImage.Destroy;
> end;

> exports
>  ExtractIconToFile      index 1;

> begin
> end.

Dave,

You need to let the dll know that it is to export the procedure when you first declare the procedure. The first
line would read:
procedure ExtractIconToFile (FileFrom, FileTo: string; IconNo: integer); export;

Then, your dll will work properly.

Regards,
Dave Skowron

Other Threads