于dll
以下是我上找的一dll改了一然后用但是出大家我看一下在那里在特急呀!
library demo;
uses
ShareMem,
SysUtils,
WinTypes,
WinProcs,
Classes;
var
SaveExit:Pointer;
procedure LibExit; far;
begin
ExitProc:=SaveExit; { 恢复原的退出程指 }
end;
function Decrypt(S:string;Key1,Key2:Word):string; StdCall;
var
I,j:Integer;
TmpStr : string;
begin
Result := ' ';
for I:=1 to (length(S) div 2) do
begin
j:=(Integer(S[2*i-1])-65)*26;
j:=j+(Integer(S[2*i])-65);
Result := Result + Char(j);
end;
TmpStr := Result;
for I:=1 to Length(TmpStr) do
begin
Result[I] := Char(Byte(TmpStr[I]) xor (Key1 shr 8));
Key1 := (Byte(TmpStr[I]) + Key1) * Key1 + Key2;
end;
end;
exports
Decrypt;
begin
{DLL的初始化工作 }
SaveExit := ExitProc; { 保存原的退出程指 }
ExitProc := @LibExit; { 安新的退出程 }
end.
然后我了一工程用
unit Unit1;
interface
uses
ShareMem,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
function Decrypt(S:string;Key1,Key2:Word):string; StdCall;EXTERNAL 'demo.dll ';
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
edit2.Text :=Decrypt(edit1.Text,197,123);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
action:=cafree;
end;
end.
行的候出以下提示
invalid pointer operation
然后是runtime error 217 at 00418c20
我了很一段都不行大大忙吧
in
[解决办法]
看来楼主是用来加密 中文字符吧,
在Delphi的dll中,传递,返回String,不是不可以,是没有问题的。
但用在加密运算中,不建议使用临时String变量,改用为Char数组,或者动态数组吧
下边偶改了一下,试试看
function Decrypt(S: string; Key1, Key2: Word): string; stdcall;
var
I, j: Integer;
//TmpStr: string;
TmpStr: array of Byte;//添加动态数组
begin
Result := ' ';
for I := 1 to (length(S) div 2) do
begin
j := (Integer(S[2 * i - 1]) - 65) * 26;
j := j + (Integer(S[2 * i]) - 65);
Result := Result + Char(j);
end;
//TmpStr := Result;
SetLength(TmpStr, Length(Result) + 1);//为动态数组分配内在
StrPCopy(@tmpstr[0], Result);//赋值
for I := 1 to Length(Result) do
begin
Result[I] := Char(Byte(TmpStr[I]) xor (Key1 shr 8));
Key1 := (Byte(TmpStr[I]) + Key1) * Key1 + Key2;
end;
SetLength(TmpStr, 0);//置空动态数组
end;
[解决办法]
在 DLL 的调出函数中使用了 String 要加上 borlndmm.dll
你新建一个DLL 时,Delphi 会有提示的,你没注意看
{ 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. }