Hi alex-rudenkiy,
Let's try to make a simple MS VC example using a Dll built with Masm. First of all, what you are trying to do is to create a dynamic link library and not a static library. A dynamic library's code can be shared among different processes. A static library will add code to your final executable. The content of the static library is not shared like Dllls during run-time.
A simple Dll exporting two functions :
.386
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
.code
LibMain PROC instance:DWORD,reason:DWORD,unused:DWORD
mov eax,1
ret
LibMain ENDP
sum PROC x:DWORD,y:DWORD
mov eax,x
add eax,y
ret
sum ENDP
StdOut PROC lpszText:DWORD
LOCAL hOutPut :DWORD
LOCAL bWritten :DWORD
LOCAL sl :DWORD
invoke GetStdHandle,STD_OUTPUT_HANDLE
mov hOutPut, eax
invoke lstrlen,lpszText
mov sl,eax
invoke WriteFile,hOutPut,lpszText,\
sl,ADDR bWritten,NULL
mov eax, bWritten
ret
StdOut ENDP
END LibMain
The module definition file :LIBRARY sample
EXPORTS
StdOut
sum
Notice that you don't need always to decorate the exported functions.
Building the Dll :
\masm32\bin\ml /c /coff Sample.asm
\masm32\bin\polink /SUBSYSTEM:WINDOWS /DLL /DEF:Sample.def Sample.obj
\masm32\bin\dumpbin /EXPORTS Sample.lib > ImplibExports.txt
\masm32\bin\dumpbin /EXPORTS Sample.dll > DllExports.txt
You can read the text files to inspect the decoration of the exported functions.
Calling the two functions from a VC++ project :
#include <stdio.h>
extern "C" {
void __stdcall StdOut(char *string);
int __stdcall sum(int x,int y);
}
int main()
{
int a=90;
int b=10;
int c=sum(a,b);
StdOut("90 + 10 = ");
printf("%d",c);
return 0;
}
The StdOut function above is for demonstration purpose. Naturally, printf provides much more functionality.
Building the executable :
cl /FaTest.asm Test.cpp sample.lib
The assembly listing shows that we declare two external symbols, our Dll functions :
.
.
EXTRN _StdOut@4:PROC
EXTRN _sum@8:PROC
I used Visual Studio 2010 Express to compile Test.cpp