在Delphi XE6 x64下编译时,GetProcAddress无法运行

时间:2014-08-29 21:46:42

标签: delphi 64-bit getprocaddress

在Delphi XE6 x64下编译时,以下GetProcAddress代码失败。在Delphi x86下编译时运行正常。你能帮忙评论一下做错了吗?

program Project11;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  SysUtils;

var
  Library_OpenGL: LongWord;

function LoadLibrary(lpFileName: pAnsiChar): LongWord; stdcall; external 'kernel32.dll' name 'LoadLibraryA';
function GetProcAddress(hModule: LongWord; lpProcName: pAnsiChar): Pointer; stdcall; external 'kernel32.dll' name 'GetProcAddress';

begin
  try
    Library_OpenGL := LoadLibrary('opengl32.dll');
    Assert(GetProcAddress(Library_OpenGL, 'glEnable') <> nil, 'GetProcAddress(Library_OpenGL, ''glEnable'') = nil');
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  ReadLn;
end.

1 个答案:

答案 0 :(得分:14)

您的翻译错了。模块句柄是指针大小,它解释了为什么你的错误翻译工作在32位而不是64位。

要更正,请将Windows单元添加到uses子句中,删除LoadLibrary()GetProcAddress()的声明,并将Library_OpenGL声明为{{1} (x64中的8个字节):

HMODULE

作为一个额外的好处,您现在直接调用本机Unicode program Project11; {$APPTYPE CONSOLE} {$R *.res} uses SysUtils, Windows; var Library_OpenGL: HMODULE; begin try Library_OpenGL := LoadLibrary('opengl32.dll'); Assert(GetProcAddress(Library_OpenGL, 'glEnable') <> nil, 'GetProcAddress(Library_OpenGL, ''glEnable'') = nil'); except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; ReadLn; end. ,而不是通过LoadLibraryW适配器进行从ANSI到系统本机UTF-16的对话。