Error : Constant or type identifier expected

Error : Constant or type identifier expected

我正在尝试在我的 delphi 7 程序中使用外部 DLL 函数。我在 C 中有一个示例,其中调用了 DLL 文件。

在C中是这样定义的

#define SerNumAddress 0x1080

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

然后在delphi我写

unit msp430d;

interface
USES windows, event, paras;

const

SerNumAddress = 80  ;
pmspganginitcom:FARPROC = nil;

TYPE
hDLL430 = HINSTANCE;

implementation

end.

但我收到 constant or type identifier expected 错误。

问题是 HINSTANCE 的使用。

System单元中有一个名为HInstance的全局变量,表示包含正在执行的代码的模块句柄。您正在尝试使用 HINSTANCE 作为类型。由于变量 HInstance,名为 HINSTANCE 的类型会发生冲突。因此,该类型在 Windows 单元中被翻译为 HINST

所以,下面的代码可以编译:

type
  hDLL430 = HINST;

不过,在我看来,这些天使用HMODULE会更正常。参见 What is the difference between HINSTANCE and HMODULE?

考虑 C 代码中的注释:

HINSTANCE hDLL430; // handle of DLL

好吧,如果您查看 LoadLibrary and GetProcAddress 的声明,您会发现 DLL 模块句柄由 HMODULE 表示。所以我会把这段代码翻译成:

type
  hDLL430 = HMODULE;

此外,与其使用 FARPROC,我会选择声明一个包含参数、return 值和调用约定的函数指针,以允许编译器强制执行类型安全。