Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Linked library
- //main.cpp
- #include <iostream>
- #define DECLDIR __declspec(dllexport)
- extern "C"
- {
- DECLDIR int Add( int a, int b ) {
- return( a + b );
- }
- DECLDIR int Subtract( int a, int b ) {
- return( a - b );
- }
- }
- //main.def
- LIBRARY "calc"
- EXPORTS
- Add @1
- Subtract @2
- //app.cpp
- #include <windows.h>
- typedef int (*funcAdd) (int, int);
- typedef int (*funcSubtract) (int, int);
- int main( ) {
- HINSTANCE hDLL = LoadLibrary(TEXT("calc.dll"));
- if (hDLL == NULL) {
- std::cout << "Failed to load library.\n";
- }
- else {
- funcAdd Add = (funcAdd)GetProcAddress(hDLL, "Add");
- funcSubtract Subtract = (funcSubtract)GetProcAddress(hDLL, "Subtract");
- if (Add)
- std::cout << "10+10=" << Add(10, 10) << std::endl;
- if (Subtract)
- std::cout << "50-10=" << Subtract(50, 10) << std::endl;
- FreeLibrary(hDLL);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement