这是我写的代码 为的是使用载入dll中的函数:) //======================================================== /** * @file DLLInterface.hpp * * 项目描述: 构造游戏引擎 * 文件描述: DLL函数接口类 * 适用平台: Windows/... * * 作者: ccsdu2004 * 电子邮件: ccsdu2004@yahoo.com.cn * 网址 : www.gaimo.net/ hi/csdn.net/ccsdu2004 * 创建日期: 2009-04-14 * 修改日期: .. * */ //======================================================== #ifndef G_LIB_DLLINTERFACE_HPP #define G_LIB_DLLINTERFACE_HPP #include <string> #include <windows.h> using namespace std; namespace g { namespace lib { /* 定义dll载入类 */ class DLLInterface { public: DLLInterface(const string &dllname); ~DLLInterface(); public: bool Reset(); bool Reset(const string &dllname); public: //返回函数的执行情况 bool Enquiry0(const string &funname); template<class output, class input> bool Enquiry1(const string &funname, output &result, const input &i); template<class output, class input1, class input2> bool Enquiry2(const string &funname, output &result, const input1 &i1, const input2 &i2); template<class output, class input1, class input2, class input3> bool Enquiry3(const string &funname, output &result, const input1 &i1, const input2 &i2, const input3 &i3); bool GetValid(); string GetDllName(); private: DLLInterface(); private: bool valid; string name; HINSTANCE dllinst; }; template<class output, class input> bool DLLInterface::Enquiry1(const string &funname, output &result, const input &i) { if(valid == false) return false; typedef output(*ptr)(input); ptr p = (ptr)::GetProcAddress(dllinst, funname.c_str()); if(p == NULL) return false; result = p(i); return true; } template<class output, class input1, class input2> bool DLLInterface::Enquiry2(const string &funname, output &result, const input1 &i1, const input2 &i2) { if(valid == false) return false; typedef output(*ptr)(input1, input2); ptr p = (ptr)::GetProcAddress(dllinst, funname.c_str()); if(p == NULL) return false; result = p(i1,i2); return true; } template<class output, class input1, class input2, class input3> bool DLLInterface::Enquiry3(const string &funname, output &result, const input1 &i1, const input2 &i2, const input3 &i3) { if(valid == false) return false; typedef output(*ptr)(input1, input2, input3); ptr p = (ptr)::GetProcAddress(dllinst, funname.c_str()); if(p == NULL) return false; result = p(i1,i2,i3); return true; } } } #endif #include "DLLInterface.hpp" namespace g { namespace lib { DLLInterface::DLLInterface(const string&dllname) { dllinst = LoadLibrary(dllname.c_str()); this->name = name; valid = (dllinst == NULL)? false: true; } DLLInterface::~DLLInterface() { if(dllinst != NULL) FreeLibrary(dllinst); } bool DLLInterface::Reset() { if(dllinst != NULL) ::FreeLibrary(dllinst); return true; } bool DLLInterface::Reset(const string &dllname) { if(dllinst != NULL) ::FreeLibrary(dllinst); dllinst = LoadLibrary(dllname.c_str()); this->name = dllname; return valid = (dllinst == NULL)? false: true; } string DLLInterface::GetDllName() { return name; } } }