Passing bitmap to a dll for drawing

Started by
2 comments, last by czar 22 years, 1 month ago
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
Advertisement
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);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
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]
Glad it helped

This topic is closed to new replies.

Advertisement