Solved

DLL File Construction(32)

Posted on 1998-02-06
9
214 Views
Last Modified: 2013-12-03
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.
0
Comment
Question by:midnightexpress
  • 6
  • 3
9 Comments
 

Author Comment

by:midnightexpress
ID: 1410962
Edited text of question
0
 

Author Comment

by:midnightexpress
ID: 1410963
Edited text of question
0
 
LVL 22

Accepted Solution

by:
nietod earned 120 total points
ID: 1410964
Answer coming
0
 
LVL 22

Expert Comment

by:nietod
ID: 1410965
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 22

Expert Comment

by:nietod
ID: 1410966
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
 
LVL 22

Expert Comment

by:nietod
ID: 1410967
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
 

Author Comment

by:midnightexpress
ID: 1410968
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
 
LVL 22

Expert Comment

by:nietod
ID: 1410969
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
 
LVL 22

Expert Comment

by:nietod
ID: 1410970
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now