DLL加載、獲取函數指針。

//Interface.h文件內容如下:

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#pragma once

#include <windows.h>

 

//function point demo

#define ISOURCOMPANYKEY "IsOurCompanyKey"

typedef INT (WINAPI* IsOurCompanyKey) (VOID);

 

 

class Bluebottle

{

 public:

     Bluebottle(LPCSTR szDllPath) : m_hModule(NULL)

    { 

          m_hModule = LoadLibraryA(szDllPath);

     }

      ~Bluebottle()

     {

           if(m_hModule)

                  FreeLibrary(m_hModule);

           m_hModule = NULL;

      }

      FARPROC GetFunc(LPCSTR szFuncName)

      {

            if(m_hModule)

           {

                  return GetProcAddress(m_hModule, szFuncName);

           }

           return NULL;

      }

protected:

       HMODULE m_hModule;

};

 

//調用過程如下

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <windows.h>

#include <tchar.h>

#include <stdio.h>

#include "Interface.h"

 

//dll name

#define DLLNAME _T("Verify.dll")

 

BOOL main(int argc, PCHAR argv[])

{

          _tprintf(_T("Test called dll's function./n"));

 

          INT32 uI32Ret = -1;

          Bluebottle PingZi(DLLNAME); 

          IsOurCompanyKey pfnOurKey = NULL;

 

          pfnOurKey = (IsOurCompanyKey)PingZi.GetFunc(ISOURCOMPANYKEY);

          if (NULL == pfnOurKey)

         {

              _tprintf(_T("get dll's functions point error."));

             return FALSE;

          }

 

          uI32Ret = pfnOurKey();

           _tprintf(_T("IsOurCompanyKey's ret code:%d/nOver"), uI32Ret);

 

          return TRUE;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章