2016-09-14 12 views
-2

ftrScanAPI.dllを正しく使用する方法に問題があります。 DLL内で関数 "ftrScanOpenDevice"を呼び出すときにエラーは発生しませんでしたが、プロシージャ "ftrScanCloseDevice"を呼び出すときにエラー "アクセス違反"が発生します。私は立ち往生して、私が正しい道にいるかどうかわからない。私はそれを正しくしていますか?DelphiでftrScanAPI.dllを使用する方法

TProcedure = procedure; stdcall; 
TProcedure_int = procedure(A_Int : Integer); stdcall; 


    TFutronicFingerPrint = class 
    fHandle : THandle; 
    public 
     constructor Create; 
     destructor Destroy; override; 
     procedure Open; 
     procedure Close; 
    end; 

constructor TFutronicFingerPrint.Create; 
begin 
    inherited; 
    fHandle := LoadLibrary('ftrScanAPI.dll'); 
end; 

destructor TFutronicFingerPrint.Destroy; 
begin 
    FreeLibrary(fHandle); 
    inherited; 
end; 

procedure TFutronicFingerPrint.Open; 
var 
    procOpenDevice : TProcedure; 
begin 

    @procOpenDevice := GetProcAddress(fHandle, 'ftrScanOpenDevice'); 
    if @procOpenDevice <> nil then 
    begin 
     procOpenDevice; 
    end; 
end; 

procedure TFutronicFingerPrint.Close; 
var 
    proc : TProcedure_int; 
begin 

    @proc := GetProcAddress(fHandle, 'ftrScanCloseDevice'); 
    if @proc <> nil then 
    begin 
     proc(fHandle); 
    end; 
end; 
+0

私たちがDLLを追跡し、そのドキュメントを調べて関数の宣言を探し、パスカルラッパーを書いてコードが動作しない理由をテストできると期待していますか? –

+1

私は精神的なデバッグスキルを使用します:私は 'ftpScanOpenDevice'への呼び出しが実際にオープンしたデバイスの整数ハンドルを返す関数であり、あなたが渡すことになっていると確信しています'ftrScanCloseDevice'呼び出しを処理します。 DLLハンドルをプロシージャに渡すのは意味がありません。 –

+0

@KenWhite:それは**まさに**それが何をしているかです。 'ftrScanOpenDevice()'は出力として 'FTRHANDLE'を返し、' ftrScanCloseDevice() 'はそれを入力として取ります。 –

答えて

3

問題は、DLL関数の宣言と使用が間違っていることです。

ftpScanOpenevice()は手続きではなく、出力としてFTRHANDLEを返す関数です。

ftrScanCloseDevice()は、その入力としてFTRHANDLENOTロードDLL自体を指しTHandleをとります。

代わりにこれを試してみてください。また

type 
    FTRHANDLE = Pointer; 
    ftrScanOpenDeviceFunc = function: FTRHANDLE; stdcall; 
    ftrScanCloseDeviceFunc = procedure(ftrHandle: FTRHANDLE); stdcall; 
    ... 

    TFutronicFingerPrint = class 
    private 
    fHandle : THandle; 
    fDevice : FTRHANDLE; 
    procOpenDevice : ftrScanOpenDeviceFunc; 
    procCloseDevice : ftrScanCloseDeviceFunc; 
    public 
    constructor Create; 
    destructor Destroy; override; 
    function Open: Boolean; 
    procedure Close; 
    end; 

constructor TFutronicFingerPrint.Create; 
begin 
    inherited; 

    fHandle := LoadLibrary('ftrScanAPI.dll'); 
    if fHandle = 0 then RaiseLastOSError; 

    @procOpenDevice := GetProcAddress(fHandle, 'ftrScanOpenDevice'); 
    if not Assigned(procOpenDevice) then RaiseLastOSError; 

    @procCloseDevice := GetProcAddress(fHandle, 'ftrScanCloseDevice'); 
    if not Assigned(procCloseDevice) then RaiseLastOSError; 
end; 

destructor TFutronicFingerPrint.Destroy; 
begin 
    Close; 
    if fHandle <> 0 then 
    FreeLibrary(fHandle); 
    inherited; 
end; 

function TFutronicFingerPrint.Open: Boolean; 
begin 
    Close; 
    fDevice := procOpenDevice(); 
    Result := fDevice <> nil; 
end; 

procedure TFutronicFingerPrint.Close; 
begin 
    if fDevice <> nil then 
    begin 
    procCloseDevice(fDevice); 
    fDevice = nil; 
    end; 
end; 

type 
    FTRHANDLE = Pointer; 

    TFutronicFingerPrint = class 
    private 
    fDevice : FTRHANDLE; 
    public 
    constructor Create; 
    destructor Destroy; override; 
    function Open: Boolean; 
    procedure Close; 
    end; 

... 

function ftrScanOpenDevice: FTRHANDLE; stdcall; external 'ftrScanAPI.dll'; 
procedure ftrScanCloseDevice(ftrHandle: FTRHANDLE); stdcall; external 'ftrScanAPI.dll'; 
... 

constructor TFutronicFingerPrint.Create; 
begin 
    inherited; 
end; 

destructor TFutronicFingerPrint.Destroy; 
begin 
    Close; 
    inherited; 
end; 

function TFutronicFingerPrint.Open: Boolean; 
begin 
    Close; 
    fDevice := ftrScanOpenDevice(); 
    Result := fDevice <> nil; 
end; 

procedure TFutronicFingerPrint.Close; 
begin 
    if fDevice <> nil then 
    begin 
    ftrScanCloseDevice(fDevice); 
    fDevice = nil; 
    end; 
end; 

あなたがftrScanAPI.hのコピーを取得した場合には、それはデルファイに変換することができ、実際のC宣言が含まれ、例えば:

unit ftrScanAPI; 

interface 

uses 
    Windows; 

type 
    FTRHANDLE = Pointer; 

const 
    FTR_MAX_INTERFACE_NUMBER = 128; 

    FTR_OPTIONS_CHECK_FAKE_REPLICA = $00000001; 
    FTR_OPTIONS_FAST_FINGER_DETECT_METHOD = $00000002; 

    FTR_ERROR_BASE = $20000000; 

    FTR_ERROR_EMPTY_FRAME = ERROR_EMPTY; 
    FTR_ERROR_MOVABLE_FINGER = FTR_ERROR_BASE or $0001; 
    FTR_ERROR_NO_FRAME = FTR_ERROR_BASE or $0002; 
    FTR_ERROR_USER_CANCELED = FTR_ERROR_BASE or $0003; 
    FTR_ERROR_HARDWARE_INCOMPATIBLE = FTR_ERROR_BASE or $0004; 
    FTR_ERROR_FIRMWARE_INCOMPATIBLE = FTR_ERROR_BASE or $0005; 
    FTR_ERROR_INVALID_AUTHORIZATION_CODE = FTR_ERROR_BASE or $0006; 

    FTR_CONST_COLOR_SMALL_IMAGE_WIDTH = 256; 
    FTR_CONST_COLOR_SMALL_IMAGE_HEIGHT = 150; 
    FTR_CONST_COLOR_SMALL_IMAGE_SIZE = FTR_CONST_COLOR_SMALL_IMAGE_WIDTH * FTR_CONST_COLOR_SMALL_IMAGE_HEIGHT; 

    FTR_CONST_CALIBRATION_SKIP_IR = $00000001; 
    FTR_CONST_CALIBRATION_SKIP_FUZZY = $00000002; 

    FTR_CONST_DIODE_OFF: Byte = 0; 
    FTR_CONST_DIODE_ON: Byte = 255; 

    _FTRSCAN_IMAGE_SIZE = packed record 
    nWidth: Integer; 
    nHeight: Integer; 
    nImageSize: Integer; 
    end; 
    FTRSCAN_IMAGE_SIZE = _FTRSCAN_IMAGE_SIZE; 
    PFTRSCAN_IMAGE_SIZE = ^_FTRSCAN_IMAGE_SIZE; 

    _FTRSCAN_FAKE_REPLICA_PARAMETERS = packed record 
    bCalculated: BOOL; 
    nCalculatedSum1: Integer; 
    nCalculatedSumFuzzy: Integer; 
    nCalculatedSumEmpty: Integer; 
    nCalculatedSum2: Integer; 
    dblCalculatedTremor: Double; 
    dblCalculatedValue: Double; 
    end; 
    FTRSCAN_FAKE_REPLICA_PARAMETERS = _FTRSCAN_FAKE_REPLICA_PARAMETERS; 
    PFTRSCAN_FAKE_REPLICA_PARAMETERS = ^_FTRSCAN_FAKE_REPLICA_PARAMETERS; 

    _FTRSCAN_FRAME_PARAMETERS = packed record 
    nContrastOnDose2: Integer; 
    nContrastOnDose4: Integer; 
    nDose: Integer; 
    nBrightnessOnDose1: Integer; 
    nBrightnessOnDose2: Integer; 
    nBrightnessOnDose3: Integer; 
    nBrightnessOnDose4: Integer; 
    FakeReplicaParams: FTRSCAN_FAKE_REPLICA_PARAMETERS; 
    Reserved: array[0..64-SizeOf(FTRSCAN_FAKE_REPLICA_PARAMETERS)-1] of Byte; 
    end; 
    FTRSCAN_FRAME_PARAMETERS = _FTRSCAN_FRAME_PARAMETERS; 
    PFTRSCAN_FRAME_PARAMETERS = ^_FTRSCAN_FRAME_PARAMETERS; 

    __FTRSCAN_INTERFACE_STATUS = (
    FTRSCAN_INTERFACE_STATUS_CONNECTED, 
    FTRSCAN_INTERFACE_STATUS_DISCONNECTED 
); 
    FTRSCAN_INTERFACE_STATUS = __FTRSCAN_INTERFACE_STATUS; 
    PFTRSCAN_INTERFACE_STATUS = ^__FTRSCAN_INTERFACE_STATUS; 

    __FTRSCAN_INTERFACES_LIST = packed record 
    InterfaceStatus: array[0..FTR_MAX_INTERFACE_NUMBER-1] of FTRSCAN_INTERFACE_STATUS; 
    end; 
    FTRSCAN_INTERFACES_LIST = __FTRSCAN_INTERFACES_LIST; 
    PFTRSCAN_INTERFACES_LIST = ^__FTRSCAN_INTERFACES_LIST; 

function ftrScanOpenDevice: FTRHANDLE; stdcall; 
function ftrScanOpenDeviceOnInterface(nInterface: Integer): FTRHANDLE; stdcall; 
procedure ftrScanCloseDevice(ftrHandle: FTRHANDLE); stdcall; 
function ftrScanSetOptions(ftrHandle: FTRHANDLE; dwMask, dwFlags: DWORD): BOOL; stdcall; 
function ftrScanGetOptions(ftrHandle: FTRHANDLE; lpdwFlags: LPDWORD): BOOL; stdcall; 

function ftrScanGetInterfaces(pInterfaceList: PFTRSCAN_INTERFACES_LIST): BOOL; stdcall; 
function ftrSetBaseInterface(nBaseInterface: Integer): BOOL; stdcall; 
function ftrGetBaseInterfaceNumber: Integer; stdcall; 

function ftrScanGetFakeReplicaInterval(pdblMinFakeReplicaValue, pdblMaxFakeReplicaValue: PDouble): BOOL; stdcall; 
procedure ftrScanSetFakeReplicaInterval(dblMinFakeReplicaValue, dblMaxFakeReplicaValue: Double); stdcall; 

function ftrScanGetImageSize(ftrHandle: FTRHANDLE; pImageSize: PFTRSCAN_IMAGE_SIZE): BOOL; stdcall; 
function ftrScanGetImage(ftrHandle: FTRHANDLE; nDose: Integer; pBuffer: Pointer): BOOL; stdcall; 
function ftrScanGetFuzzyImage(ftrHandle: FTRHANDLE; pBuffer: Pointer): BOOL; stdcall; 
function ftrScanGetBacklightImage(ftrHandle: FTRHANDLE; pBuffer: Pointer): BOOL; stdcall; 
function ftrScanGetDarkImage(ftrHandle: FTRHANDLE; pBuffer: Pointer): BOOL; stdcall; 
function ftrScanGetColourImage(ftrHandle: FTRHANDLE; pDoubleSizeBuffer: Pointer): BOOL; stdcall; 
function ftrScanGetSmallColourImage(ftrHandle: FTRHANDLE; pSmallBuffer: Pointer): BOOL; stdcall; 
function ftrScanGetColorDarkImage(ftrHandle: FTRHANDLE; pDoubleSizeBuffer: Pointer): BOOL; stdcall; 

function ftrScanIsFingerPresent(ftrHandle: FTRHANDLE; pFrameParameters: PFTRSCAN_FRAME_PARAMETERS): BOOL; stdcall; 
function ftrScanGetFrame(ftrHandle: FTRHANDLE; pBuffer: Pointer; pFrameParameters: PFTRSCAN_FRAME_PARAMETERS): BOOL; stdcall; 

function ftrScanSave7Bytes(ftrHandle: FTRHANDLE; pBuffer: Pointer): BOOL; stdcall; 
function ftrScanRestore7Bytes(ftrHandle: FTRHANDLE; pBuffer: Pointer): BOOL; stdcall; 

type 
    PFTRCALIBRATEFNCB = function(pContext, pParams: Pointer): BOOL; cdecl; 

function ftrScanZeroCalibration(pfnCallbackProc: PFTRCALIBRATEFNCB; pContext: Pointer): BOOL; stdcall; 
function ftrScanZeroCalibration2(dwOptions: DWORD; pfnCallbackProc: PFTRCALIBRATEFNCB; pContext: Pointer): BOOL; stdcall; 
function ftrScanGetCalibrationConstants(ftrHandle: FTRHANDLE; pbyIRConst, pbyFuzzyConst: PByte): BOOL; stdcall; 
function ftrScanStoreCalibrationConstants(ftrHandle: FTRHANDLE; byIRConst, byFuzzyConst: Byte; bBurnInFlash: BOOL): BOOL; stdcall; 
function ftrScanGetFakeReplicaParameters(ftrHandle: FTRHANDLE; pFakeReplicaParams: PFTRSCAN_FAKE_REPLICA_PARAMETERS): BOOL; stdcall; 

function ftrScanSetNewAuthorizationCode(ftrHandle: FTRHANDLE; pSevenBytesAuthorizationCode: Pointer): BOOL; stdcall; 
function ftrScanSaveSecret7Bytes(ftrHandle: FTRHANDLE; pSevenBytesAuthorizationCode, pBuffer: Pointer): BOOL; stdcall; 
function ftrScanRestoreSecret7Bytes(ftrHandle: FTRHANDLE; pSevenBytesAuthorizationCode, pBuffer: Pointer): BOOL; stdcall; 

function ftrScanSetDiodesStatus(ftrHandle: FTRHANDLE; byGreenDiodeStatus, byRedDiodeStatus: Byte): BOOL; stdcall; 
function ftrScanGetDiodesStatus(ftrHandle: FTRHANDLE; pbIsGreenDiodeOn, pbIsRedDiodeOn: PBOOL): BOOL; stdcall; 

implementation 

const 
    ftrScanAPI = 'ftrScanAPI.dll'; 

function ftrScanOpenDevice; external ftrScanAPI; 

function ftrScanOpenDeviceOnInterface; external ftrScanAPI; 
procedure ftrScanCloseDevice; external ftrScanAPI; 
function ftrScanSetOptions; external ftrScanAPI; 
function ftrScanGetOptions; external ftrScanAPI; 

function ftrScanGetInterfaces; external ftrScanAPI; 
function ftrSetBaseInterface; external ftrScanAPI; 
function ftrGetBaseInterfaceNumber; external ftrScanAPI; 

function ftrScanGetFakeReplicaInterval; external ftrScanAPI; 
procedure ftrScanSetFakeReplicaInterval; external ftrScanAPI; 

function ftrScanGetImageSize; external ftrScanAPI; 
function ftrScanGetImage; external ftrScanAPI; 
function ftrScanGetFuzzyImage; external ftrScanAPI; 
function ftrScanGetBacklightImage; external ftrScanAPI; 
function ftrScanGetDarkImage; external ftrScanAPI; 
function ftrScanGetColourImage; external ftrScanAPI; 
function ftrScanGetSmallColourImage; external ftrScanAPI; 
function ftrScanGetColorDarkImage; external ftrScanAPI; 

function ftrScanIsFingerPresent; external ftrScanAPI; 
function ftrScanGetFrame; external ftrScanAPI; 

function ftrScanSave7Bytes; external ftrScanAPI; 
function ftrScanRestore7Bytes; external ftrScanAPI; 

function ftrScanZeroCalibration; external ftrScanAPI; 
function ftrScanZeroCalibration2; external ftrScanAPI; 
function ftrScanGetCalibrationConstants; external ftrScanAPI; 
function ftrScanStoreCalibrationConstants; external ftrScanAPI; 
function ftrScanGetFakeReplicaParameters; external ftrScanAPI; 

function ftrScanSetNewAuthorizationCode; external ftrScanAPI; 
function ftrScanSaveSecret7Bytes; external ftrScanAPI; 
function ftrScanRestoreSecret7Bytes; external ftrScanAPI; 

function ftrScanSetDiodesStatus; external ftrScanAPI; 
function ftrScanGetDiodesStatus; external ftrScanAPI; 

end. 

uses 
    ..., ftrScanAPI; 

    TFutronicFingerPrint = class 
    private 
    fDevice : FTRHANDLE; 
    public 
    constructor Create; 
    destructor Destroy; override; 
    function Open: Boolean; 
    procedure Close; 
    end; 

... 

constructor TFutronicFingerPrint.Create; 
begin 
    inherited; 
end; 

destructor TFutronicFingerPrint.Destroy; 
begin 
    Close; 
    inherited; 
end; 

function TFutronicFingerPrint.Open: Boolean; 
begin 
    Close; 
    fDevice := ftrScanOpenDevice(); 
    Result := fDevice <> nil; 
end; 

procedure TFutronicFingerPrint.Close; 
begin 
    if fDevice <> nil then 
    begin 
    ftrScanCloseDevice(fDevice); 
    fDevice = nil; 
    end; 
end; 
+0

あなたは私にそれを打ち負かしました(そして私よりもはるかに優れていました)。 :-) –

+0

ありがとうございました。一度テストしたものを更新します。 – Ago

+0

@KenWhite - あなたのJedi Mind-Debuggingスキルは、全く同じです。 –

関連する問題