Author Topic: How do I alias an imported DLL function?  (Read 19623 times)

Offline ben321

  • Full Member
  • **
  • Posts: 185
How do I alias an imported DLL function?
« on: January 11, 2017, 02:51:01 AM »
That is to say, how do I rename an external function, so that the name used internally is different from the actual name of the function I'm calling.
For example, in Windows, GDI32.DLL has a function called SetPix. After pushing the required arguments onto the stack, you run "CALL SetPix". What if I want to rename the function internally, so that it has the name SetPixel, such that when called you would use "CALL SetPixel"? Visual Basic 6 has such a feature, the key word Alias. In VB6 you import a function using this syntax:
Declare Function NameOfFunctionInTheVisualBasicProgram Lib "DllFileName.dll" Alias "NameOfFunctionInTheDllFile" (arguments, go, in, the, parentheses, and, are, separated, by, commas)

How do you do such an Alias in NASM? Is it possible to have the name within NASM be different than the name of the name of the function in the imported DLL file? This is important, because I'm going to have a program where one of the names of the assembly language functions I'm using happens to also have the same name as a similar function in a DLL file that my program also uses.

Offline Mathi

  • Jr. Member
  • *
  • Posts: 82
  • Country: in
    • Win32NASM
Re: How do I alias an imported DLL function?
« Reply #1 on: January 12, 2017, 12:07:02 PM »
Hi ,

From the NASM Manual (6.2.4)
The IMPORT directive takes two required parameters, separated by white space, which are (respectively) the name of the symbol you wish to import and the name of the library you wish to import it from. For example:
          import WSAStartup wsock32.dll

A third optional parameter gives the name by which the symbol is known in the library you are importing it from, in case this is not the same as the name you wish the symbol to be known by to your code once you have imported it. For example:

          import asyncsel wsock32.dll WSAAsyncSelect

Looks to be related to your query.

Regards,
Mathi.

Offline ben321

  • Full Member
  • **
  • Posts: 185
Re: How do I alias an imported DLL function?
« Reply #2 on: January 13, 2017, 11:48:48 PM »
Hi ,

From the NASM Manual (6.2.4)
The IMPORT directive takes two required parameters, separated by white space, which are (respectively) the name of the symbol you wish to import and the name of the library you wish to import it from. For example:
          import WSAStartup wsock32.dll

A third optional parameter gives the name by which the symbol is known in the library you are importing it from, in case this is not the same as the name you wish the symbol to be known by to your code once you have imported it. For example:

          import asyncsel wsock32.dll WSAAsyncSelect

Looks to be related to your query.

Regards,
Mathi.

Ever tried to compile a program with the target system being win32, when your asm code contains the IMPORT directive? It just doesn't work. It gives an error about how you can't use IMPORT. I think you have to select a different target type than win32, but then it can't run properly in Windows. To work around the lack of the ability to use IMPORT, I've simply used EXTERN for the function, and then I make sure to include the name of the DLL file that has that function, in the command line for the linker. The linker I'm using us is GoLink, because it takes actual DLL file names as command line parameters, and looks in each one until it finds one that exports function being requested in the object file that is output from NASM. It's requested in the object file, because of the use of the use of the EXTERN directive. It doesn't require an import library as other linkers (such as ALink) require, making it a much easier linker to use, and gets around the problem of the IMPORT directive not working in NASM's win32 mode.

Without using the IMPORT directive, and only using the EXTERN directive, is it possible to alias the name of a function in a DLL file in NASM?
« Last Edit: January 14, 2017, 12:00:32 AM by ben321 »

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: How do I alias an imported DLL function?
« Reply #3 on: January 28, 2017, 06:53:19 AM »
WARNING:
I haven't used Windows in a few years. I'm not sure how up to date this information is.



Is it possible to have the name within NASM be different than the name of the name of the function in the imported DLL file?

Yes you can! But not with NASM.  ???

Okay, so that answer isn't exactly clear is it.  ;D

The problem with your above question is, what you want to do isn't the responsibility of NASM and therefore NASM doesn't do it. Aliasing external functions is for your linker to handle. What VB6 was doing was creating a .DEF file and using the LIB program to generate a .LIB file which got linked into your program. The .LIB file would contain an alias that forwards one symbol to the other. For example, here is a .DEF file for KERNEL32.DLL. This file can be built using LIB /DEF:KERNEL32.DEF and then the resulting KERNEL32.LIB can be linked in at compile-time. In this example, the internal (decorated) names are being aliased with simpler (undecorated) names.

If you want to create a DEF file for a DLL, use the DUMPBIN utility that comes with MS VC++. With this program, you can get a list of all the function names exported by the DLL, like so:

Code: [Select]
DUMPBIN /EXPORTS  wsock32.dll  >  wsock32.def
Then edit the wsock32.def to include these lines before the list of names:

Code: [Select]
LIBRARY wsock32
EXETYPE NT
SUBSYSTEM WINDOWS
EXPORTS

Finally, at the end of the DEF add a line to forward the symbol:

Code: [Select]
WSAAsyncSelect = asyncsel
And finally, you can create your .LIB from this .DEF file like we do above, LIB /DEF:wsock32.def. Then in your assembler code, you should be able to just use EXTERN asyncsel without worrying about what the other name was.

Of course, this assumes you are using a linker that supports LIB files, like Microsoft's Linker or Pelle Orinius' Linker. As far as I know, GoLINK doesn't have support for non-static .LIB files (in fact, it's a bragging right that it doesn't need them and uses the DLL's directly). So your mileage may vary.
« Last Edit: January 28, 2017, 06:56:32 AM by Bryant Keller »

About Bryant Keller
bkeller@about.me

Offline ben321

  • Full Member
  • **
  • Posts: 185
Re: How do I alias an imported DLL function?
« Reply #4 on: March 16, 2017, 05:37:44 AM »
WARNING:
I haven't used Windows in a few years. I'm not sure how up to date this information is.



Is it possible to have the name within NASM be different than the name of the name of the function in the imported DLL file?

Yes you can! But not with NASM.  ???

Okay, so that answer isn't exactly clear is it.  ;D

The problem with your above question is, what you want to do isn't the responsibility of NASM and therefore NASM doesn't do it. Aliasing external functions is for your linker to handle. What VB6 was doing was creating a .DEF file and using the LIB program to generate a .LIB file which got linked into your program. The .LIB file would contain an alias that forwards one symbol to the other. For example, here is a .DEF file for KERNEL32.DLL. This file can be built using LIB /DEF:KERNEL32.DEF and then the resulting KERNEL32.LIB can be linked in at compile-time. In this example, the internal (decorated) names are being aliased with simpler (undecorated) names.

If you want to create a DEF file for a DLL, use the DUMPBIN utility that comes with MS VC++. With this program, you can get a list of all the function names exported by the DLL, like so:

Code: [Select]
DUMPBIN /EXPORTS  wsock32.dll  >  wsock32.def
Then edit the wsock32.def to include these lines before the list of names:

Code: [Select]
LIBRARY wsock32
EXETYPE NT
SUBSYSTEM WINDOWS
EXPORTS

Finally, at the end of the DEF add a line to forward the symbol:

Code: [Select]
WSAAsyncSelect = asyncsel
And finally, you can create your .LIB from this .DEF file like we do above, LIB /DEF:wsock32.def. Then in your assembler code, you should be able to just use EXTERN asyncsel without worrying about what the other name was.

Of course, this assumes you are using a linker that supports LIB files, like Microsoft's Linker or Pelle Orinius' Linker. As far as I know, GoLINK doesn't have support for non-static .LIB files (in fact, it's a bragging right that it doesn't need them and uses the DLL's directly). So your mileage may vary.

My linker of choice to use with NASM is GoLink. It does not take LIB files. Since LIB files are collections of OBJ files, to streamline the usage of GoLink requires discrete OBJ files (it will not work with LIB files). As for import libraries, that still won't work with go link, because again these are LIB files. Can you think of a way to do Aliases with GoLink?

Offline dreamCoder

  • Full Member
  • **
  • Posts: 107
Re: How do I alias an imported DLL function?
« Reply #5 on: March 17, 2017, 12:05:31 PM »
Use %define

Offline ben321

  • Full Member
  • **
  • Posts: 185
Re: How do I alias an imported DLL function?
« Reply #6 on: May 09, 2017, 07:57:40 PM »
Use %define

Can you give me an example of using %define to alias an imported DLL function name?