This topic is now archived and is closed to further replies.


Passing bitmap to a dll for drawing

Recommended Posts

czar    122
Hello everyone I am stuck and I am not sure which way to go. I have the following challenge. I have a program (a dll) that wants to pass a bitmap to a second dll, this in return draws some information onto the bitmap and sends it back to the calling program that then puts it on the dxdraw component. But its not working. In my caling program I have the following ---------- procedure DrawBitmap(var UserBmap : Tbitmap); stdcall; external ''advdll.dll''; In the create I have created MyBitmap and loaded it with a default picture, and then in my dxtimer loop DrawBitmap(MyBitMap); dxdraw1.surface.canvas.Draw(10,10,MyBitMap); ---------- The DrawBitmap procedure is in the AdvDll.dll procedure DrawBitmap(var UserBmap : Tbitmap); begin UserBmap.lineTo(30,30); end; I get an access violation. I know its probably simple but I have no idea where to go to fix it. Help

Share this post

Link to post
Share on other sites
MButchers    123
Hello there,

for starters you dont need the var keyword so you can
declare your procedure as

procedure DrawBitmap(UserBmap : TBitmap);

this is due to the fact that it is an object which is
already a pointer.

It may also be a good idea to just pass the data as an
untyped pointer and typecast the pointer back to a bitmap
in the procedure call eg

procedure DrawBitmap(UserBmap : Pointer);
bmp: TBitmap;
Assert(Assigned(UserBMap),''Invalid pointer passed to DrawBitmap'');

bmp := TBitmap(UserBMap);

//draw code here


this is due to the fact that delphi requires additional
dlls to be distributed if a dll passes any objects that
contain strings

you will also need to use the stdcall keyword on both
the declaration of your procedures in the dll

procedure DrawBitmap(var UserBmap : Tbitmap);stdcall;

as this defines the order in which parameters are passed
to the call ( the calling convension )

Hope it helps


Share this post

Link to post
Share on other sites
czar    122
Thanks very much for your help. It is now working. I ran into a few clitches, when I typecast the bitmap it wasn't working properly. When the calling DLL was itself closed its calling program would hang and delphi reported that there were 'too many consqeuctive errors'. So to fix it I only passed the canvas and the problem was solved.

Just for records sake here is snippets of the final code in case someone else has a similar question.

procedure DrawBitmap(UserBmap : Pointer);stdcall; external 'advdll.dll';

procedure TForm1.DXTimer1Timer(Sender: TObject; LagCount: Integer);
if not dxdraw1.candraw then exit;

in the dll I put
procedure DrawBitmap(UserBmap : Pointer);stdcall;
var bmp: TCanvas;
Assert(Assigned(UserBMap),'Invalid pointer passed to DrawBitmap');
bmp := Tcanvas(UserBMap);

Thanks again. These forums are a great way to learn new stuff

[edited by - czar on March 20, 2002 6:18:57 PM]

Share this post

Link to post
Share on other sites