vc++6.0 creation and invocation of dynamic libraries (non-MFC DLLs)

Source: Internet
Author: User

Creation of non-MFC dynamic libraries ...

A lib.cpp, a lib.h.

/*lib.h*/  #ifndef lib_h  #define LIB_H    //declares the export function of add as DLL.  extern "C" int _declspec (dllexport) Add (int x,int y);  #endif  

You can also add def files

; Lib.def: Export DLL function         library Dlltestdef      Exports     add @ 1  

  

Calls to non-MFC dynamic libraries

1. Static Calls

Static call DLL dynamic library, the DLL and LIB files have been placed under the corresponding working path  #include <stdio. h>  #pragma comment (lib, "DllTEST.lib")//. Tells the compiler where to put the Lib file associated with the DLL.    extern "C" _declspec (dllimport) Add (int x,int y);//Declaration import Function    int main ()  {      int result=add (2,3);      printf ("2+3=%d \ n", result);            return 0;  }  

2. Dynamic invocation

Dynamic Call DLL dynamic-link library.  Just put the DLL in the appropriate working directory to  #include <stdio.h>  #include <windows.h>  // Defines a function pointer type that is the same as the add function parameter type and the return pointer type.  The  typedef int (*lpaddfun) (int,int) is then instantiated in the main function;//macro definition function pointer type    int main ()  {      hinstance hdll;//dll handle      Lpaddfun addfun;//Instantiates the function pointer type      hdll=loadlibrary ("./dlltest.dll") above, and//dynamically loads the DLL module and assigns the module handle of the DLL to hDLL.      if (hdll!=null)      {          addfun= (lpaddfun) GetProcAddress (hDLL, "add"),//The address of the Add function in the DLL module is given to Addfun          if ( Addfun!=null)          {              int result=addfun (2,3);//Addfun to invoke the Add function                            printf ("2+3=%d \ n", result);          }                    FreeLibrary (hdll);//unload DLL dynamic module      }         return 0;        

  

  

vc++6.0 creation and invocation of dynamic libraries (non-MFC DLLs)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.