NO

Author Topic: Using a dll  (Read 7826 times)

rob

  • Guest
Using a dll
« on: September 04, 2007, 04:44:38 PM »
Can someone explain how to use a dll?  I made a win32 program and I put some functions in a dll (using the dll wizard), but I'm having troubles finding information on how to use the dll.

Diddl

  • Guest
Re: Using a dll
« Reply #1 on: September 04, 2007, 06:23:06 PM »
a DLL is only like a normal lib, but it is loaded at runtime. so your .exe is smaller. if many .exe files uses DLL the code in the DLL is loaded once, so you need less space on harddisc and less amount off memory.

there are win32 functions to load DLL and get pointers to functions. but this is the diffilcult way ...

you can use import lib, which is automatically created by a tool (implib or so). and this lib does all work to make it easy to use a DLL (load and unload of DLL, interface to functions). for your source all functions in the DLL are available like any other function. if you call a function the DLL is automatically loaded if nessecary. so you only need a .h file for prototypes and this importlib.

rob

  • Guest
Re: Using a dll
« Reply #2 on: September 04, 2007, 06:39:23 PM »
Can you give some sample code on how to do that?  Thanks for the info.

codamateur

  • Guest
Re: Using a dll
« Reply #3 on: September 05, 2007, 12:40:30 AM »
Assuming you have created a DLL that EXPORTS a bunch of funtions and you want to use these functions
in a program without having to include the code for these functions..

1) you have to declare prototypes for your functions:
For example, int my_function1(int , char) is such declaration

2) you need a .lib file to make the linker capable to create the IMPORT table of the program (not the dll)

To create such file you need to edit a .def file:

For example, here is a such file i have been using to create my own crt.lib file to compile a program:

LIBRARY CRTDLL
EXPORTS
malloc
menset
free
fopen
fclose
fread
fscanf
sprintf

comments:
first line  is the name of the dll that exports the functions wich names are following
you need to replace "CRTDLL" string by "MY_DLL" if your dll is named "MY_DLL" (better to use upper case characters)

words "LIBRARY"  and "EXPORTS" are reserved words of polib.exe, the program needed to create the .lib file (it's a program included in Pelles-C package)

To create a .lib file lauching polib.exe from console box execute:

polib.exe /DEF:myfile.def /OUT:mylib.lib

To compile your project using the GUI of Pelles-C you'll need to change options of this project to tell linker to take into account the .lib created (corresponding to your dll functions you want to use dynamically in your program)

in the GUI: project/project options/linker and add your lib in the first line that shows a list of .lib (and eventually .obj). No need to change the last line, it's updated automatically it seems.

Speaking about polib, i'm a dirty coder, i work generally on small projects, i don't need, most of the time, to create a makefile but if someone could explain me the syntax to put in the makefile commands to use polib to create a .lib you're welcome ;)





codamateur

  • Guest
Re: Using a dll
« Reply #4 on: September 05, 2007, 12:59:49 AM »
/MESSAGE has been modified to fix errors/

By the way, i have already noticed crt.lib is included in Pelles-C package ;) BUT...

I suspect this crt.lib to replace all C functions by code that uses only Kernel32 functions only.

To make tiny programs (i m "a small program" maniac) i use my own crt.lib to force the linker to not add code.

rob: you can import functions, not only by names but also by ordinal.
it can be useful to decrease size of your programs, to make the work of code-reverser guys more complex, to speed up the loading of a program.

Using ordinal is not always a good idea cause' compatibility problems (a new version of a dll could not using same ordinals for functions) BUT if you are using YOUR own DLL, it's OK. You'll need to keep same ordinal for your functions in the new versions of your DLL and all is fine.

ordinal is a number.

to use ordinal you need to change your .def file:


LIBRARY CRTDLL
EXPORTS
my_functA @1
my_functB @2
my_functC @3


Notice the SPACE and the following @, the number following is the ordinal of the corresponding function (this value and THE NAME OF FUNCTIONS HAVE TO BE  the same as shown in the DLL binaries)

the order of functions is important: they need to be sorted in alphabetical order
« Last Edit: September 05, 2007, 01:26:17 PM by codamateur »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Using a dll
« Reply #5 on: September 05, 2007, 08:07:27 AM »
Is ordinal last in line like @1, separated with space ?
Does func@4 means stdcall function with one parameter ?

« Last Edit: September 05, 2007, 08:43:57 AM by timovjl »
May the source be with you

codamateur

  • Guest
Re: Using a dll
« Reply #6 on: September 05, 2007, 12:18:39 PM »
timovjl:
using tdump (borland tool) with your DLL i got:


Exports from TestDll.dll
  1 exported name(s), 1 export addresse(s).  Ordinal base is 1.
    Ordinal RVA       Name
    ------- --------  ----
    0000    00001020  _SampleFunction@8


Bad compiled DLL?

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: Using a dll
« Reply #7 on: September 05, 2007, 12:53:18 PM »
Look it with PEView:
http://www.magma.ca/~wjr
You can compile it again if you think so.
May the source be with you

codamateur

  • Guest
Re: Using a dll
« Reply #8 on: September 05, 2007, 01:05:52 PM »
I understand now. Prototype used to create a function in a DLL and the prototype used to use it in an another program make transparent the fact, the actual name of the function in the DLL bins isn't what i'm expecting to find.

To use the SampleFunction function without to use LoadLibrary thingy create a .def file to create testdll.lib :


LIBRARY TESTDLL
EXPORTS
_SampleFunction@8


and now you can use it to compile:



#include <stdio.h>

int SampleFunction(int a,int b);
void main(void)
{
printf("Result: %d",SampleFunction(5,2));
}




#project file/makefile (.ppj) :

POC_PROJECT_VERSION = 4.00#
POC_PROJECT_TYPE = 0#
POC_PROJECT_ARGUMENTS = #
POC_PROJECT_WORKPATH = #
POC_PROJECT_EXECUTOR = #
CC = pocc.exe#
AS = poasm.exe#
RC = porc.exe#
LINK = polink.exe#
CCFLAGS = -Tx86-coff -Ot -W1 -Ze -Gz#
ASFLAGS = -AIA32 -Gz#
RCFLAGS = -r#
LINKFLAGS =  -subsystem:console -machine:ix86  kernel32.lib user32.lib gdi32.lib comctl32.lib comdlg32.lib advapi32.lib delayimp.lib testdll.lib#
WizCreator = Pelle Orinius#

.SILENT:

dlltest.exe: \
   output\main.obj
   $(LINK) $(LINKFLAGS) -out:"$@" $**

output\main.obj: \
   main.c
   $(CC) $(CCFLAGS) "$!" -Fo"$@"

.EXCLUDEDFILES:




codamateur

  • Guest
Re: Using a dll
« Reply #9 on: September 05, 2007, 01:22:40 PM »
 I was WRONG in the syntax to export function by ordinal.

If you want to import by ordinal the function "SampleFunction" you have to use the .def file following:


LIBRARY TESTDLL
EXPORTS
_SampleFunction@8 @1

NOTICE the space between 8 and @
and recompile the test program above with the new testdll.lib

codamateur

  • Guest
Re: Using a dll
« Reply #10 on: September 05, 2007, 02:18:24 PM »
For fun, i have compiled :

#include <stdio.h>

int SampleFunction(int a,int b);
void mymain(void)
{
printf("Result: %d",SampleFunction(5,2));
}

to make it tiny...size=1024 bytes ;) (by default, size is 24k or so)

i have created a .lib  file containing a "mymain" function (containing simple code:

int mymain(void)
{
  return 0;
}
)

i have created a .lib to have a mycrt.lib (to take into account printf function)


i have used the TESTDLL.lib file and i have compiled the stuff (changing the makefile) to get a 1024 bytes file.


rob

  • Guest
Re: Using a dll
« Reply #11 on: September 05, 2007, 04:41:09 PM »
I created a test dll using the wizard and then at the top of my program I have the line:

#include "test.h" 

When I try calling SampleFunction I get the error:

POLINK: error: Unresolved external symbol '__imp__SampleFunction'.
POLINK: fatal error: 1 unresolved external(s).

How do I fix this?

rob

  • Guest
Re: Using a dll
« Reply #12 on: September 05, 2007, 04:54:38 PM »
Nevermind I found the answer.  I needed this line of code:
#pragma comment( lib, "test.lib" )

codamateur

  • Guest
Re: Using a dll
« Reply #13 on: September 05, 2007, 08:49:23 PM »
#pragma comment( lib, "test.lib" ) is the dirty way to add a lib into the makefile (project file) as explained above ;)