Link to home
Start Free TrialLog in
Avatar of sa9813
sa9813

asked on

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);

}
ASKER CERTIFIED SOLUTION
Avatar of basant
basant

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sa9813
sa9813

ASKER

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 );
}
(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.

Avatar of sa9813

ASKER

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.
Any thing more if u want I can provide the help.
Avatar of sa9813

ASKER

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!
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.