News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

using dumpbin.exe to create a .def file

Started by MikeT, October 09, 2009, 09:46:06 AM

Previous topic - Next topic

sinsi

If you know the name of the dll and the functions, can't you just type out a def file? It's only a text file.
Light travels faster than sound, that's why some people seem bright until you hear them.

rags

God made Man, but the monkey applied the glue -DEVO

Vortex

Hi MikeT,

I downloaded sqlite3.dll from the link posted by MichaelW. Attached is sqlite3.def extracted from this dll.

MikeT

Vortex,
Im sorry, the SQLite dll was a bad choice. I am not trying to make a .lib for that library. I just mentioned it as an example of a dll that one might want to use because it was used in the example here:
http://www.coderetard.com/2009/01/21/generate-a-lib-from-a-dll-with-visual-studio/
The test dll I AM trying to link statically is called AddTwoNum.dll. Sorry for the confusion

sinsi,
Yes I just realized that! urgh.

I finally got the VS2008 dumpbin to work by copying the following files to a seperate folder
dumpbin.exe
link.exe
lib.exe
mspdb80.dll

If you copy mspdb80.dll to the bin folder of VC then the compiler will say:
"c1902 program database manager mismatch please check your installation"
so the files have to be combined in a seperate folder

Now the problem is the calling convemtion
http://support.microsoft.com/kb/131313
"Specifically, the functions need to have been declared to use the C calling convention."
I could simply add CDECL to every PB function that I have ever written, but thats a pain. They are all STDCALL so I would like to use them that way.


Now I need to create a .Lib file using the stdcall convention.
http://www.geocities.com/yongweiwu/stdcall.htm

writing the def file like this;
EXPORTS
__stdcallAddTwoNum
__stdcallLIBMAIN
__stdcallShowString


This creates a .lib file but when I try to compile my VS2008 C++ project I get the linking error:

main.obj : error LNK2019: unresolved external symbol _AddTwoNum@12 referenced in function _main
C:\C++Projects\UseWrapDll\Debug\UseWrapDll.exe : fatal error LNK1120: 1 unresolved externals

somehow it is looking for a decorated function name now





Vortex

Hi MikeT,

Creating an import library with decorated function names is easy. You have the functions below :

AddTwoNum
LIBMAIN
ShowString


Now, the question is how many parameter takes each function. For example, ExitProcess takes one parameter :

1 parameter * 4 bytes per param = 4  => _ExitProcess@4  ( This is the decorated form. )

MessageBoxA takes four parameters :

4 parameters * 4 bytes per param = 16  => _MessageBoxA@16

AllocConsole : no any parameters :

0 parameter * 4 bytes per param = 0  => _AllocConsole@0

You can create import libraries with Polib coming with the Masm32 installation.

MikeT

Well isnt that handled by the function declaration in my cpp file?

extern "C" { // put Dll C function prototypes here
int __stdcall AddTwoNum(int n, double f); // __stdcall
}


Vortex

Hi MikeT,

You are right. Please could you post here the declaration of the other functions?

Vortex

Hi MikeT,

I could not find the source code of AddShow.dll in the zip file you posted here

Analyzing AddShow.dll, I found that :

AddTwoNum takes 3 parameters
ShowString : 4
and LIBMAIN takes 3 parameters.

Here is the module definition file :

LIBRARY AddShow
EXPORTS
"_AddTwoNum@12"
"_LIBMAIN@12"
"_ShowString@16"


Now, you can create an import library with decorated names :

polib /OUT:AddShow.lib /DEF:AddShow.def /MACHINE:IX86

or

def2lib.exe AddShow.def


In both cases, you should get a library of 1786 bytes. ( Tested with polib V6.00.1 and def2lib V1.1 )

MikeT

#23
Sorry I didn't think of that. Typically I will have at least a header file with the function definitions in it
Here is the AddShow.Dll code:
' A Dll to test C++ linking
#COMPILE DLL "AddShow.dll" 
#INCLUDE     "WIN32API.INC"
'¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
FUNCTION LIBMAIN SDECL ( BYVAL hInstance    AS LONG, _  ' main
                         BYVAL fwdReason    AS LONG, _
                         BYVAL lpvReserved  AS LONG     ) EXPORT AS LONG
    SELECT CASE fwdReason
        CASE %DLL_PROCESS_ATTACH 
            LIBMAIN = 1  ' DLL Attached successfully
        CASE %DLL_PROCESS_DETACH   '
    END SELECT
END FUNCTION
'¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
FUNCTION AddTwoNum SDECL ALIAS "AddTwoNum" ( BYVAL k    AS LONG , _
                                             BYVAL mpg  AS DOUBLE ) EXPORT AS LONG
    MSGBOX "k="+STR$(k) + ", mpg="+STR$(mpg),64,"DLL Values Received:"
  FUNCTION = k + CLNG(mpg)
END FUNCTION
'¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
FUNCTION ShowString SDECL ALIAS "ShowString" ( BYVAL k    AS LONG , _
                                               BYVAL mpg  AS DOUBLE , _
                                               BYVAL sPtr AS DWORD ) EXPORT AS LONG                     
  LOCAL pzStr AS ASCIIZ PTR                 
    pzStr = sPtr
    MSGBOX ">"+ @pzStr +"<",64,"DLL Display String of Len:" + STR$(LEN(@pzStr))
  FUNCTION = LEN(@pzStr)
END FUNCTION
'¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤


How did you create the .Def function names? (I need to automate that)
Why are decorated names required with stcall and not cdecl?

PBrennick

Hi Mike,

I can see where I reached the wrong conclusion. Sorry for my comment.

Paul
The GeneSys Project is available from:
The Repository or My crappy website

MikeT

No problem Paul. No offence taken.

Vortex, I suspect we are in different time zones (are you in Europe maybe?)
I ran polink with the switches you specified:
/OUT:AddShow.lib /DEF:AddShow.def /MACHINE:IX86

And I get:
POLINK: warning: No object files specified; libraries used.                     
POLINK: error: Unresolved external symbol '__AddTwoNum@12'.                     
POLINK: error: Unresolved external symbol '__LIBMAIN@12'.                       
POLINK: error: Unresolved external symbol '__ShowString@16'.                   
POLINK: error: Unresolved external symbol '___DllMainCRTStartup@12'.           
POLINK: fatal error: 4 unresolved external(s).


(I have v5.00 of polink installed with MASM yesterday)
I am guessing because the function names in the dll are not decorated?
where do you download polink v6?


Vortex

Hi MikeT,

I am on GMT + 3:00

I built the import library with polib Version 6.00.1  I copied this latest version to my \masm32\bin folder. My apologies, I should tell it you before.

You receive error messages because there are some extra underscore characters in your import library. For example, instead of __AddTwoNum@12, you should have _AddTwoNum@12 in your library ( only one leading underscore )

The function names in the DLL should not be decorated here.

You can get the latest Pelles tools from :

http://www.smorgasbordet.com/pellesc/download.htm

I created manually the def file. I understand that you need to automate this task. Does the Power Basic compiler create an object file for the DLL? It's possible to convert MS COFF object modules to def files.

Attached is the import library created with polib V6.00.1



MikeT

#27
Success!
Thank you for all this help! This is a minefield!!

Does the Power Basic compiler create an object file for the DLL?
ha ha ha... powerbasic can't even get the date right it seems.

I downloaded and installed Pelle's C to get polib. I think his work is great. What an amazing contribution. Hutch too.
I explored Pelle's C for while a couple of years ago but the reality is I need OOP classes to work on team projects in industry.

polib now works as you describe

>The function names in the DLL should not be decorated here.
right

>I created manually the def file
So I need to code this procedure because I have MANY Dlls to convert and I want to automate this whole process. If I am following the subtext correctly, I *must* use decorated names if I use the stdcall calling convention yes?
In which case, is there any code that I can use (written in any language) that creates decorated names?


MichaelW

#28
Within my experience not all STDCALL DLLs use decorated names. A quick way to determine what sort of names a DLL, library, or object module contains is to open the file in an editor and take a look. Most of what you see will be gibberish, but the names should be readable.

Edit: "Use" is the wrong word, that should have been:

Within my experience not all STDCALL DLLs contain decorated names (and now that I check, most don't).

And if the target DLL does not, then I can't see any way to fully automate the creation of an import library.
eschew obfuscation

MikeT

Well this is where I remain confused. My Dll does not use decorated names yet the project that calls it will only link if decorated names are used.
I am guessing this is a condition C++ imposes?

Hutch, I see you dealt with this question in 2005
http://www.powerbasic.com/support/forums/Forum4/HTML/012129.html

Vortex, how did you know how to decorate the names
"_AddTwoNum@12"
"_LIBMAIN@12"
"_ShowString@16"


why not

"_AddTwoNum@1"
"_LIBMAIN@2"
"_ShowString@3"

(using the ordinal from dumpbin)

for example?