Forum > General
[SOLVED] Question about "cvar"
440bx:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$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:
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:
Probably for cygwin/mingw use that prefixed local vars with _ on COFF targets.
440bx:
--- Quote from: tetrastes on July 03, 2021, 11:30:49 am ---after removing "cvar" from declaration, your code works both for 32 and 64 bit.
--- End quote ---
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:
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.
Navigation
[0] Message Index
[#] Next page