Note that there are some explanatory texts on larger screens.

plurals
  1. POPlease help how to Convert C code to Delphi code (qsBarcode)
    primarykey
    data
    text
    <p>I need to use a DLL file from qsBarcode <a href="http://www.qsbarcode.de/en/index.htm" rel="nofollow">http://www.qsbarcode.de/en/index.htm</a> (here is the download link <a href="http://www.qsbarcode.de/en/download/qsbar39.zip" rel="nofollow">http://www.qsbarcode.de/en/download/qsbar39.zip</a>). The dll will decode a bitmap image that contain barcode code39 into a string.</p> <p>In their example there are only VB and C example, but I need to use it in Delphi. here is the official example code in C:</p> <pre><code>#include &lt;windows.h&gt; #include &lt;stdio.h&gt; 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 &lt; 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; } </code></pre> <p>and this is what I try to code in Delphi</p> <pre><code>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 &lt; 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. </code></pre> <p>When I execute this code and debug, hasil only contains <strong>#$11'½</strong> while it should return some character in the barcode image (you can get the file image in the zip file). Please help me, thank you.</p> <hr> <p>latest update:</p> <p>@500, thanks, I have put stdcall </p> <p>@dthorpe, thanks, done</p> <p>Actually the advice is great, my code is supposed to running well, but I've mistakenly put JvFilenameEdit1.text instead of JvFilenameEdit1.FileName, my bad :)</p> <p>Thank you again for the advice, so here is the working code:</p> <pre><code>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 &lt; 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. </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload