News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

how to access an integer variable in dll

Started by markallyn, December 31, 2017, 06:17:06 AM

Previous topic - Next topic

markallyn

...But, to actually link to the function (mult2ints), the EXTERNDEF mult2ints:PROC declaration is not sufficient.  One also has to explicitly link to the function in the .dll. And, it appears that the mult2ints symbol in the .dll must be declared PUBLIC in the .dll.  It's a nuisance, but it works.  Much more efficient to use the mult2ints PROTO declaration.

Mark Allyn

markallyn

Hello everyone,

Well, it turns out you can use EXTERNDEF to define an imported function from a DLL.  In the following code I import a data item (myint) and a function (mult2ints) from a DLL (MyMathFuncs.dll):

Quote
nclude \masm32\include64\masm64rt.inc
includelib   \masm32\mystuff\mymathfuncs.lib

add2ints PROTO :QWORD, :QWORD
;mult2ints PROTO :QWORD, :QWORD
printf   PROTO :QWORD, :VARARG

EXTERNDEF   __imp_myint:QWORD
myint   TEXTEQU <__imp_myint>
EXTERNDEF   __imp_mult2ints:PROC
mult2ints TEXTEQU <__imp_mult2ints>

.data
int1   QWORD   10h
int2    QWORD   30h
frmt1   BYTE   "The result of the addition is %d",13,10,0
frmt2   BYTE   "The result of the multiplication is %d",13,10,0
strname BYTE   "mymathfuncs",0
dataname BYTE   "myint",0
funcname BYTE   "mult2ints",0

.data?
libname   QWORD   ?
;mult2ints   QWORD   ?
.code
main   PROC   public


   mov   rcx, myint
   mov   rcx, qword ptr[rcx]
   mov   rdx, int1
   invoke   add2ints, rcx, rdx
   mov   rdx, rax
   invoke   printf, ADDR frmt1, rdx
   


   
   mov   rax, mult2ints
   mov   rdx, int1
   mov   rcx, myint
   mov   rcx, qword ptr[rcx]
   call    qword ptr[rax]
   mov   rdx, rax
   invoke   printf, ADDR frmt2, rdx
   

   ret
main   ENDP
END



So, LoadLibrary isn't necessary to get the .dll function imported.  CALLing its address is sufficient.

Regards,
Mark Allyn

six_L

hi,markallyn
QuoteSo, LoadLibrary isn't necessary to get the .dll function imported.  CALLing its address is sufficient.
you'v not understood the difference of "static library" and "dynamic library".
QuoteIf you program long enough, you'll find that the programs you wrote usually have some code routines in common. It's such a waste of time to rewrite them everytime you start coding new programs.
A dynamic link library is a kind of common pool of functions. Windows will not load several copies of a DLL into memory so even if there are many instances of your program running at the same time, there'll be only one copy of the DLL that program uses in memory. And I should clarify this point a bit. In reality, all processes that use the same dll will have their own copies of that dll. It will look like there are many copies of the DLL in memory. But in reality, Windows does it magic with paging and all processes share the same DLL code.So in physical memory, there is only one copy of DLL code. However, each process will have its own unique data section of the DLL.
The program links to a DLL at runtime unlike the static library. That's why it's called dynamic link library. You can also unload a DLL at runtime as well when you don't need it. If that program is the only one that uses the DLL, it'll be unloaded from memory immediately. But if the DLL is still used by some other program, the DLL remains in memory until the last program that uses its service unloads it.
However, the linker has a more difficult job when it performs address fixups for the final executable file. Since it cannot "extract" the functions and insert them into the final executable file, somehow it must store enough information about the DLL and functions into the final execuable file for it to be able to locate and load the correct DLL at runtime.
That's where import library comes in. An import library contains the information about the DLL it represents. The linker can extract the info it needs from the import libraries and stuff it into the executable file. When Windows loader loads the program into memory, it sees that the program links to a DLL so it searches for that DLL and maps it into the address space of the process as well and performs the address fixups for the calls to the functions in the DLL.
You may choose to load the DLL yourself without relying on Windows loader.
Say you, Say me, Say the codes together for ever.

markallyn

Good morning, six_L,

1.  What is it you think I don't understand about the difference between "static" and "dynamic"?

2.  Where did the quotation originate?

Regards,
Mark