我有几个C++类,想把它封装成DLL,提供几个接口函数。
请问我该如何做呢?(VC环境。)
请给个简单例子,本人很菜。
多谢指教!

解决方案 »

  1.   

    class AFX_EXT_CLASS CPersistentFrame : public CFrameWnd
    { // remembers where it was on the desktop
        DECLARE_DYNAMIC(CPersistentFrame)
    private:
        static const CRect s_rectDefault;
        static const char s_profileHeading[];
        static const char s_profileRect[];
        static const char s_profileIcon[];
        static const char s_profileMax[];
        static const char s_profileTool[];
        static const char s_profileStatus[];
        BOOL m_bFirstTime;
    protected: // Create from serialization only
        CPersistentFrame();
        ~CPersistentFrame();
    //{{AFX_VIRTUAL(CPersistentFrame)
        public:
        virtual void ActivateFrame(int nCmdShow = -1);
        protected:
        //}}AFX_VIRTUAL    //{{AFX_MSG(CPersistentFrame)
        afx_msg void OnDestroy();
        //}}AFX_MSG    DECLARE_MESSAGE_MAP()
    };#endif // _INSIDE_VISUAL_CPP_PERSISTENT_FRAME
    在类前面加AFX_EXT_CLASS
      

  2.   

    一、 函数的定义和使用方法:  第一步:   运行AppWizard,定义项目名为mydll,选择MFC AppWizard(dll),而不是MFC AppWizards(exe)。  第二步:   在这个例子中,只有一个AppWizard屏幕出现,选择MFC扩展DLL(MFC Extension DLL (using shared MFC DLL),点击FINISH生成工程。  第三步:   点击File中的New,选择C/C++ Header File,File Name中输入dll,点击OK,创建dll.h。输入extern "C" __declspec(dllexport) int fun(int x1,int x2,int x3); ,保存。  第四步:   点击File中的New,选择C++ Source File,File Name中输入dll,点击OK,创建dll.cpp。输入  #include "stdafx.h"
      #include "dll.h"
      extern "C" __declspec(dllexport) int fun(int x1,int x2,int x3)
       {
        return x1+x2+x3;
       }  编译生成mydll.dll和mydll.lib。  第五步:  选择Project 中Add To Project 中的New , 重新生成一个工程,选择MFC AppWizards(exe),项目名为mydlltest , 选择Single Document ,点击FINISH,生成一个新的工程。选择菜单中的Project àSet Active Project àmydlltest ,将mydlltest设为当前活动工程。  第六步:  拷贝…\mydll\debug\mydll.dll 到 ..\mydlltest\debug\下,拷贝…\mydll\debug\mydll.lib到…\mydlltest\目录下。  第七步:  在mydlltestView.cpp中的#endif下添加  extern "C" __declspec(dllimport) int fun(int x1,int x2,int x3);  在void CMydlltestView::OnDraw(CDC* pDC)中添加代码如下:  void CMydlltestView::OnDraw(CDC* pDC)
       {
        CMydlltestDoc* pDoc = GetDocument();
        ASSERT_VALID(pDoc);
        // TODO: add draw code for native data here
        int x=fun(1,2,3);
        CString str;
        str.Format("%d",str);
        pDC->TextOut(10,10,x);
       }  第八步:  在WorkSpace中的mydlltest files上点击右键,选择Add files to Project ,将mydll.dll添加到工程。
    好了,我们的工作做完了,运行一下看看吧!  二、 类的添加和使用步骤:  在mydll中生成一个新类,类的声明处改为class AFX_EXT_CLASS CMyClass ,在此类中和其他类一样添加自己需要的变量和函数,编译生成新的dll和lib,重复上面的第六步,在mydlltest中利用上面的方法生成一个myclass.h文件,拷贝mydll中myclass.h的所有代码到新生成的myclass.h中。在需要使用dll中CmyClass类的文件中包含myclass.h,然后我们就可以向其他类一样使用该类了。  三、 字符串的定义和使用  在mydll中定义字符串资源,编译生成新的dll和lib,重复上面的第六步,在mydlltest中的myclass.h中添加 #define IDS_MYSTR 1(假设我们定义的字符串资源的ID号为IDS_MYSTR),在需要是要该字符串的地方添加#include "myclass.h",即可使用该字符串了。  CString str2;
      str2.LoadString(IDS_MYSTR);  四、 对话框的定义和使用  在mydll中定义对话框资源,生成一个新类CDlg,类的声明处改为class AFX_EXT_CLASS CDlg : public CDialog ,像其他对话框一样定义对话框的属性和功能,编译生成新的dll和lib,重复上面的第六步,在mydlltest中利用上面的方法生成一个dlg.h文件,拷贝mydll中dlg.h的所有代码到新生成的dlg.h中。在需要使用dll中此对话框资源的文件中包含dlg.h,然后我们就可以向其他对话框一样使用此对话框了。  CDlg dlg;
      dlg.DoModal();
     
    上面的是另一位仁兄的话,我借的。调用DLL的方法,显示
    //--定义“实例”
     HINSTANCE Dll_handler;//--声明dll内的函数
    typedef BOOL (*DLLTEST)(int nPort, int nBaud, int nByte, int nParity);
    DLLTEST CommOpen;;
    //--装载dll
    Dll_handler = LoadLibrary("Commpro.dll"); CommOpen = (DLLTEST)GetProcAddress(Dll_handler,"CommOpen");//--取得dll中的函数地址
    CommOpen(m_nPort,m_nBaud,m_nByte,m_nParity)
    FreeLibrary(Dll_handler);
    隐示就是加LIB到CPP文件夹中,再写个头文件来声明DLL中的函数。
      

  3.   

    非常感谢几位,尤其是zhang865(《我现在才发现我最笨》) 
    但是我是想把它封成WIN32 DLL,谢谢!!
      

  4.   

    zhang865(《我现在才发现我最笨》老兄写的真详细,我们向他学习和致敬
      

  5.   

    WIN32 DLL吗。用def文件导出函数。(fnGetServer是个全局函数)
    LIBRARY "ServerDLL.DLL"EXPORTS
    fnGetServer @1 PRIVATE