Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Lazarus, FreePascal, Kylix, etc. (https://www.clubdelphi.com/foros/forumdisplay.php?f=14)
-   -   Al accesar un Bitmap en DLL se genera ACCESS VIOLATION (https://www.clubdelphi.com/foros/showthread.php?t=88684)

rmostalac 15-07-2015 22:54:00

Al accesar un Bitmap en DLL se genera ACCESS VIOLATION
 
Despues de ejecutar Bitmap := TBitmap.Create; verifico que Bitmap no sea NIL, pero al tratar de establecer el tamaño con Bitmap.Setsize(29,29); recibo el error ACCESS VIOLATION.

El mismo código en una Aplicación de escritorio funciona correctamente!

¿Alguien podrá darme un "empujoncito" para poder resolver el problema?

ElKurgan 16-07-2015 07:39:13

Pues hombre, sin ver nada más del código y con la información que proporcionas, es difícil poder ayudarte.

¿Intentas acceder a un Bitmap en una DLL? ¿Quieres devolver el Bitmap a un programa desde una DLL? ¿Le pasas el Bitmap a la DLL como parámetro?

Hay un dicho en este grupo que dice que hay que tomarse un tiempo para plantear una pregunta, porque contra mejor esté planteada, más fácil es de responder

Saludos

Casimiro Noteví 16-07-2015 09:48:42

Como bien dice ElKurgan, léete nuestra guía de estilo, gracias :)

rmostalac 16-07-2015 20:04:47

Código referido
 
El código de la DLL es el siguiente:

La instrucción en BOLD señala en donde ocurre el ACCESS VIOLATION. Si quito cualquier referncia al Bitmap no se genera error en la DLL. Este mismo código funciona perfectamente bien en una aplicación de escritorio.


Código Delphi [-]
library QRCodeLibDinPrj;
 
 
uses
  Windows, Classes, Graphics, QRCodeUnit
  { you can add units after this };
 
 
function GetQRCodeImagen(Text: Pchar; Codificacion, ZonaSegura: Integer): HBitmap; export; stdcall;
var
  QRCodeBitmap: TBitmap;
  QRCode: TRAMLQRCode;
  Row, Column: Integer;
begin
  QRCodeBitmap := TBitmap.Create;
  QRCode := TRAMLQRCode.Create;
  try
    QRCode.Data := Text;
    QRCode.Encoding := TQRCodeEncoding(Codificacion);
    QRCode.QuietZone := ZonaSegura;
 
    QRCodeBitmap.SetSize(QRCode.Rows, QRCode.Columns);
 
    for Row := 0 to QRCode.Rows - 1 do
    begin
      for Column := 0 to QRCode.Columns - 1 do
      begin
        if (QRCode.IsBlack[Row, Column]) then
        begin
          QRCodeBitmap.Canvas.Pixels[Column, Row] := clBlack;
        end else
        begin
          QRCodeBitmap.Canvas.Pixels[Column, Row] := clWhite;
        end;
      end;
    end;
    Result := Bitmap.ReleaseHandle;
  finally
    QRCode.Free;
    QRCodeBitmap.Free;
  end;
end;
 
 
exports GetQRCodeImagen;
 
 
begin
end.


El programa que consume la DLL es el siguiente:

Código Delphi [-]
unit qrcodedll;
 
 
{$mode objfpc}{$H+}
 
 
interface
 
 
uses
  Windows, Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls;
 
 
type
 
 
  { TQRCodeDLLForm }
 
 
  TQRCodeDLLForm = class(TForm)
    ComboBox1: TComboBox;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    PaintBox1: TPaintBox;
    procedure ComboBox1Change(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure Edit2Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure PaintBox1Paint(Sender: TObject);
  private
    { private declarations }
    QRCodeBitmap: TBitmap;
  public
    { public declarations }
    procedure Update; override;
  end;
 
 
 
 
var
  QRCodeDLLForm: TQRCodeDLLForm;
 
 
  function GetQRCodeImagen(Text: Pchar; Codificacion, ZonaSegura: Integer): HBitmap;
           stdcall; external 'QRCodeLibDinPrj.dll';
 
 
implementation
 
 
{$R *.lfm}
 
 
{ TQRCodeDLLForm }
 
 
procedure TQRCodeDLLForm.Edit1Change(Sender: TObject);
begin
  Update;
end;
 
 
procedure TQRCodeDLLForm.Edit2Change(Sender: TObject);
begin
  Update;
end;
 
 
procedure TQRCodeDLLForm.ComboBox1Change(Sender: TObject);
begin
  Update;
end;
 
 
procedure TQRCodeDLLForm.FormCreate(Sender: TObject);
begin
  QRCodeBitmap := TBitmap.Create;
  Update;
end;
 
 
procedure TQRCodeDLLForm.FormDestroy(Sender: TObject);
begin
  QRCodeBitmap.Free;
end;
 
 
procedure TQRCodeDLLForm.PaintBox1Paint(Sender: TObject);
var
  Scale: Double;
begin
  PaintBox1.Canvas.Brush.Color := clWhite;
  PaintBox1.Canvas.FillRect(Rect(0, 0, PaintBox1.Width, PaintBox1.Height));
  if ((QRCodeBitmap.Width > 0) and (QRCodeBitmap.Height > 0)) then
  begin
    if (PaintBox1.Width < PaintBox1.Height) then
    begin
      Scale := PaintBox1.Width / QRCodeBitmap.Width;
    end else
    begin
      Scale := PaintBox1.Height / QRCodeBitmap.Height;
    end;
    PaintBox1.Canvas.StretchDraw(Rect(0, 0, Trunc(Scale * QRCodeBitmap.Width), Trunc(Scale * QRCodeBitmap.Height)), QRCodeBitmap);
  end;
end;
 
 
procedure TQRCodeDLLForm.Update;
begin
  QRCodeBitmap.Handle := GetQRCodeImagen(PChar(Edit1.Text), ComboBox1.ItemIndex, StrToIntDef(Edit2.Text, 4));
  PaintBox1.Repaint;
end;
 
 
end.

Espero que alguien pueda darme luz de como resolver el problema!


La franja horaria es GMT +2. Ahora son las 08:44:35.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi