[SOLVED] Question about "cvar" lazarus / 62-General ---------------------------------------------------------------------- 440bx · 2021-07-03 01:55 Hello, Ntdll exports a number of variables which can be imported in FPC using "cvar". There is a variable named LdrSystemDllInitBlock which is exported in both, the 32bit and 64bit versions of ntdll.  Accessing the variable from a 64bit program works as expected but, from a 32bit program, Windows complains that the variable "_LdrSystemDllInitBlock" (note the extra underscore) is not found in ntdll. Here is a little test program that operates as expected in 64bit but, causes the "_LdrSystemDllInitBlock entry point not found" in ntdll.  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$APPTYPE     CONSOLE} {$TYPEDADDRESS  ON} {$LONGSTRINGS   OFF} { -------------- ------------------------------------------------------------- }  {$ifdef VER90}  {$FATAL this program can only be compiled with FPC}{$endif} { ------------------------------------------------------- -------------------- }  program _cvars; uses  sysutils  ;  var  LdrSystemDllInitBlock : DWORD; cvar; external 'ntdll';  begin  writeln;  writeln;   writeln('  address of LdrSystemDllInitBlock is : ',             '$' + IntToHex(ptruint(@LdrSystemDllInitBlock), 0));  writeln;   writeln('  value   of LdrSystemDllInitBlock is : ',           LdrSystemDllInitBlock);   writeln;  writeln;  writeln('press ENTER/RETURN to end this program');  readln;end.         My question is: why is it looking for the variable name preceded by an "_" when the program is compiled for 32bit and what are the steps to take to eliminate the problem ? Thank you for your help. Attached is a screenshot of the error message and verification that the export is present in the 32bit version of ntdll (without the "_") ---------------------------------------------------------------------- tetrastes · 2021-07-03 09:30 I don't know why "cvar" adds underscore for 32 bit and does not for 64, but after removing "cvar" from declaration, your code works both for 32 and 64 bit. ---------------------------------------------------------------------- marcov · 2021-07-03 11:00 Probably for cygwin/mingw use that prefixed local vars with _ on COFF targets. ---------------------------------------------------------------------- 440bx · 2021-07-03 18:35 Quote from: tetrastes on July 03, 2021, 11:30:49 amafter removing "cvar" from declaration, your code works both for 32 and 64 bit. For some reason, I was under the impression that "cvar" was required to import external variable.  I'm rather pleased to see that it is not.  tetrastest, thank you very much for mentioning that.  Very useful detail to know. ---------------------------------------------------------------------- PascalDragon · 2021-07-04 10:54 cvar applies the C mangling to the variable. And that mangling on Windows 32-bit does have a leading _ (on Windows 64-bit Microsoft dropped that). However the Windows API does not use C mangling, thus you must not use cvar for those variables. ---------------------------------------------------------------------- 440bx · 2021-07-04 14:00 Quote from: PascalDragon on July 04, 2021, 12:54:21 pmcvar applies the C mangling to the variable. And that mangling on Windows 32-bit does have a leading _ (on Windows 64-bit Microsoft dropped that). However the Windows API does not use C mangling, thus you must not use cvar for those variables. Thank you PascalDragon.  I like to understand how something works.  That explanation was very helpful. ---------------------------------------------------------------------- tetrastes · 2021-07-05 10:57 Yes, the explanation was helpful, but why we read something different in documentation? https://www.freepascal.org/docs-html/current/prog/progsu147.html https://www.freepascal.org/docs-html/current/ref/refse22.html ---------------------------------------------------------------------- PascalDragon · 2021-07-05 11:03 Quote from: tetrastes on July 05, 2021, 12:57:46 pmYes, the explanation was helpful, but why we read something different in documentation? https://www.freepascal.org/docs- html/current/prog/progsu147.html#x186-1890007.1.2 https://www.freepascal.org/docs- html/current/ref/refse22.html#x53-730004.2 Well, the documentation is mostly right (especially the case sensitive part). The only thing that's missing is the mention of the C-prefix (the '_'). Please file a bug report. ---------------------------------------------------------------------- 440bx · 2021-07-05 19:41 Quote from: PascalDragon on July 05, 2021, 01:03:03 pmThe only thing that's missing is the mention of the C-prefix (the '_'). Please file a bug report. Done.  Ticket: 0039164 ---------------------------------------------------------------------- PascalDragon · 2021-07-06 13:24 Thanks.