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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 );
}
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.

Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

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.
basantCommented:
Any thing more if u want I can provide the help.
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!
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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.