Ver Mensaje Individual
  #3  
Antiguo 29-05-2024
[egostar] egostar is offline
Registrado
 
Registrado: feb 2006
Posts: 6.568
Reputación: 26
egostar Va camino a la fama
Cita:
Empezado por Matorral Ver Mensaje
Hola¡¡
.....
Desde Delphi 12, al introducir '1111' en el edit y luego al hacer click en el boton, muestra el resultado digito a digito, y en los digitos pares aparece un valor nulo en wClave1[i].

Desde Delphi 7 da el valor esperado.

No entiendo nada, se me escapa.


Gracias¡¡
Bueno, es que el detalle es que no estás considerando Unicode.

Crea la DLL en Delphi 7 así


Código Delphi [-]
library DLLCodifica;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }

uses
  SysUtils,
  Classes;

{$R *.res}

function Codifica(v: WideString): integer; stdcall;
var
  wClave1: string;
  i,j: longint;
begin
  j := 0;
  wclave1 := v;
  for i := 1 to Length(wClave1) do
  j := j * 255 + ord(wClave1[i]);
  j := j * 7;
  Result := j;
end;

exports
  Codifica;
  
begin
end.

Y con Delphi 11 consumela así


Código Delphi [-]
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  dllCodifica = function(v: WideString): integer; stdcall;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  Handle: THandle;
  procInt: dllCodifica;
  Regreso: integer;
begin
  Regreso := 0;
  Handle := LoadLibrary( PChar(ExtractFilePath(ParamStr(0)) + 'DLLCodifica.dll') );
  if Handle <> 0 then
  begin
    @procInt := GetProcAddress(Handle, PChar('Codifica'));
    if @procInt <> nil then
    try
      Regreso := procInt('1111');
      ShowMessage(Regreso.ToString);
    except
      on E : Exception do
      ShowMessage(E.ClassName+' error raised, with message : '+E.Message);
    end;
  end;
  FreeLibrary(Handle);
end;

end.

__________________
"La forma de empezar es dejar de hablar y empezar a hacerlo." - Walt Disney
Responder Con Cita