Simple Questions About DLL's

Posted on 2003-02-20
Medium Priority
Last Modified: 2011-08-18
Hi All...

Im woundering if anyone got some god tutorials on making DLL's and or could explain the basics

Question by:Snurre
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
LVL 12

Expert Comment

ID: 7987770
How to make DLLs differ from platform to platform. Using MSVC or Borland, they both have the option when you create a project you specify what kind of project you want to make.

You can typically select between:

Win32 Console application. (.EXE running console).
Win32 Application (.EXE making windows etc).
Win32 static library (.LIB)
Win32 dynamic library (.DLL).

Obviously you want to select the latter.

One problem is that they do not necessarily call it "Win32 Console application" etc....but they usually contain the keywords given above in one form or sequence or another.

LVL 12

Expert Comment

ID: 7987782
Also, be aware that low priced versions of the tools (MSVC studio or borland C++ builder) lack the ability to make all types of projects. Meaning it is possible you can't make a DLL using your present compiler but have to upgrade to a more expensive type. This is what microsoft and borland learned to do when they took the class "marketing and profit making 101 - or how to screw your customer and make him enjoy it".


Author Comment

ID: 7988351
OK Guys, i now all that, i need to now how to write DLL Code and how to call upon them in my Softwear... Some Tutorial would be great... :o)

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Expert Comment

ID: 7989866
Here's the ultra super basics for visual C++ as I remember.

First create a DLL project as mentioned above. In order for the function name be properly exported, the prototype of the function must be preceded by __declspec( dllexport ). You could also use a .DEF file which defines all your exports, but this isn't needed in this case. Thus the C prototype:

int square( int );


__declspec( dllexport ) int square( int );


Now that you have a DLL, you need to use it in an EXE or another DLL. You can do this by explicitly linking to a DLL or implicitly linking to the DLL.  I find explicit linking to be interesting as you can link dynamiclly to any dll you want at run time. Here is some code to import a method from a dll, and use the method. This is modified from MSDN

typedef int(CALLBACK* LPFNDLLSQUARE)(int);
HINSTANCE hDLL;               // Handle to DLL
LPFNDLLSQUARE lpfnMySquareFunction;    // Function pointer
int nParam1;
int nReturnVal;

hDLL = LoadLibrary("MyDLL.DLL");
if (hDLL != NULL)
   if (!lpfnMySquareFunction)
      // handle the error
      return SOME_ERROR_CODE;
      // call the function
      nReturnVal = lpfnMySquareFunction(nParam1);

The key is to define your function pointer correctly (match the function you are importing).  Then use LoadLibrary to load your DLL and GetProcAddress to get the address (function pointer) of the function you want to call.

That's it. That's the most basic dynamic DLL.  If I were you I would try using that code, but in the future look into using COM DLLs. COM DLLs use similar code under the hood, but they allow for passing full blown Objects around that are language independant (you can use them in VB, C++ etc. etc.)

Author Comment

ID: 7999287
OK weary, can i call classes too like this or?

And one question more, if i create a dll with graphics in it, in a resource for ex. with my bitmap class. can i draw the dynamicly created bitmap in the main window??

Accepted Solution

weary earned 80 total points
ID: 8012526
>OK weary, can i call classes too like this or?

>And one question more, if i create a dll with graphics in >it, in a resource for ex. with my bitmap class. can i >draw the dynamicly created bitmap in the main window??

Calling methods in classes gets a bit tough this way. You would likely need to keep a global instance, and have the dll method call the method on the class for you. Best to go with COM if you want to use objects etc.

In terms of resources, you can get any resources you have loaded using:

HRSRC FindResource(          HMODULE hModule,
    LPCTSTR lpName,
    LPCTSTR lpType
); //use the hmodule from the loadlibrary call

and then

HGLOBAL LoadResource(          HMODULE hModule,
    HRSRC hResInfo

This gets you a handle to the resource. voila

Author Comment

ID: 8018596
Thanks weary... That did the trick :o)

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

752 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