Z
zxp_ping
Unregistered / Unconfirmed
GUEST, unregistred user!
代码:
下面是一段Dll的调用接口声明,C的:
typedef BOOL (*fnCipherInitialize)( void );
typedef BOOL (*fnCipherShutdown)( void );
typedef BOOL (*fnEncodeBuffer)( const PVOID Source, PVOID Dest, INT DataSize,BOOL IsFrog = TRUE );
typedef BOOL (*fnDecodeBuffer)( const PVOID Source, PVOID Dest, INT DataSize,BOOL IsFrog = TRUE );
typedef DWORD (*fnCRCbuffer)( PVOID Data, DWORD DataSize );
typedef DWORD (*fnCRCfile)( char *FileName, DWORD Offset );
fnCipherInitialize CipherInitialize = NULL;
fnCipherShutdown CipherShutdown = NULL;
fnEncodeBuffer EncodeBuffer = NULL;
fnDecodeBuffer DecodeBuffer = NULL;
fnCRCbuffer CRCbuffer = NULL;
fnCRCfile CRCfile = NULL;
void test(void)
{
HINSTANCE hInstance = LoadLibrary( "CiperDLL.dll" );
if ( hInstance )
{
CipherInitialize = (fnCipherInitialize) GetProcAddress( hInstance, "CipherInitialize" );
CipherShutdown = (fnCipherShutdown) GetProcAddress( hInstance, "CipherShutdown" );
EncodeBuffer = (fnEncodeBuffer) GetProcAddress( hInstance, "EncodeBuffer" );
if ( CipherInitialize )
{
char test[100];
memset( &test, 0, sizeof(test) );
strcpy( test, "1234567890" );
// 首次使用必须调用
CipherInitialize();
EncodeBuffer( &test, &test, strlen( test ) );
// 结束使用
CipherShutdown();
}
}
if ( hInstance )
FreeLibrary( hInstance );
}
我改后的Pascal申明:
1.动态调用:
function EnBuffer(pSource,pDest:Pointer;DataSize :Integer; IsFrog :Bool=true):Bool;
type fnCipherInitialize = function ():Boolean;
type fnCipherShutdown = function ():Boolean;
type fnEncodeBuffer = function (const Source:Pointer; Dest:Pointer; DataSize:Integer; IsFrog:Boolean = TRUE ):Boolean;
type fnDecodeBuffer = function (const Source:Pointer; Dest:Pointer; DataSize:Integer; IsFrog:Boolean = TRUE ):Boolean;
type fnCRCbuffer = function (Data:Pointer; DataSize:DWORD):DWORD;
type fnCRCfile = function (FileName:PChar; Offset:DWORD):DWORD;
var
CipherInitialize :fnCipherInitialize;
CipherShutdown :fnCipherShutdown;
EncodeBuffer :fnEncodeBuffer;
DecodeBuffer :fnDecodeBuffer;
CRCbuffer :fnCRCbuffer;
CRCfile :fnCRCfile;
implementation
function EnBuffer(pSource,pDest:Pointer;DataSize :Integer; IsFrog :Bool=true):Bool;
var
hm : HMODULE;
begin
result := false;
hm := LoadLibrary('CiperDLL.dll');
if hm <> NULL then
begin
CipherInitialize := GetProcAddress(hm, 'CipherInitialize');
CipherShutdown := GetProcAddress(hm, 'CipherShutdown');
EncodeBuffer := GetProcAddress(hm, 'EncodeBuffer');
if CipherInitialize then
begin
// 首次使用必须调用
CipherInitialize();
try
EncodeBuffer( pSource, pDest, DataSize, IsFrog );
except
CipherShutdown();
FreeLibrary(hm);
exit;
end;
// 结束使用
CipherShutdown();
end;
FreeLibrary(hm);
result := true;
end;
end;
或是静态申明:
function CipherInitialize:Bool;external 'CiperDLL.dll' name 'CipherInitialize';
function CipherShutdown:Bool;external 'CiperDLL.dll' name 'CipherShutdown';
function EncodeBuffer(const Source: Pointer; Dest :Pointer; DataSize :Integer; IsFrog :Bool = true):Bool;external 'CiperDLL.dll' name 'EncodeBuffer';
function DecodeBuffer(const Source: Pointer; Dest :Pointer; DataSize :Integer; IsFrog :Bool = true):Bool;external 'CiperDLL.dll' name 'DecodeBuffer';
function CRCbuffer(Data :Pointer; DataSize :DWORD):DWORD;external 'CiperDLL.dll' name 'CRCbuffer';
function CRCfile(FileName :PString; Offset :DWORD):DWORD;external 'CiperDLL.dll' name 'CRCfile';
//开始调用
procedure loadtest;
var
test: array [1..100] of char;
begin
for i:= 1 to 100 do
test[i] := #0;
test[1] := 'a';
test[2] := 'b';
test[3] := 'c';
test[4] := 'd';
CipherInitialize();
EncodeBuffer(@test, @test, 4 );
//或是:encodeBuffer((@test,(@test,4);
CipherShutdown();
//都是执行到Encode时非法提示(....Read Address 'FFFFFF'.....).烦。
还望哪位富翁能指点迷津。
end;