Static and Dynamic DLL loading [Sample Code]

Source: Internet
Author: User

1. dllSource code

Mydll. h

[CPP] View plaincopyprint?
    1. //////////////////////// //////////////////////////////////////// /// //
    2. // mydll. H
    3. // declare a function
    4. int _ stdcall add ( int , int B);
    5. int _ stdcall sub ( int , int B);

//////////////////////////////////////// /// // <Br/> // mydll. h <br/> // declare the function <br/> int _ stdcall add (int A, int B); <br/> int _ stdcall sub (int, int B );

 

Mydll. cpp

[CPP] View plaincopyprint?
  1. //////////////////////////////////////// //////////////////////////////////
  2. // Mydll. PP
  3. // Declaration implementation
  4. # Include "mydll. H"
  5. Int_ Stdcall add (IntA,IntB)
  6. {
  7. ReturnA + B;
  8. }
  9. Int_ Stdcall sub (IntA,IntB)
  10. {
  11. ReturnA-B;
  12. }

//////////////////////////////////////// /// // <Br/> // mydll. PP <br/> // Declaration implementation <br/> # include "mydll. H "<br/> int _ stdcall add (int A, int B) <br/>{< br/> return A + B; <br/>}< br/> int _ stdcall sub (int A, int B) <br/>{< br/> return a-B; <br/>}

 

Mydll. Def

[CPP] View plaincopyprint?
    1. Mydll is the project name
    2. Library mydll
    3. Declare the function to be exported here
    4. Exports
    5. Add
    6. Sub

; Mydll is the project name <br/> library mydll <br/>; declare the function to be exported here <br/> exports <br/> Add <br/> sub

 

2. EXE TestingCode

Demonstrate the dynamic and static loading methods. Check the code!

[CPP] View plaincopyprint?
  1. VoidCtestdlg: onbtnstatic ()
  2. {
  3. // Todo: add your control notification handler code here
  4. // Static Loading Method:
  5. // 1. Add the header file # include "mydll. H"
  6. // 2. Introduce the Lib library # pragma comment (Lib, "mydll. lib ")
  7. // 3. You can directly use the function imported in mydll. h.
  8. Cstring STR;
  9. Str. Format ("Static Loading: 1 + 1 = % d 1-1 = % d", Add (1, 1), sub (1, 1 ));
  10. MessageBox (STR );
  11. }
  12. VoidCtestdlg: onbtndynamic ()
  13. {
  14. // Todo: add your control notification handler code here
  15. // Dynamic Loading Method:
  16. // The header file and Lib file do not need to be introduced. Only one DLL is required.
  17. // Note the Treaty call conventions here _ stdcall do not forget to add (otherwise ESP errors will be introduced)
  18. Typedef Int(_ Stdcall * addproc )(Int,Int);
  19. Typedef Int(_ Stdcall * subproc )(Int,Int);
  20. HinstanceHandle;
  21. Handle = loadlibrary ("Mydll. dll");
  22. If(Handle)
  23. {
  24. // The second getprocaddress parameter has two methods:
  25. // 1. Use the function name in the DLL
  26. // 2. Check the ordinal index value in the depend tool.
  27. Addproc myadd = (addproc) getprocaddress (handle,"Add");
  28. Subproc mysub = (addproc) getprocaddress (handle, makeintresource (2 ));
  29. If(! Myadd)
  30. {
  31. MessageBox ("An error occurred while obtaining the function add address! ");
  32. Return;
  33. }
  34. If(! Mysub)
  35. {
  36. MessageBox ("An error occurred while obtaining the Function Sub address! ");
  37. Return;
  38. }
  39. Cstring STR;
  40. Str. Format ("Dynamic Loading: 1 + 1 = % d 1-1 = % d", Myadd (1, 1), mysub (1, 1 ));
  41. MessageBox (STR );
  42. }
  43. Freelibrary (handle );
  44. }

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.