​

 ​


  1. // The myPuts function writes a null-terminated string to  
  2. // the standard output device.  

  3. // The export mechanism used here is the __declspec(export)  
  4. // method supported by Microsoft Visual Studio, but any  
  5. // other export method supported by your development  
  6. // environment may be substituted.  


  7. #include <windows.h>  

  8. #define EOF (-1)  

  9. #ifdef __cplusplus    // If used by C++ code,   
  10. extern "C" {          // we need to export the C interface  
  11. #endif  

  12. __declspec(dllexportint __cdecl myPuts(LPTSTR lpszMsg) // __cdecl | __stdcall | __fastcall  
  13. {  
  14.     DWORD cchWritten;  
  15.     HANDLE hStdout;  
  16.     BOOL fRet;  

  17.     // Get a handle to the standard output device.  

  18.     hStdout = GetStdHandle(STD_OUTPUT_HANDLE);  
  19.     if (INVALID_HANDLE_VALUE == hStdout)  
  20.         return EOF;  

  21.     // Write a null-terminated string to the standard output device.  

  22.     while (*lpszMsg != '\0')  
  23.     {  
  24.         fRet = WriteFile(hStdout, lpszMsg, 1, &cchWritten, NULL);  
  25.         if( (FALSE == fRet) || (1 != cchWritten) )  
  26.             return EOF;  
  27.         lpszMsg++;  
  28.     }  

  29.     return 1;  
  30. }  

  31. #ifdef __cplusplus  
  32. }  
  33. #endif  

 


  1. // A simple program that uses LoadLibrary and   
  2. // GetProcAddress to access myPuts from Myputs.dll.   

  3. #include <stdio.h>   
  4. #include <windows.h>   

  5. typedef int (__cdecl *MYPROC)(LPTSTR); // __cdecl | __stdcall | __fastcall  

  6. VOID main(VOID)   
  7. {   
  8.     HINSTANCE hinstLib;   
  9.     MYPROC ProcAdd;   
  10.     BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;   

  11.     // Get a handle to the DLL module.  

  12.     hinstLib = LoadLibrary(TEXT("bin\\Myputs")); // 虽然 MSDN Library 说这里如果  
  13.                                                  // 指定了路径,要用 backslashes (\),  
  14.                                                  // 不要用 forward slashes (/),但  
  15.                                                  // 其实用二者都可以。  
  16.                                                  // 注:如果用 \,要用 \\。  

  17.     // If the handle is valid, try to get the function address.  

  18.     if (hinstLib != NULL)   
  19.     {   
  20.         ProcAdd = (MYPROC)GetProcAddress(hinstLib, "myPuts"); // __cdecl   : myPuts  
  21.                                                               // __stdcall : _myPuts@4  
  22.                                                               // __fastcall: @myPuts@4  

  23.         // If the function address is valid, call the function.  

  24.         if (NULL != ProcAdd)   
  25.         {  
  26.             fRunTimeLinkSuccess = TRUE;  
  27.             (ProcAdd) (TEXT("Message via DLL function\n"));   
  28.         }  

  29.         // Free the DLL module.  

  30.         fFreeResult = FreeLibrary(hinstLib);   
  31.     }   

  32.     // If unable to call the DLL function, use an alternative.  

  33.     if (! fRunTimeLinkSuccess)   
  34.         printf("Message via alternative method\n");   
  35. }  

 ​