I have this code in C that I want to port to Delphi, but i can't make it work.
.CPP CODE
#include <Windows.h>
#include <io.h>
#include <stdio.h>
#include "GLibExp.h"
#pragma comment(lib, "GLib.lib")
void MyCFunc(LPCTSTR GStr)
{
GFile GVar = NULL;
GVar = GrfLoad(GStr, 1);
if ( !GVar )
{
printf("Error during loading!\n");
} else
printf("All fine!\n");
GrfFree(GVar);
system("pause");
}
void main()
{
CHAR StrG[MAX_PATH] = "Test.grf";
MyCFunc(StrG);
return;
}
GLibExp.h
#ifndef GLibExpH
#define GLibExpH
#if defined(GRF_DLL)
#define GEXPORT __declspec(dllexport)
#else
#define GEXPORT extern
#endif
class CGFILE;
typedef CGFILE* GFile;
//typedef void* GFile; //Also works like this
#ifdef __cplusplus
extern "C" {
#endif
GEXPORT GFile GrfLoad(const char *GName, unsigned char Mode = 1);
GEXPORT void GrfFree(GFile GVar);
#ifdef __cplusplus
}
#endif
#endif//GLibExpH
The program calls a DLL at runtime to use the GRFLoad
and GRFFree
functions. I try to port this to Delphi, but without success.
Delphi/Lazarus Code:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
{$Link GLib.lib}
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
function GrfLoad(const fname: PChar; Modo: Boolean): Pointer; cdecl; external 'GLib.dll';
procedure TForm1.Button1Click(Sender: TObject);
var
PVar: Pointer;
begin
PVar:= GrfLoad(PChar('test.grf'),false);
end;
end.
If I comment out the line {$Link GLib.lib}
, the program runs, but it always crashes when I call GRFLoad
(the program stops working and then closes). If I leave in the {$Link GLib.lib}
line, the program don't compile and reports an error:
project1.lpr(20,1) Error: Illegal COFF Magic while reading GLib.lib
Any hints?
NOTE: I just added a link to a Visual C++ 2010 project with all files needed. In fact, I just made a "New project -> Win32 Console Application" (I mark "empty project" in the Wizard), add a new CPP file and paste the code, and change "Properties -> Configuration Properties -> Linker -> Enable Incremental Linking: NO", that all.
https://drive.google.com/open?id=1JxW4Wra_kT8gfBda1t0WWqagzazzQVne