Categories

CODE
Visual C++ Programming
C++ (Non Visual C++ Issues)
Modern Windows Apps (Metro)
C++ and WinAPI
Managed C++ and C++/CLI
Visual C++ Bugs & Fixes
Graphics Programming
Multithreading
Network Programming
Driver Development
Visual Basic 6.0 Programming
Visual Basic .NET
Crystal Reports
C-Sharp Programming
Managed C++
Visual Basic .NET
ASP.NET
.NET Framework
ADO.NET
Windows Presentation Foundation (WPF) & XAML forum
Silverlight
.NET Installation and Configuration Issues
Java Programming
AJAX
Scripting - Client Side
Database
XML
Wireless/Mobile Development
Assembly
Scripting - Server Side (PHP, Perl, etc.)
Python
General Developer Topics
Project Planning, Design, and Management
Testers and Testing
Algorithms & Data Structures
General Discussion / Chit Chat
Announcements, Press Releases, & News
Feedback
Articles Suggestions
Testing Area
Programming Projects
C# Game(s) Project
Game Engine Project
C++ Coding Project
Project: Code War
Slow Chat: Talk with Microsoft Developer Teams
Slow Chat: Developing Multithreaded Applications
Slow Chat: C++0x
Slow Chat: Visual C++: Yesterday, Today, and Tomorrow
Directory Services
General Windows and DNA Programming
Windows OS Issues
Open Positions (Jobs)
Looking for Work
Visual Basic .NET FAQs
Visual Basic FAQs
CodeGuru Individual FAQs
CodeGuru Individual Visual Basic FAQs

Resources

Java Database
Linux
Coding
Mobile
Hardware
Software Development
Software Development
iOS,OS X
iOS,OS X
ORACLE
IBM DEVELOPER
IBM DEVELOPER
MSDN
MSDN


Tags

Visual C++ Programming

Ask questions about Windows programming with Visual C++ and help others by answering their questions.

How to import a function from a DLL


That's it...  Jaime    				  			

  				  					I guess, you are speaking about export: in your "def" file: //example  EXPORTS     ; Explicit exports can go here 	DllGetClassObject PRIVATE 	DllRegisterServer PRIVATE   Dmitriy, MCSE  				  			

  				  					No! if the subject of the question has the word "import" is about that what I was asking for. I am NOT doing a DLL so I DON'T want to make any exports. I just want to USE the functions from a DLL, so that I need to IMPORT them. In BC++ I did something like INT WINAPI _import FS_GetAuxCommMsg( long UnitNo, char *Buffer, long Length ); where GS_GetAux is a function from a DLL. I have to import it so that I am able to use it in my program and the linker can link by using the LIB file which has been added to the project.  So... my questions can be enumerated as follows:  1. How to declare the IMPORT in the Header file. Maybe something like _declspec(dllimport). 2. How to include the DLL file in my project. In BC++ I had to create a LIB file from the DLL and then add it to the project, so that the Linker can link the functions properly. How can I do it in VC++ 6.0  Now... that's it thanks in advance Jaime     				  			

  				  					Jaime, maybe you were not on the same wavelength as Dmitriy: You have to export your functions & classes for them to be imported elsewhere. Here is my latest DLL exporting CHook (this is "Hook.h":   #include "SetWindowsHookExDLLDeclSpec.h" // START EXPAND #ifndef _SET_WINDOWS_HOOK_EX_DLL_DECL_SPEC__H_ #define _SET_WINDOWS_HOOK_EX_DLL_DECL_SPEC__H_  #ifndef AFX_DLL 	#define SET_WINDOWS_HOOK_EX_DLL_DECL_SPEC __declspec( dllexport )  #else 	#define SET_WINDOWS_HOOK_EX_DLL_DECL_SPEC __declspec( dllimport )  #endif  #endif // _SET_WINDOWS_HOOK_EX_DLL_DECL_SPEC__H_ // END EXPAND   ///////////////////////////////////////////////////////////////////////////// // CHook window  class SET_WINDOWS_HOOK_EX_DLL_DECL_SPEC CHook { // whatever.... };      When the above is compiled in the DLL project, AFX_DLL is defined, and the class is exported.  When the above is included in the EXE, it is imported, so all you have to do is tell your linker to import the library (through project settings), or in code (#pragma comment lib libname).  Note that if you are importing the above in another DLL, you are going to have to change the decl spec define name to something more specific, or it will not be imported by the new dll because most likely the new dll has AFX_DLL defined (was that clear???).  An other approach is for you to do LoadLibrary, then GetProcAddress, but then you end up with ugly (in my opinion) typedefs for function pointer types, such as   typedef LRESULT (WINAPI *PFN_Hook)(int, WPARAM, LPARAM);  HINSTANCE hDLL = LoadLibrary("ShellProcDLL.dll"); ASSERT(NULL != hDLL);  PFN_Hook pHook = (PFN_Hook) ::GetProcAddress(hDLL, "CBTProc"); ASSERT(NULL != pHook);       				  			

  				  					I think the answer to my question is simpler than you have actually answered.. I only want to use a FUNCTION from a DLL not made by me... to be able to compile the code, I have include this in the header file:   int __declspec( dllimport ) IBE_WIN_ResetBoard();     where IBE_WIN_ResetBoard( ) is the function to be imported. The only problem to be solved now is how can the linker link that function with my project. The name of the DLL is T3IBEW95.DLL. I have included this file in project settings but linker doesn't recognize the format... how can I create the corresponding  LIB file? LIB files I could create with BC++ would have different format..  Thanks Jaime    				  			

  				  					There two ways to call DLL's exported functions: Implicit and Explicit.  Implicit linking requires lib file during link. This lib file is created when the DLL is built.  Therefore, if you don't have the lib file, you should consider explicit linking.  Explicit linking is done by you when you need the function (as opposed to Windows loading all DLLs that are implicitly linked when your program gets loaded). You call LoadLibrary() to find/load the DLL and GetProcAddress() to get the address of the function.  Example:  typedef int (TestFunction)(int); HINSTANCE hDll; TestFunction* pFunc; hDll = ::LoadLibrary("testdll.dll"); pFunc = (TestFunction*)::GetProcAddress(hDll, "FunctionName"); int result = (*pFunc)(100); /* call the function passing 100 as the argument */       				  			

  				  					Thanks!!! your hint was really useful... I have already solved the problem..  Jaime    				  			

  				  					Hello Sir,          I am a final year engineering student.I am writing a DLL for my project.Now I like to import these functions from my DLL (written in VC++) to a VC++ application.When I try to implicitly link the library with the apllication I get a linker error.I have declared all the functions to be exported in the dll as _stdcall.Please help me. Your's sincerely,          Vignesh    				  			

  				  					Hello  Here are steps for importing functions: In your DLL project you: 1. In stdafx.h add these lines: #define DllImport       __declspec( dllimport ) #define DllExport       __declspec( dllexport ) 2. In your project settings dialog you must add a macro which will be used as a flag. 'C/C++' tab, Category combo set to 'Preprocessor', in preprocessor definitions you add something like 'MY_FIRST_DLL'. It will be a 'signature' of your DLL. If your application includes more than one project, each DLL must have an unique 'signature' 3. In your DLL you create a header which will include declarations of functions you want to declare. This header must contain these definitions:   3a. #ifdef MY_FIRST_DLL //for header only 	#define MyFirstDllExport DllExport #else 	#define MyFirstDllExport DllImport #endif   3b. Declarations of functions or classes you want to export from your DLL. All declarations must be prefixed by the word 'MyFirstDllExport'  MyFirstDllExport void MyFunction4Export(); class MyFirstDllExport MyClassForExport { };    4. You implement you functions and classes in CPP file. It's not necessary to mention any 'import/export' stuff in CPP file - you just do not forget to "#include" header with declaration to CPP  5. That's pretty much it for DLL part. Now after compilation compiler will create a LIB file. To make sure everything is fine try to open thsi file in DevStudio and find names of functions you want to export. File is binary and names will be founded as substrings.   Now in EXE project which uses you DLL you: 1. Do the same changes in EXE's stdafx.h, i.e. add these lines: #define DllImport       __declspec( dllimport ) #define DllExport       __declspec( dllexport ) 2. In the place where you want to call functions from DLL you include a header file from DLL. I assume you have DLL in a separate directory. So, your include will look like #include "MyDLL/ExportedStuff.h" 'ExportedStuff.h' is a file mentioned in p.3 of prev section. 3. You write calls of these functions 4. Do not forget to link your EXE project with LIB generated during DLL compilation 5. Compile EXE project 6. Place DLL file in the same directory where EXE file is and run. That's it.  I use this method for years now, it doesn't need any DEF files and never gave me any troubles.           Good luck Please rate if you think this response was useful for you  				  			

  				  					Thank you for your kind reply and useful code.But I have written a win32 dll.I use a DEF file to export all functions.How can I import those functions implicitly in a MFC application.    				  			

  				  					Thank you for your kind reply and useful code.  - Your are welcome   But I have written a win32 dll.I use a DEF file to export all functions.How can I import those functions implicitly in a MFC application. - The mechanizm described in my prev post doesn't work for win32 dll? Did you try?  If you didn't try, do it.  If you tried and it doesn't work when I don't know. What's not working? Maybe we can fix it togever?    Good luck Please rate if you think this response was useful for you Have more questions? Feel free to ask.   				  			

  				  					I have written win32 dll with all the functions to be exported placed in a DEF file.Now I started to develop a VC++ MFC application linking the dll implicitly.I linked the LIB file by changing the project settings.But I get a "unresolved external error symbol" when I use a function from the dll.I have used the same declaration as present in the dll.    				  			

  				  					I have written win32 dll with all the functions to be exported placed in a DEF file - This DLL was written using Visual C++ or another compiler?      Good luck Please rate if you think this response was useful for you Have more questions? Feel free to ask.   				  			

  				  					Yes the dll was written using VC++.    				  			

  				  					Yes the dll was written using VC++. - Also I hope you have a source code? In this case why don't you use the method I gave you? Remove DEF from your DLL project, do what I described and recompile your DLL - it will work.   Good luck Please rate if you think this response was useful for you Have more questions? Feel free to ask.


Related Links

Dynamic-Link - Loading/Unloading Dlls
C2511 error
CDaoDatabase and ODBC problem
Stuck on ASCII function
Application not terminating
WINVER not defined
Can anyone help?
Preventing Tab Keypress from loosing focus when the focus is on a specific control
How to retrieve the path for linked OLE objects ?
replace double backslash with single backslash in char array
Having trouble with functions...
Building MSI problem
how to interpret an ASCII string to be a unsigned __int64
doubt about timestamping
what is byte alignment
how to create dll for my project