Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2004.04.04;
Скачать: CL | DM;

Вниз

Ошибка при закрытии   Найти похожие ветки 

 
Chcnger   (2004-03-15 20:47) [0]

Почему после того как я из своей программы вызываю процедуру из этой ДЛЛ, то при закрытии моей программы происходит ошибка :,,

library Atwain;

uses   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
 ExtCtrls, StdCtrls, DelphiTwain;

type
TEvent = procedure(I: Integer) of object;

type
 TMyClass = class(TObject)
   private
      procedure TwainTwainAcquire(Sender: TObject; const Index: Integer;
                Image: TBitmap; var Cancel: Boolean);
       procedure TwainAcquireProgress(Sender: TObject;
                const Index: Integer; const Image: HBITMAP; const Current,
              Total: Integer);

 public   Twain: TDelphiTwain;
end;

var
  BITMAP:Tbitmap;
  TOT:integer;
procedure TMyClass.TwainAcquireProgress(Sender: TObject;
 const Index: Integer; const Image: HBITMAP; const Current,
 Total: Integer);
begin
       tot:=Current;
end;

procedure TMyClass.TwainTwainAcquire(Sender: TObject; const Index: Integer;
 Image: TBitmap; var Cancel: Boolean);
begin
//  ImageHolder.Picture.Assign(Image);
//  Cancel := TRUE;  {Only want one image}
 BITMAP.Assign(Image);
 BITMAP.SaveToFile("c:\lol.bmp");
 BitMap.Free;
end;
procedure SetColorType(Index:integer);
begin
{   Case Index of
  0:s.Twain.Source[SelectedSource].SetIPixelType(tbdBw);
  1:s.Twain.Source[SelectedSource].SetIPixelType(tbdGray);
  2:s.Twain.Source[SelectedSource].SetIPixelType(tbdRgb);
  3:s.Twain.Source[SelectedSource].SetIPixelType(tbdCmyk);
  4:s.Twain.Source[SelectedSource].SetIPixelType(tbdYuvk);
  end;}
end;

procedure SetSourceAndAcquire(UI:boolean;TransferMode,DPI,ImageWidth,ImageHeight,BitDepth,IPixelType:integer);stdcall;//(UI:boolea n;transfer:integer);stdcall;
var
  s:TMyClass;
  SelectedSource: Integer;
begin
  Bitmap := TBitmap.Create ;
  Bitmap.Width :=ImageWidth;
  Bitmap.Height :=ImageHeight;
  s.Twain:=TDelphiTwain.Create(Nil);
  s.Twain.OnTwainAcquire:=s.TwainTwainAcquire;
  s.Twain.OnAcquireProgress:=s.TwainAcquireProgress;
 {It is always recommended to load library dynamically, never forcing}
 {final user to have twain installed}
 if s.Twain.LoadLibrary then
 begin

   {Load source manager}
   s.Twain.SourceManagerLoaded := TRUE;
   {Allow user to select source}
   SelectedSource := s.Twain.SelectSource;
   if SelectedSource <> -1 then
   begin
     {Load source, select transference method and enable (display interface)}
     s.Twain.Source[SelectedSource].Loaded := TRUE;
      case IPixelType of
       0:s.Twain.Source[SelectedSource].SetIPixelType(tbdBw);
       1:s.Twain.Source[SelectedSource].SetIPixelType(tbdGray);
       2:s.Twain.Source[SelectedSource].SetIPixelType(tbdRgb);
       3:s.Twain.Source[SelectedSource].SetIPixelType(tbdCmyk);
       4:s.Twain.Source[SelectedSource].SetIPixelType(tbdYuvk);
      end;
      case TransferMode of
       0:s.Twain.Source[SelectedSource].TransferMode := ttmMemory;
       1:s.Twain.Source[SelectedSource].TransferMode := ttmFile;
       2:s.Twain.Source[SelectedSource].TransferMode := ttmNative;
      end;
     s.Twain.Source[SelectedSource].SetIYResolution(DPI);
     s.Twain.Source[SelectedSource].SetIYResolution(DPI);
     s.Twain.Source[SelectedSource].SetIBitDepth(BitDepth);
     s.Twain.Source[SelectedSource].TransferMode := ttmMemory;
     s.Twain.Source[SelectedSource].ShowUI:=UI;
     s.Twain.Source[SelectedSource].Enabled := TRUE;
   end {if SelectedSource <> -1}
 end
 else
  begin
    showmessage("Twain is not installed.");
   end;

 While Tot<100 do begin ShowmEssage(IntTostr(tot)) end;
 s.Free;
end;

exports SetSourceAndAcquire;
begin
end.


 
YuRock ©   (2004-03-15 20:52) [1]

Как минимум потому, что Вы не вычищаете память "из-под" объектов. Так, мельком глянув можно заметить:

1. Bitmap не удаляется;
2. s.Twain не удаляется;
3. s вообще не создается! должно валиться еще при первом обращении (s.Twain в четвертой строке ф-ции SetSourceAndAcquire)


 
Petr V. Abramov ©   (2004-03-15 20:57) [2]

Если "происходит ошибка" - это Runtime error 216/217, Access Violation, Invalid Pointer Operation и т.п. - либо Вы не освобождаете память ( не уничтожаете объекты), либо ошибки в деструкторах ( например, что-то уже удалено, возможно, автоматически, а Вы к этому чему-то обращаетесь). Короче, простое правило - все, что насоздавали/навыделяли ручками убить/освободить, желательно в порядке, обратном порядку создания. Если какие-то другие ошибки - обратитесь к разработчику.


 
Petr V. Abramov ©   (2004-03-15 20:59) [3]

Кстати - в 20:54 сообщение YuRock ©   (15.03.04 20:52) [1] у меня видно не было


 
YuRock ©   (2004-03-15 21:16) [4]

> Кстати - в 20:54 сообщение YuRock ©   (15.03.04 20:52) [1] у меня видно не было

А это я машину времени изобрел :))


 
Chcnger   (2004-03-15 21:32) [5]

Runtime error 216/217, Access Violation, Invalid Pointer Operation  да именно это ошибка.

2YuRock
Вродже создаю  ? :  s.Twain:=TDelphiTwain.Create(Nil);
 s.Twain.OnTwainAcquire:=s.TwainTwainAcquire;

И еще почему я не могу указать s:TMyClass; как глобальную переменную(постоянно возникают ошибки), а как локально все работает ?


 
YuRock ©   (2004-03-15 21:34) [6]

Перед s.Twain:=

надо, наверно, :)),

s := TMyClass.Create;


 
Chcnger   (2004-03-15 22:21) [7]

:))))))) Спасибо :)


 
Chcnger   (2004-03-15 22:28) [8]

Вот добавил процедуру. Перед закрытием формы ее вызываю(очищаю все объекты), но все равно происходит ошибка -
Access Violation, Invalid Pointer Operation

procedure FREEALL; stdcall;
begin
  s.Twain.Free;
  s.free;
  BitMap.Free;
end;


library Atwain;

uses   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
 ExtCtrls, StdCtrls, DelphiTwain;

type
TEvent = procedure(I: Integer) of object;

type
 TMyClass = class(TObject)
   private
      procedure TwainTwainAcquire(Sender: TObject; const Index: Integer;
                Image: TBitmap; var Cancel: Boolean);
       procedure TwainAcquireProgress(Sender: TObject;
                const Index: Integer; const Image: HBITMAP; const Current,
              Total: Integer);

 public   Twain: TDelphiTwain;
end;

var
  BITMAP:Tbitmap;
  TOT:integer;
  s:TMyClass;
 
procedure TMyClass.TwainAcquireProgress(Sender: TObject;
 const Index: Integer; const Image: HBITMAP; const Current,
 Total: Integer);
begin
       tot:=Current;
end;

procedure TMyClass.TwainTwainAcquire(Sender: TObject; const Index: Integer;
 Image: TBitmap; var Cancel: Boolean);
begin
//  ImageHolder.Picture.Assign(Image);
//  Cancel := TRUE;  {Only want one image}
 BITMAP.Assign(Image);
 BITMAP.SaveToFile("c:\lol.bmp");
end;
procedure SetColorType(Index:integer);
begin
{   Case Index of
  0:s.Twain.Source[SelectedSource].SetIPixelType(tbdBw);
  1:s.Twain.Source[SelectedSource].SetIPixelType(tbdGray);
  2:s.Twain.Source[SelectedSource].SetIPixelType(tbdRgb);
  3:s.Twain.Source[SelectedSource].SetIPixelType(tbdCmyk);
  4:s.Twain.Source[SelectedSource].SetIPixelType(tbdYuvk);
  end;}
end;

procedure SetSourceAndAcquire(UI:boolean;TransferMode,DPI,ImageWidth,ImageHeight,BitDepth,IPixelType:integer);stdcall;//(UI:boolea n;transfer:integer);stdcall;
var
  SelectedSource: Integer;
begin
  Bitmap := TBitmap.Create ;
  Bitmap.Width :=ImageWidth;
  Bitmap.Height :=ImageHeight;
  s := TMyClass.Create;
  s.Twain:=TDelphiTwain.Create(Nil);
  s.Twain.OnTwainAcquire:=s.TwainTwainAcquire;
  s.Twain.OnAcquireProgress:=s.TwainAcquireProgress;
 {It is always recommended to load library dynamically, never forcing}
 {final user to have twain installed}
 if s.Twain.LoadLibrary then
 begin

   {Load source manager}
   s.Twain.SourceManagerLoaded := TRUE;
   {Allow user to select source}
   SelectedSource := s.Twain.SelectSource;
   if SelectedSource <> -1 then
   begin
     {Load source, select transference method and enable (display interface)}
     s.Twain.Source[SelectedSource].Loaded := TRUE;
      case IPixelType of
       0:s.Twain.Source[SelectedSource].SetIPixelType(tbdBw);
       1:s.Twain.Source[SelectedSource].SetIPixelType(tbdGray);
       2:s.Twain.Source[SelectedSource].SetIPixelType(tbdRgb);
       3:s.Twain.Source[SelectedSource].SetIPixelType(tbdCmyk);
       4:s.Twain.Source[SelectedSource].SetIPixelType(tbdYuvk);
      end;
      case TransferMode of
       0:s.Twain.Source[SelectedSource].TransferMode := ttmMemory;
       1:s.Twain.Source[SelectedSource].TransferMode := ttmFile;
       2:s.Twain.Source[SelectedSource].TransferMode := ttmNative;
      end;
     s.Twain.Source[SelectedSource].SetIYResolution(DPI);
     s.Twain.Source[SelectedSource].SetIYResolution(DPI);
     s.Twain.Source[SelectedSource].SetIBitDepth(BitDepth);
     s.Twain.Source[SelectedSource].TransferMode := ttmMemory;
     s.Twain.Source[SelectedSource].ShowUI:=UI;
     s.Twain.Source[SelectedSource].Enabled := TRUE;
   end {if SelectedSource <> -1}
 end
 else
  begin
    showmessage("Twain is not installed.");
   end;
end;
exports SetSourceAndAcquire;

procedure FREEALL; stdcall;
begin
  s.Twain.Free;
  s.free;
  BitMap.Free;
end;

exports FreeAll;
begin
end.


 
YuRock ©   (2004-03-15 22:48) [9]

1. Зачем переменные Bitmap и s глобальные? Почему их не объявить локально и удалять прямо в ф-ции? Ведь такой подход может привести к тому, что этих объектов создастся несколько, а удалятся - в лучшем случае только 1 экземпляр.

И вообще, где конкретно возникает ошибка? В Delphi в Run - Parameters - HostApplication можно прописать exe-файл, который выполняет работу с dll, таким образом можно отлаживать библиотеку. Пройдись отладчиком.


 
Chcnger   (2004-03-16 21:51) [10]

Прямо в фу-ии нельзя, т.к. нужно, чтобы сначала выпрлнилось событие OnTwainAcquire, поэтому и переменные глобальные. !!


 
Chcnger   (2004-03-17 19:41) [11]

.



Страницы: 1 вся ветка

Текущий архив: 2004.04.04;
Скачать: CL | DM;

Наверх




Память: 0.51 MB
Время: 0.04 c
3-1078209099
Evyshka
2004-03-02 09:31
2004.04.04
Обращение через алиас и путь


4-1075355382
Alexander666
2004-01-29 08:49
2004.04.04
Замена процедуры окна через хук


3-1078331942
Rauf
2004-03-03 19:39
2004.04.04
Проблемы с TADOTable


7-1074151506
alex31
2004-01-15 10:25
2004.04.04
алгоритм поиска файлов в файловой системе FAT32


14-1078910092
Drunya
2004-03-10 12:14
2004.04.04
Как послать мыло с файлом на WinAPi