Search code examples
delphidelphi-7

Error : Constant or type identifier expected


I am trying to use an External DLL function in my delphi 7 program. I have an example in C in which DLL file called.

In C it is defined like this

#define SerNumAddress 0x1080

HINSTANCE hDLL430;                          // handle of DLL
static FARPROC pMSPGangInitCom  = NULL; // pointer to DLL function

and in delphi i write

unit msp430d;

interface
USES windows, event, paras;

const

SerNumAddress = $1080  ;
pmspganginitcom:FARPROC = nil;

TYPE
hDLL430 = HINSTANCE;

implementation

end.

But I am getting constant or type identifier expected error.


Solution

  • The problem is the use of HINSTANCE.

    In the System unit there is a global variable named HInstance that represents the module handle that contains the code being executed. You are trying to use HINSTANCE as a type. Because of the variable HInstance, a type named HINSTANCE would clash. So instead that type is translated as HINST in the Windows unit.

    So, the following code would compile:

    type
      hDLL430 = HINST;
    

    However, in my view, it would be more normal to use HMODULE these days. See What is the difference between HINSTANCE and HMODULE?

    Consider the comment in the C code which says:

    HINSTANCE hDLL430; // handle of DLL
    

    Well, if you look at the declarations of LoadLibrary and GetProcAddress, you'll see that the DLL module handle is represented by HMODULE. So I would translate this code as:

    type
      hDLL430 = HMODULE;
    

    Furthermore, rather than using FARPROC I would opt to declare a function pointer that contained the parameters, return value and calling convention to allow the compiler to enforce type safety.