DLL File Construction(32)

I am attempting to put together a 32-bit DLL File under Windows 95 and further.  In attempting, I am also learning how it is put together.  I have also searched the Internet, also Microsoft's pages, and have not come up with an understandable way of learning how a DLL File is together.
So, I am asking one of you Windows Programmers(C) Wizards, for some solid source code examples of HOW a 32-bit DLL File is assembled together.  In the source code examples, please include the following things, with some comments:
 
  1) what includes files are needed.
  2) examples of functions declarations.
  3) the correct DLLMain set up.
  4) write some functions that pass the following data            types, back and forth from a calling program(*) to the       DLL function:
    A) character data types.  
    B) numerical(int and long) data types,
        pass them by value and by reference.
  5) work 2) and 4) together.
  6) include what other code is necessary, for efficient DLL       File performance.

  Thank you,  Your assistance will surely be appreciated.
 
  NOTE: (*) under and using MS WIndows platforms: e.g.
             MS VB, Delphi, and etc.

  Tom Thorpe  -  MidnightExpress

  email: methorpe@burgoyne.com
  RSVP Please.
midnightexpressAsked:
Who is Participating?
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.

midnightexpressAuthor Commented:
Edited text of question
0
midnightexpressAuthor Commented:
Edited text of question
0
nietodCommented:
Answer coming
0

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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

nietodCommented:
here is a starter dll.

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define DllImp __declspec(dllimport)
#define DllExp __declspec(dllexport)

bool          // true if library initialized okay.  false if   //
              // there was an error.  Used only 1st time       //
              // procedure is called for a process.            //
WINAPI DllMain(HINSTANCE DLLHnd, // >> DLL module.                                //
               ULONG     Rsn,    // Reason for calling function.                  //
               LPVOID    Rsv)    // Reserved.                                     //
{
   switch (Rsn)
   {
   case DLL_PROCESS_ATTACH:
      break;
   case DLL_PROCESS_DETACH:
      break;
   case DLL_THREAD_ATTACH:
      break;
   case DLL_THREAD_DETACH:
      break;
   }
   return true; // always return true even though only used on process attach.
}

you don't need the windows_mean_and_lean, but it makes things compile faster as it prevents inclusion of lots of the rarely used stuff.

I put in a dummy switch statemend for the DllMain's 4 calling reasons.  You may not need all or any cases.  DllMain must return true when a process attaches or the OS will consider it an error and unload the dll.  The return vlaue is ignored in all other cases.  

when a process attaches to the Dll it is rally the process's primary thread that is being attached, however there is no THREAD_ATTACH call, just the PROCESS_ATTACH call, so often PROCESS_ATTACH case just falls into the THREAD_ATTACH.  The same is true for detaching.

more coming.

0
nietodCommented:
As for example Dll functions, they are really no different than regular functions except for the use of __declspec(dllexport).

for example you might have a function to increment an integer.  In a header file (include file, .h file, whatever you call it.) you would declare it like

__declspec(dllexport) int Increment(int i);

In the Dll you would define it like

__declspec(dllexport) int Increment(int i)
{
   return i + 1;
}

that's all there is to it.  Pointers, characters, floatinpoints, references, all work the way you expect.  There are no hidden pitfalls.  You just need to tack on the __declspec() and it works.  

There is no extra work on the other end (an exe or dll that uses the Dll you are writing.)  It just includes the header file, sees the declspec() and you are done.  

There is one extra step not that I think about it.  the exe/dll that uses your dll, must link with your dll's export library.  when you create the dll you will get a .lib file as well as the .dll.  anything that impliciltly links with the dll will need to link with the .lib file.  

In VC this can be done one of two ways.  If the dll and the exe/dll that use it are both sub-projects in the same project then the exe/dll must be set to be dependant on the dll.  If the exe/dll that uses the dll will be in a seperate project from the dll you are writting, then add the .lib file to the exe/dll project.

I hope that's clear.  Let me know if you need more details in this area.

As for you last question.  There not alot you can do to make it more or less efficient.  there is a little extra overhead to making Dll calls compared to regular calls, but you can't do anything about it.  There is also overhead that occurs when loadins the dll. Realistically you can't do much about that.  The less functions you have and the shorter there names are the faster it will load.  But this overhead occurs only when the DLL loads, so its not really worth worying about.  Also if you have multiple dll's you can make sure each one gets a different base address.  This will speed up load time a little.  However, after load time there's not much you can do to effect efficiency that would be different than a regular program.  i.e. use good algorytms.

Hope this gets you started.  I'll check in tommorrow morning if you have more questions.
0
nietodCommented:
Opps!  Rereading for the third time I see you mentions Vissual basic.  I could have sworn you said Vissual c.  Sorry.  I hope this is of some help anyways.
0
midnightexpressAuthor Commented:
To: Nietod:    Feb 7, 1998
 
 Thanks for so far.  I will use this template and compile it into a DLL in MS VC++ and then it under MS VB. But for right now, can you do a rough draft on the following?:
   Question 4) write some functions that pass the following data
      types, back and forth from a calling program(*) to the
               DLL function:
       A) character data types.
       B) numerical(int and long) data types,
             pass them by value and by reference.


 And then I'll get back to you.  What is your email for some feedback?

  Thom Thorpe - methorpe@burgoyne.com   MidnightExpress.
 
0
nietodCommented:
1)  Is C++ okay?  I don't "do" basic.  (I didn't expect you to accept my answer when I realized you wanted basic.)

2)  There is really no need for you to see example dll procedures.  They are declared 100% the same as ussual except you put _declspec(dllexport) in front of them.  i.e. in the example I wrote above, you just change the parameters/return values from int's to whatever you want and it will work.  There's no special handling for parameters or return values.  With that in mind, if you still want some examples, list a few here possibilities here and I'll fill then in tomorrow, maybe tonight. (i.e. list a few functions you want to stick in a dll.)

3)  My e-mail is nietod@theshop.net, but you can post comments here and I will respond.

0
nietodCommented:
It occured to me lastnight that you might be looking for examples where a C++ DLL is used by a Basic EXE.  Is that why you wanted to see the examples with different parameters?
0
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.