Exetools

Exetools (https://forum.exetools.com/index.php)
-   General Discussion (https://forum.exetools.com/forumdisplay.php?f=2)
-   -   Static import by ordinal (https://forum.exetools.com/showthread.php?t=10195)

nino 09-27-2006 00:08

Static import by ordinal
 
Is there any way to import a function in a C program that is only exported by ordinal from a DLL (besides dynamically using LoadLibrary/GetProcAddress)?
For example the IsOS function is documented by MS (recently) and is exported by shlwapi.dll as ordinal 437, how can it be statically linked in a C program?

MarkusO 09-27-2006 00:45

Simply use a *.DEF file and pass "import by ordinals" to your linker.

nino 10-02-2006 07:40

Your answer was a bit cryptic but it put me on the way to the solution.
For other readers of this post I would recommend the following article on Microsoft's Knowledge Base: "How To Create 32-bit Import Libraries Without .OBJs or Source". As the title suggests the modifications are NOT to your application but are related to the DLL instead. Also the "import by ordinal" linker option is only relevant to Borland C/C++ products. Hope this helps...

MarkusO 10-02-2006 19:14

The way Microsoft describes the problem is quite complex and I don't see how to use the document to import by ordinals (since "IsOS" is not exported by name, only by ordinal). I only know a far more simple way.

Create a *.DEF file with the following content:
Code:

IMPORTS
  IsOS = SHLWAPI.437

Declare the "IsOS" function as external. Then pass the *.DEF file to your linker and use the "import by ordinal" option (every linker has it, not only the Borland linker). The problem is that the Microsoft linker says that "IMPORTS" is not supported on Win32, which makes it quite useless here (but it works with Win16 ordinals).

This will then import all functions by name, expect the one you called "IsOS", which is imported by ordinal. If you don't use the "import by ordinal" switch, your program will try to load "IsOS" from SHLWAPI.DLL and (of course) fail.

If you have to use the Microsoft linker for some reason, you should simply create yourself a program which parses your import table and replaces a given imported name with it's ordinal. Of course this will require manual input if the function isn't exported by name at all.

nino 10-03-2006 03:37

The IMPORT section is no longer valid in a .DEF file. So there is no way you could tell your C program how to import. Other compilers - including managed code in Visual C++ - do have the option to specify import attributes such as the ordinal for the import.
The article explains two alternatives: one with import by ordinals only for C functions and the other for general functions (but does not use import by ordinal). However, just combine the two solutions and you can get what you want. That is, create stubs for the functions you want to import and also create a DEF file with EXPORTS to specify the ordinal, then generate the dummy DLL and you get a LIB that allows you to import the desired function by ordinal. I did it and it worked.


All times are GMT +8. The time now is 01:47.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2026, vBulletin Solutions, Inc.
Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX