Recent

Author Topic: importing "var" by ordinal possible ?  (Read 1340 times)

440bx

  • Hero Member
  • *****
  • Posts: 6069
importing "var" by ordinal possible ?
« on: April 14, 2022, 04:55:46 am »
Hello,

Some Windows dlls export variables by _ordinal_ _only_, among them is comctl32 which exports a number of them.

For instance, "Free" is ordinal 73 (the name is from its PDB - the export is _not_ named), when I try to declare the variable, i.e,
Code: Pascal  [Select][+][-]
  1.   var
  2.     Free : pchar; external comctl32 index 73;
  3.  

the compiler complains about it with the following error :
Code: Text  [Select][+][-]
  1. Fatal: Syntax error, ";" expected but "identifier INDEX" found

Since the export is not named, its ordinal has to be specified. 

Is there a way to declare that exported variable in a way that makes it acceptable to FPC ?

Thank you for your help.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

ccrause

  • Hero Member
  • *****
  • Posts: 1093
Re: importing "var" by ordinal possible ?
« Reply #1 on: April 14, 2022, 07:41:11 am »
I don't see an index option listed in the reference.

Quote
var
    Free : pchar; external comctl32 index 73;
I assume comctrl32 is declared as a string constant?

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: importing "var" by ordinal possible ?
« Reply #2 on: April 14, 2022, 08:43:25 am »
"By default, functions and procedures that are declared and implemented in library are not available to a programmer that wishes to use this library. In order to make functions or procedures available from the library, they must be exported in an exports clause:"

(see the .pngs in this link: https://www.freepascal.org/docs-html/ref/refse117.html#x242-26600016.7

"Under Win32, an index clause can be added to an exports entry. An index entry must be a positive number larger or equal than 1, and less than MaxInt.
Optionally, an exports entry can have a name specifier. If present, the name specifier gives the exact name (case sensitive) by which the function will be exported from the library.
If neither of these constructs is present, the functions or procedures are exported with the exact names as specified in the exports clause."
 

440bx

  • Hero Member
  • *****
  • Posts: 6069
Re: importing "var" by ordinal possible ?
« Reply #3 on: April 14, 2022, 11:59:52 am »
I don't see an index option listed in the reference.

Quote
var
    Free : pchar; external comctl32 index 73;
I assume comctrl32 is declared as a string constant?
You're right the "index" option does not appear in that definition but, it is a valid option (maybe a documentation bug should be filed ?).  For instance, the following compiles:
Code: Pascal  [Select][+][-]
  1. function AddMRUData
  2.            (
  3.             { _in_       } InListHandle : THANDLE;
  4.             { _in_ const } InData       : pointer;
  5.             { _in_       } InDataSize   : DWORD
  6.            )
  7.          : longint; stdcall; external comctl32 index 167;   { ordinal only !! }
  8.  
which imports comctl32's ordinal 167 using the name "AddMRUData" (which is the name it is given in comctl32's PDB symbols - in the PE file, it is unnamed)

Also, you are correct that the external name "comctl32" has been defined as a constant earlier in the unit.



@howardpc

I want to import unnamed variables that are exported by comctl32.dll - for a function, the compiler allows to specify the index of the unnamed function (as shown above) but, it doesn't seem to allow the same for an unnamed variable which is what's needed when the variable is exported by ordinal only.
 
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

PascalDragon

  • Hero Member
  • *****
  • Posts: 6311
  • Compiler Developer
Re: importing "var" by ordinal possible ?
« Reply #4 on: April 14, 2022, 12:05:36 pm »
Is there a way to declare that exported variable in a way that makes it acceptable to FPC ?

Importing variables by ordinal is currently not supported. Feel free to open a feature request.

As a work around (not tested) you could try to import it as a function and then play around with its address. Or use a global property that dynamically loads the variable using LoadLibrary if not yet loaded.

"By default, functions and procedures that are declared and implemented in library are not available to a programmer that wishes to use this library. In order to make functions or procedures available from the library, they must be exported in an exports clause:"

(see the .pngs in this link: https://www.freepascal.org/docs-html/ref/refse117.html#x242-26600016.7

"Under Win32, an index clause can be added to an exports entry. An index entry must be a positive number larger or equal than 1, and less than MaxInt.
Optionally, an exports entry can have a name specifier. If present, the name specifier gives the exact name (case sensitive) by which the function will be exported from the library.
If neither of these constructs is present, the functions or procedures are exported with the exact names as specified in the exports clause." 

That is about exporting, not importing.

I don't see an index option listed in the reference.

Quote
var
    Free : pchar; external comctl32 index 73;
I assume comctrl32 is declared as a string constant?
You're right the "index" option does not appear in that definition but, it is a valid option (maybe a documentation bug should be filed ?).

Yes, please file such a bug report as well.

440bx

  • Hero Member
  • *****
  • Posts: 6069
Re: importing "var" by ordinal possible ?
« Reply #5 on: April 14, 2022, 12:22:57 pm »
ETA:

@PascalDragon,

Thank you for the clear answer



Yes, please file such a bug report as well.
done.  ticket: 39668
« Last Edit: April 14, 2022, 12:37:02 pm by 440bx »
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018