#### Archived

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

# Passing bitmap to a dll for drawing

This topic is 5744 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 on other sites
Hello there,

for starters you dont need the var keyword so you can

procedure DrawBitmap(UserBmap : TBitmap);

this is due to the fact that it is an object which is

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);var  bmp: TBitmap;begin  Assert(Assigned(UserBMap),''Invalid pointer passed to DrawBitmap'');  bmp := TBitmap(UserBMap);  //draw code hereend;

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
eg

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

Mark

##### Share on other sites
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);
begin
if not dxdraw1.candraw then exit;
DrawBitmap(MyBitMap.canvas);
dxdraw1.surface.canvas.Draw(0,0,MyBitMap);
end;

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

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

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

• ### Forum Statistics

• Total Topics
628642
• Total Posts
2983998

• 9
• 9
• 10
• 21
• 20