我传递一个 HBitmap 到 Dll 函数中,
为了接收它,我定义了一个 TBitmap 变量,但是在释放该变量的时候出现了一个问题函数如下:function PicView(hBmp: HBitMap):longint;stdcall;
var
  PicViewFrm:TPicViewFrm;
  bmp:TBitmap;
begin
  PicViewFrm:=TPicViewFrm.Create(Application);
  bmp:=TBitmap.Create;
  try
    bmp.Handle:=hBmp;
    PicViewFrm.SetPic(bmp);
    PicViewFrm.ShowModal;
  finally
    bmp.Dormant;
    bmp.FreeImage;
    bmp.ReleaseHandle;
    bmp.Free;
    PicViewFrm.Free;
  end;
  result:=0;
end;一旦执行了 bmp.Free 后,原来资源中的那个 Bitmap 也被释放掉了,这不是我希望的啊
可是如果不释放这个变量,会不会存在内存泄漏呢?

解决方案 »

  1.   

    其实不加bmp.Dormant;
    bmp.FreeImage;
    bmp.ReleaseHandle;也一样的
      

  2.   

    用ASSIGN吧 相当于
    执行一个COPY,function PicView(hBmp: TBitMap):longint;stdcall;
    var
      PicViewFrm:TPicViewFrm;
      bmp:TBitmap;
    begin
      PicViewFrm:=TPicViewFrm.Create(Application);
      bmp:=TBitmap.Create;
      try
        hBmp.assign(bmp);
        PicViewFrm.SetPic(bmp);
        PicViewFrm.ShowModal;
      finally
      

  3.   

    to nyf1220不能传递 TBitmap 对象到 Dll 中的
      

  4.   

    我试了一下,好像没问题啊:
    library Project1;uses
      SysUtils, Windows, Classes, Graphics;{$R *.res}
    function PicView(hBmp: HBitMap):longint;stdcall;
    var
      bmp:TBitmap;
    begin
      bmp:=TBitmap.Create;
      try
        bmp.Handle:=hBmp;
      finally
        bmp.Free;
      end;
      result:=0;
    end;exports PicView;begin
    end.//调用
    function PicView(hBmp: HBitMap): longint;stdcall; external 'project1.dll';procedure TForm1.Button1Click(Sender: TObject);
    var
      h: hbitmap;
    begin
      h := image1.Picture.Bitmap.Handle;
      picview(h);
    end;
      

  5.   

    不过又用资源文件试了一下,果然被释放了,晕
    可以在传入dll之前用一个临时TBitmap把资源文件里的bmp拷贝下来啊
      

  6.   

    searoom(海龙) ( ) 
    为什么不能传入对象??
      

  7.   

    to ny1220不知道,提示 Cann't Assign TBitmap To TBitmap当然在一般的程序中没什么问题的,但当传 TBitmap 对象给 Dll 的函数时,无法使用 Assign 的方法to zzlingaaa是啊,一释放的时候就把原来的也释放了
    所以我不敢用 Free 释放,可是这样用的话又感觉会出现内存的问题哪位兄弟知道对 Handle 赋值的时候是什么过程?
    我看了他的代码,好像和 Assign 的过程差别很大
      

  8.   

    Handle似乎和指针一样的东西,不清楚
      

  9.   

    啊,?真的 ?
    传递到DLL中  ASSIGN也变了?
    还真不清楚了哦,可以给我代码吗,,就这部分的,我帮忙调试下,如有需要联系我qq 89269709
      

  10.   

    to nyf1220呵呵,我最近在将我以前用过的一些函数封装在 Dll 中,为了方便写程序
    也可以省的每次为了写程序都必须把相应的 vcl 装好,麻烦的要死你可以到 Searoom.ys168.com 下载看看
      

  11.   

    你给我的那个没有DLL代码啊,我想改里面的试下