首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

關于寫dll解决思路

2012-03-13 
關于寫dll以下是我從網上找的一個dll﹐改了一點﹐然后調用﹐但是出現錯誤﹐請大家幫我看一下﹐問題在那里﹐現在

關于寫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. }

热点排行