Ver Mensaje Individual
  #4  
Antiguo 16-07-2015
rmostalac rmostalac is offline
Miembro
 
Registrado: jul 2004
Ubicación: Texcoco, México
Posts: 3
Reputación: 0
rmostalac Va por buen camino
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!
Responder Con Cita