首页
社区
课程
招聘
[求助]VB.NET调用Delphi的DLL出错
发表于: 2009-3-10 02:25 6414

[求助]VB.NET调用Delphi的DLL出错

2009-3-10 02:25
6414
这个是传奇封包加密解密的DLL,Project1.Dll是自己编译的,调用“Encode”,另外一个是从其他工具中分离出来的。
这两个DLL不论是在VB.NET下还是VB6下都无法调用成功。
.NET提示错误:尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
VB6下直接是内存错误。。。然后就退出了。。
调用试过如下代码,但都失败:
    'Public Declare Function Encode Lib "E:\Project1.Dll" Alias "Encode" (ByRef Source As String) As String
    Public Declare Function Encode Lib "E:\Project1.Dll" (ByVal Source As String) As String

希望高手帮忙看下。。
附上Project1.DLL源代码:
library Project1;

{ 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 Encode(source : string):string;stdcall;
var
Source_Len,Len : integer;
Count,c : integer;
a1,a2 : byte;
ind : Word   ;
Encode_Str : string;
begin
Result := '';
Encode_Str := '';
Len := 0;
a1 := 0;
a2 := 0;
c := 0;
ind := 0;
Count := 0;
Source_Len := Length(source);
while Count < Source_Len do
begin
if Len >= $2710 then
break;
ind := ord(source[Count+1]);
ind := ind shr (c+2);
a1 := ind or a2;
a1 := a1 and $3f;
ind := ord(source[Count+1]);
ind := ind shl (8-(c+2));
ind := ind shr 2;
a2 := ind and $3f;
inc(c,2);
if c >= 6 then
begin
if Len >= $270f then
begin
Encode_Str := Encode_Str + chr(a1 + $3c);
inc(Len);
end
else
begin
Encode_Str := Encode_Str + chr(a1 + $3c);
Encode_Str := Encode_Str + chr(a2 + $3c);
Inc(Len,2);
end;
c := 0;
a2 := 0;
end
else
begin
Encode_Str := Encode_Str + chr(a1 + $3c);
Inc(Len);
end;
inc(Count);
end;
if c > 0 then
begin
Encode_Str := Encode_Str + chr(a2 + $3c);
Inc(Len);
end;
SetLength(Encode_Str,Len);
Result := Encode_Str;
end;
exports
    Encode;
begin
end.

[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)

上传的附件:
收藏
免费 0
支持
分享
最新回复 (1)
雪    币: 212
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
在使用 delphi 编写DLL 时  
函数的传入和传出(尤其是传出 )参数  一定要用PCHAR 而不要使用STRING
2009-3-10 06:44
0
游客
登录 | 注册 方可回帖
返回
//