How can i import functions with ordinal numbers using libraries?
I want to the linker place only just the ordinal not the name of a function. Or i must modify the .idata section manually? And if, what i need to place after .code .idata?

Thanks!
Posted on 2004-09-09 12:39:12 by Zord
Zord

You asked about importing functions with ordinal numbers using libraries. I'm not sure whether you mean using static libraries (containing actual code) or libraries which contain only the names of exports and which are given to the linker. If the latter, then it is easy to do (at least with GoAsm and GoLink), as you can see from this excerpt from the GoAsm help file:-


Direct importing by ordinal
The type of importing we have been looking at using CALL procedure will import the procedure by name. What happens here is that when the Windows loader starts the program it searches through the Dlls for the imports required by the program. This is done by comparing the names of the Dll exports against the names of the program's imports. To speed up this process with private Dlls sometimes exporting and importing by ordinal is used. Then the loader can find the correct import by using an index to a table within the Dll. Note that it is unwise to do this in the case of Windows system Dlls since the ordinal numbers of the exports are not guaranteed to be the same across different Dll versions.
Using GoAsm and its companion program GoLink, you can import by ordinal using this simple syntax:-

CALL MyDll:6

This will call procedure number 6 in MyDll.dll. Note that the extension "dll" is assumed if no extension is given. Suppose you want a Dll to call a function in the main executable by ordinal, then you could use:-
CALL Main.exe:15

This calls the 15th function in Main.exe.
Calls to ordinals using the absolute form (using opcodes FF15) will result from using this syntax:-
CALL

You should not include the path of the file in the call. GoLink carries out a wide search for specified files, but if it is necessary to provide a path this should be given to the linker and not incorporated in the call in the assembler script.
Obviously in order to use this method of calling a function by ordinal you must ensure that the ordinal number of the function is fixed see exporting by ordinal.
Note: the above only applies to GoLink

There is another way to use ordinals using LoadLibrary to load the Dll (or return a handle if it is already loaded) and then calling GetProcAddress passing the ordinal value to get the value of the procedure to call. Finally you call the procedure as returned by GetProcAddress.
Posted on 2004-09-09 12:52:25 by jorgon
Yes i know it, but i use MASM. I want that the import table contains only the ordinal not the name of the function. I don't know how to make that, maybe using libraries or modify the .idata section in source?
Thanks for any help!
Posted on 2004-09-09 13:10:25 by Zord
Zord

Perhaps you could use the "Go" tools to set up your functions where you want to use ordinals and use masm for the rest of your code? If so, here is an excerpt from the relevant part of the GoAsm manual:

Exporting by ordinal
Normally exports are conducted by name. What happens here is that when the Windows loader starts the program it searches through the Dlls for the imports required by the program. This is done by comparing the names of the Dll exports against the names of the program's imports. To speed up this process with private Dlls sometimes exporting and importing by ordinal is used. Then the loader can find the correct import by using an index to a table within the Dll. Note that it is unwise to do this in the case of Windows system Dlls since the ordinal numbers of the exports are not guaranteed to be the same across different Dll versions.
In order to use this method it is clearly imperative that the exporting program specifies an ordinal value for a particular export and the linker must not change this. Again, using GoAsm you can specify the correct ordinal value and pass this to the linker via the .drectve section (not all linkers support this).
Here is how to specify an export by ordinal if exports are listed before any sections are declared:-
EXPORTS CALCULATE:2, DATA_VALUE:6

Here the linker will be instructed to use the ordinals 2 and 6 for the exports. If you are using the alternative method of declaring exports (within a section) you can use for example:-
EXPORT:2 CALCULATE:

or in the case of data:-
EXPORT:6 DATA_VALUE DD 0

Exporting by ordinal without a name
Exporting by ordinal does not stop the name of the export appearing in the final executable. This is because it is the importing program which decides whether to import by ordinal or by name. All the exporting program can do is to fix the ordinal value. However sometimes a programmer might like to ensure no name for the export appears in the final executable. You sometimes see such "no name" exports in system Dlls for example, probably in order to hide the job carried out by particular functions. In order to do this in GoAsm add the word NONAME to the end of the export for example:-
EXPORT:2:NONAME
CALCULATE:

Here the value of the code label CALCULATE will be exported as ordinal number 2, but the name of the export will not appear in the final executable. This means that if another program tried to call the CALCULATE function it would fail. The function can only be called by ordinal.

Possibly masm allows you to do the same - maybe through the EXPORT directive, NONAME and the .drectve section as used by the "Go" tools.
Posted on 2004-09-09 14:40:49 by jorgon
Thanks for replies!
I created a module definition file for the dll and used with lib.exe to generate import library. It seems works correctly, i just put the used functionnames after EXPORTS directive and i used NONAME to throw names away from import table.
I don't tried the .drectve section but it seems good for this.
Posted on 2004-09-09 15:09:23 by Zord