现在的位置: 首页 > 综合 > 正文

通过函数指针动态调用 dll 中的函数

2014年01月29日 ⁄ 综合 ⁄ 共 959字 ⁄ 字号 评论关闭

/******************************************************************/
///name       : GetKey
//function    : 校验密码
//access      : public
//para        :
//         1. : const std::string &strdata
//            : 参与密码校验的值
//return      : 校验后值
//author      : hzh
//date        : 2005-04-21
/*******************************************************************/
const std::string GetKey(const std::string &strdata)

 std::string result = "";
 if(strdata == "")
 {
  return result;
 }

 //动态加载 dll,并调用里面的 函数
 HINSTANCE DllInst = LoadLibrary("codekey.dll" );
 if( DllInst == NULL )
 {
  throw std::exception( "无法引导动态库 codekey.dll" );
 }
 
 //函数指针 CodeKey,保证和 dll 中需要调用的函数申明一致
 char * ( __stdcall * CodeKey)(const char *) = NULL;
 try
 {
  //指向 dll 中函数地址
  CodeKey = (char* ( __stdcall* )(const char * ))
   GetProcAddress(DllInst,"CodeKey");
  if(CodeKey)
  {
   //函数指针调用 dll 函数
   result = CodeKey(strdata.c_str());
  }
  else
  {
   throw std::exception("编译密码出错");
  }
 }
 catch( ... )
 {
  throw std::exception( "调用动态库codekey.dll出错!" );
 }
 //释放库
 FreeLibrary(DllInst);

 return result;
}

抱歉!评论已关闭.