• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 193
  • Last Modified:

Simple Questions About DLL's

Hi All...

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

Tanks!
0
Snurre
Asked:
Snurre
  • 3
  • 2
  • 2
1 Solution
 
SalteCommented:
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.

Alf
0
 
SalteCommented:
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".

Alf
0
 
SnurreAuthor Commented:
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)
0
Independent Software Vendors: 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!

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

becomes

__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)
{
   lpfnMySquareFunction=(LPFNDLLSQUARE)GetProcAddress(hDLL,
                                           "square");
   if (!lpfnMySquareFunction)
   {
      // handle the error
      FreeLibrary(hDLL);      
      return SOME_ERROR_CODE;
   }
   else
   {
      // 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.)
0
 
SnurreAuthor Commented:
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??
0
 
wearyCommented:
>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
0
 
SnurreAuthor Commented:
Thanks weary... That did the trick :o)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

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

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now