C Code ---> Pascal Code 出现问题,迷惑ing!(200分)

  • 主题发起人 主题发起人 zxp_ping
  • 开始时间 开始时间
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;
 
EncodeBuffer(@tes[1], @test[1], 4 );

看看吧!
 
var
text1, text2: array [1..100] of Char;

EncodeBuffer(@text1, @Text2, 4);
按理应该是,一个输入,一个输出。。。

function CRCfile(FileName :PString; ...
to:
function CRCfile(FileName :PChar;

其它的好像没什么错。
 
To Adnil:
也不行。还是出错。
To copy_paste:
还是一样。
:(
各位富翁,还有没有别的主意啊?
 
和调用协议有关系吗,是不是
stdcall调用还是 cdecl
 
如果用Delphi默认的调用方式fastcall来调用stdcall的函数
当然会出错了
 
To LiChaoHui
他给我的调用申明我已经全部贴上来了。
那我应该如何调用呢?
 
你试过直接用C调用成功没?
将C调用的代码拿来看看
 
对方给的就只有我贴上去的内容,下面是我拷贝他的demo,还原出来的一个控制台程序,可以
正常运行。
代码:
#include "stdafx.h"
#include <stdio.h>

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);

int main(int argc, char* argv[])
{
	test();
	return 0;
}

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 );

}
 
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;

改成

type fnCipherInitialize = function ():Boolean;stdcall;
type fnCipherShutdown = function ():Boolean;stdcall;
type fnEncodeBuffer = function (const Source:Pointer; Dest:Pointer; DataSize:Integer; IsFrog:Boolean = TRUE ):Boolean;stdcall;
type fnDecodeBuffer = function (const Source:Pointer; Dest:Pointer; DataSize:Integer; IsFrog:Boolean = TRUE ):Boolean;stdcall;
type fnCRCbuffer = function (Data:Pointer; DataSize:DWORD):DWORD;stdcall;
type fnCRCfile = function (FileName:PChar; Offset:DWORD):DWORD;stdcall;

看看!
 
照 Adnil说的试一下吧,
可以查一下Delphi的帮助,输入关键字 fastcall 对Delphi支持的调用协议进行一下了解
 
To Adnil:
还是一样的出错。
To LiChaoHui:
谢谢。:)
 
将DLL发到copy_paste@163.com,我试试
 
您的信件已经成功发送到 copy_paste@163.com

先谢谢了!:)
 
type
fnCipherInitialize = function: Boolean;
fnCipherShutdown = function: Boolean;
fnEncodeBuffer = function(Src, Dst: Pointer; DataSize: Integer; IsFrog: Boolean): Boolean; stdcall;
fnDecodeBuffer = function(Src, Dst: Pointer; DataSize: Integer; IsFrog: Boolean): Boolean; stdcall;
fnCRCbuffer = function(Data:Pointer; DataSize: DWORD): DWORD;
fnCRCfile = function(FileName: PChar; Offset: DWORD): DWORD;

procedure Test;
var
Handle: THandle;
CipherInitialize: fnCipherInitialize;
CipherShutdown: fnCipherShutdown;
EncodeBuffer: fnEncodeBuffer;
Buffer: array [0..99] of Char;
begin
Handle := LoadLibrary('c:/temp/CiperDLL.dll');
if Handle <> 0 then
try
CipherInitialize := GetProcAddress(Handle, 'CipherInitialize');
CipherShutdown := GetProcAddress(Handle, 'CipherShutdown');
EncodeBuffer := GetProcAddress(Handle, 'EncodeBuffer');
FillChar(Buffer, SizeOf(Buffer), 0);
StrCopy(Buffer, '1234567890');
if Assigned(CipherInitialize) and CipherInitialize then
begin
EncodeBuffer(@Buffer, @Buffer, StrLen(Buffer), True);
if Assigned(CipherShutdown) then
CipherShutdown;
end;
finally
FreeLibrary(Handle);
end;
end;

调用正常了,就是在EncodeBuffer后面加一个stdcall,其它的函数没试,如果出错,你加个
stdcall试试就行了。
 
多人接受答案了。
 
后退
顶部