我需要使用qsBarcode的DLL文件http://www.qsbarcode.de/en/index.htm(這裏是下載鏈接http://www.qsbarcode.de/en/download/qsbar39.zip)。該DLL會將包含條形碼code39的位圖圖像解碼爲一個字符串。請幫助如何將C代碼轉換爲Delphi代碼(qsBarcode)
在他們的例子中只有VB和C的例子,但我需要在Delphi中使用它。 這裏是在C正式示例代碼:
#include <windows.h>
#include <stdio.h>
typedef int (WINAPI * CODE39_PROC)(char *, char *);
int main(int argc, char* argv[])
{
HINSTANCE hinstLib;
CODE39_PROC ProcAdd;
BOOL fFreeResult;
char cFileName[512] = "\0";
char cResult[512] = "\0";
int iReturn = 0;
if(argc < 2) return 0; //no bitmap filename in argv[1]
strcpy(cFileName,argv[1]);
hinstLib = LoadLibrary("qsBar39");
if (hinstLib == NULL) return -1; //can't load lib
ProcAdd = (CODE39_PROC) GetProcAddress(hinstLib, "ReadCode39");
if (NULL == ProcAdd) return -1; //can't access Proc
//dll Proc call
iReturn = (ProcAdd) (cFileName, cResult);
printf("%s", cResult);
fFreeResult = FreeLibrary(hinstLib);
return iReturn;
}
,這就是我試圖代碼德爾福
unit uRead;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Mask, JvExMask, JvToolEdit;
type
TDLLFunc = function(namafile: PChar; hasil:PChar):integer;
TForm2 = class(TForm)
JvFilenameEdit1: TJvFilenameEdit;
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
const
DLLFunc: TDLLFunc = nil;
var
Form2: TForm2;
DLLHandle: THandle;
implementation
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
var
feedback : integer;
hasil:PChar;
begin
DLLHandle := LoadLibrary('qsBar39.dll');
if (DLLHandle < HINSTANCE_ERROR) then
raise Exception.Create('library can not be loaded or not found. ' + SysErrorMessage(GetLastError));
try
{ load an address of required procedure}
@DLLFunc := GetProcAddress(DLLHandle, 'ReadCode39');
{if procedure is found in the dll}
if Assigned(DLLFunc) then
feedback := DLLFunc(PChar(JvFilenameEdit1.Text), PChar(hasil));
showmessage(hasil);
finally
{unload a library}
FreeLibrary(DLLHandle);
end;
end;
end.
當我執行這個代碼和調試,hasil只包含#$11'½ 雖然它應該返回條形碼圖像中的一些字符(您可以在zip文件中獲取文件圖像)。 請幫助我,謝謝。
最新更新時間:
@ 500,謝謝,我已經把STDCALL
@dthorpe,謝謝,做
其實建議是偉大的,我的代碼應該運行良好,但我錯誤地把JvFilenameEdit1.text而不是JvFilenameEdit1.FileName,我的壞:)
再次感謝您的意見,所以這裏是工作代碼:
unit uRead;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Mask, JvExMask, JvToolEdit;
type
TDLLFunc = function(namafile: PAnsiChar; hasil:PAnsiChar):integer; stdcall;
TForm2 = class(TForm)
JvFilenameEdit1: TJvFilenameEdit;
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
const
DLLFunc: TDLLFunc = nil;
var
Form2: TForm2;
DLLHandle: THandle;
implementation
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
var
feedback : integer;
hasil: array [0..512] of char;
begin
DLLHandle := LoadLibrary('qsBar39.dll');
if (DLLHandle < HINSTANCE_ERROR) then
raise Exception.Create('library can not be loaded or not found. ' + SysErrorMessage(GetLastError));
try
{ load an address of required procedure}
@DLLFunc := GetProcAddress(DLLHandle, 'ReadCode39');
{if procedure is found in the dll}
if Assigned(DLLFunc) then
feedback := DLLFunc(PAnsiChar(JvFilenameEdit1.FileName), @hasil);
edit1.Text := StrPas(@hasil);
finally
{unload a library}
FreeLibrary(DLLHandle);
end;
end;
end.
如果你用delphi 2009+(Unicode版本),那麼我認爲你需要在Delphi代碼 – ComputerSaysNo 2011-05-20 01:07:19
喜與PAnsiChar更換PChar類型Dorin,我使用的是Delphi 2006,但我試圖使用PAnsiChar,謝謝 – Erwan 2011-05-20 03:46:30
Erwan:不要將標題改爲(已解決)。將問題標記爲已回答。 – dthorpe 2011-05-20 06:35:45