Notsosuperhero
April 21st, 2005, 12:02 PM
I want to start learning how to make DLLs but I can't find anything except stuff with MFC and I don't want to use MFC, so does anyone know any good tutorials or books on non-MFC DLLs?
|
Click to See Complete Forum and Search --> : DLLs Notsosuperhero April 21st, 2005, 12:02 PM I want to start learning how to make DLLs but I can't find anything except stuff with MFC and I don't want to use MFC, so does anyone know any good tutorials or books on non-MFC DLLs? Siddhartha April 21st, 2005, 12:12 PM I want to start learning how to make DLLs but I can't find anything except stuff with MFC and I don't want to use MFC, so does anyone know any good tutorials or books on non-MFC DLLs? This thread (http://www.codeguru.com/forum/showthread.php?p=1137109#post1137109) will give you info, sample and leads to a lot of online (MSDN) documents. A Codeguru FAQ (http://www.codeguru.com/forum/showthread.php?s=&threadid=231254) :thumb: NoHero April 21st, 2005, 12:27 PM There are two ways: 1. Common things If a application of yours uses a DLL you have written it must be in one of the following directories: The same path where the application is, or the same working directory In the system32 directory of your Windows In a path specified by the PATH environment variable The linker creates .lib files for you when building a DLL which help you to import the exported functions you have written more easily. Every DLL uses a DllMain function as entry point that should always return TRUE, otherwise a application startup will fail of because the DLL initialation failed.: BOOL WINAPI DllMain ( HANDLE hModule, DWORD dwReasonForCall, LPVOID lpvParam ) { return TRUE; } Check this (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/dllmain.asp) MSDN aricle for more details on the DllMain function. 2. Using .def files: You add a .def file to your project and ensure you give it to your linker as input. Then you write normal C style functions, and ensure you use the __stdcall calling convention otherwise old Visual Basic 6.0 won't be able to load it. Then you add a the names of the to-export functions to the def file under the EXPORTS section: The Code: // MyDll.lib int __stdcall foo( int x, int y ) { return x + y; } The .def file: LIBRARY MyDLL EXPORTS foo @ 1 The @ followed by a number beginning at 1, specified the ordinal export number. This number can also be used when loading a function dynamitcally. The linker will create the DLL for you and a .LIB file. When you want to use the library in another program of yours, you simplay define the proto-type function of your function (in the case above, do not forget extern "C" directive) and add the lib file to your linker.: // MyLIb.h #pragma once #ifdef __cplusplus extern "C" { #endif int __stdcall foo ( int x, int y ); #ifdef __cplusplus } #endif Simply import this header and use your DLL function.: // main.c of your application #include <stdio.h> #include "MyLib.h" #pragma comment(lib, "MyDll.lib") int main ( void ) { printf("%d\n", foo(1, 1)); } 3. Or by using __declspec( dllexport ) Using __declspec has following advantages: It also stores information on attributes and paramters the function takes, so you can easily export overloaded C++ functions. It allows you to export entire classes from your source code. It also may export global variables But also some drawbacks: The exported function gets decorated to hold the information of parameters. This decoration is C++ specific and so it's harder to be used by other Platforms such as Visual Basic or Delphi. When you need support for these platforms then you should avoid this option. Here are some examples from the MSDN on how to use the __declspec(dllexport) directive: // Example of the dllimport and dllexport class attributes __declspec( dllimport ) int i; __declspec( dllexport ) void func(); More readable: #define DllImport __declspec( dllimport ) #define DllExport __declspec( dllexport ) DllExport void func(); DllExport int i = 10; DllImport int j; DllExport int n; Exporting an entire class: #define DllExport __declspec( dllexport ) class DllExport C { int i; virtual int func( void ) { return 1; } }; Everything you have imported by using __declspec(dllexport) can be imported by using the __declspec(dllimport) directive: #include <stdio.h> #define DllImport __declspec( dllimport ) class DllImport C { virtual int func( void ); }; int main ( void ) { C c; return c.func(); } Just pick what you need for your applicaton... /I am tired now, and need to do something for school... I may fix this post if I find a bug ;) //is there a FAQ about? if not... here is one! Notsosuperhero April 21st, 2005, 12:45 PM Thanks you guys, MFC is taking over the world. :cry: NoHero April 21st, 2005, 12:48 PM MFC is taking over the world. :cry: It has already.... Now the .NET is kicking MFC's butt... ... but not in this forum! :D Andreas Masur April 22nd, 2005, 04:17 AM Dynamic-Link Libraries (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/dynamic_link_libraries.asp)... codeguru.com
Copyright Internet.com Inc., All Rights Reserved. |