dynamic linking

Can somebody please explain to me how to use a dynamic link library, using C. This code I have just got from exsamles in my helpfiles, it does not work very well! :o) I am aware of the concept of dynamic linking...but the practical part hmm..
How would the following be done? Calling a function in a .dll displaying a message sent by a calling application?

#include <windows.h>

VOID myPuts(LPTSTR);

void main(){

   char dllName[] = "test";
   HINSTANCE myInstance;
   FARPROC myProc;

   myInstance = LoadLibrary( dllName );
   myProc = GetProcAddress( myInstance, "myPuts" );

   myProc("message printed using the DLL function\n");
   getch();
}


#include <windows.h>

 VOID _export myPuts(LPTSTR lpszMsg)
{
    DWORD cchWritten;
    HANDLE hStdout;
 
    // Get a handle to the standard output device.
 
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);

    // Write a null-terminated string to the standard output device.

    while (*lpszMsg)
        WriteFile(hStdout, lpszMsg++, 1, &cchWritten, NULL);

}
sa9813Asked:
Who is Participating?
 
basantCommented:
Practical Problems with First time DLL
writers :
1. Compile Time Problem :

void main(){

   char dllName[] = "test";
   HINSTANCE myInstance;
   FARPROC myProc;
   
/* Note you have to define the right defintion of function say
your function is int myfunc(float f);*/
typedef int (*myFuncType)(float f);
   MyFuncType myProc;


   myInstance = LoadLibrary( dllName );
   // Check to see whethere lbirary is opened or not
   if( myIns tance == 0 )
       {
   // Error Message
}
   myProc = (myFuncType) GetProcAddress( myInstance, "myPuts" );

int n = myfunc( 2.5);  
getch();
}


Note the points
1. You have to cast the return FARPROC in
correct function syntax.
2. Always check whether library is opened or not . Library DLL should be
avaialable in one of the following paths :
(1) Where executable lies
(2) Windows Directory
(3) Windows System Directory.
(4) Current Working Directory
... and some more way

Timebeing you can put ur DLL in the same directory in which the executable lies.


DLL issues :

DLL function should be properly exported there are many ways to do it .
Among them 2 are :
(1) Use *.def files

one.def file COntent
Library one
Exports
   myfunc @1

Note one.def should be included in the DLL project.

(2) The other ways is

one.cpp of DLL file

__declspec( dllexport) int myputs(float f)
{
// Body of the Function
}


(3) Use compile time options . I am not goint in to that detail now.



To Check Whether the DLL exports the
function properly use the dumpbin.exe utility like this
dumpbin /exports one.dll

THis will show all the functions which are exported from DLL. if some thing wrong happens here then you will not see your functions exported.


That's all for now.
Try this and tell if any other problems are there.
0
 
sa9813Author Commented:
Thank you for your time! I need some time to evaluate this...I have tried to put together a .def-file, it complains about the @ in

Exports
    myPuts @1

I have imported it to the .dll project, but it does'nt work...this is what I've got

#include <windows.h>
#include <conio.h>

//is this correct?
typedef void( *myFuncType )( float f );


void main(){

   char dllName[] = "test";
   HINSTANCE myInstance;
   FARPROC myProc;

   myInstance = LoadLibrary( dllName );

   if( myInstance == 0 )
       printf( "fel" );

   myProc = ( myFuncType ) GetProcAddress( myInstance, "myPuts" );
   myProc( 1.0 );
   getch();
}


#include <windows.h>
#include "test.def"

int myPuts( float f )
{
    //just return something
    return ( 5 );
}
0
 
basantCommented:
(1) Have u tried dumpbin.exe utility.
Show me the output of
dumpbin /exports one.dll

(2) R u be able to load library successfully.
   if( myInstance == 0 )
       printf( "fel" );

(3) Check whether your GetProcAddress
is Successful or failing.



   myProc = ( myFuncType )GetProcAddress( myInstance, "myPuts" );
if( myProc == 0)
{
   printf("\n Error :");
   return;
}
   
(4) Is your DLL function in C++.
If yes then
use extern "C" for declartion of function.

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
sa9813Author Commented:
I have not got it up working yet, but I now know what it should look like, I think I can do the rest with the helpfile. Thanx.
0
 
basantCommented:
Any thing more if u want I can provide the help.
0
 
sa9813Author Commented:
Okey, basant I will study this information now, learn how it's suppose to work! I will ask you if I there is something I do not understand. I do not have dumpin.exe, maybe this is provided by microsoft, I'm using borland C++? I found a program ( provided by borland )that created a .def file, provided the .dll, compiler complained about it though! Strange!
0
 
basantCommented:
This is not dumpin.exe
this is dumpbin.exe

This shipped with VC look for the directory
C:\Program Files\Microsoft Visual Studio\VC98\Bin :

Note go to bin directory where VC is installed.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.