读书人

怎么在C++Builder下把C++文件编译成DL

发布时间: 2012-02-28 13:06:35 作者: rapoo

如何在C++Builder下把C++文件编译成DLL文件 - C++ Builder / Windows SDK/API
如题,非常感谢!~~~

[解决办法]

C/C++ code
#include <iostream>#define NO_STRING  //#ifdef NO_STRING#define TREE_STRING char*#else#include <string>#define TREE_STRING string#endif#define DLL_TREE    //#ifdef DLL_TREE#define DLLXXPORT _declspec(dllexport)#define EXPIMP_TEMPLATE#else#define DLLXXPORT _declspec(dllimport)#define EXPIMP_TEMPLATE extern#endifusing namespace std;#ifndef DLL_TREEHPP#define DLL_TREEHPP//EXPIMP_TEMPLATE template class _declspec(DLLXXPORT) std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >;class Tree;extern "C"{class cx_Tree{public:    virtual void __stdcall printTreeInfo()=0;    virtual Tree __stdcall copyTree(const Tree &)=0;};class DLLXXPORT Tree: public cx_Tree{ private:    int id;    TREE_STRING name;   int high;   int zhijin;   TREE_STRING zhonglei; public:     __stdcall Tree(){};    //必须定义,不然定义copyTree后会出问题     __stdcall ~Tree(){};    //必须定义,不然定义copyTree后会出问题     //Tree(int i,char* n,int h,int zj,char* zl);      __stdcall Tree(int i,TREE_STRING n,int h,int zj,TREE_STRING zl);    virtual void __stdcall printTreeInfo(void);    virtual Tree __stdcall copyTree(const Tree &);};}extern "C"{    DLLXXPORT cx_Tree* __stdcall new_Tree(int i,TREE_STRING n,int h,int zj,TREE_STRING zl);    DLLXXPORT void __stdcall delete_Tree(cx_Tree* o);}#endif  //DLL_TREEHPP
[解决办法]
C/C++ code
//---------------------------------------#include <windows.h>//---------------------------------------//   Important note about DLL memory management when your DLL uses the//   static version of the RunTime Library:////   If your DLL exports any functions that pass String objects (or structs///   classes containing nested Strings) as parameter or function results,//   you will need to add the library MEMMGR.LIB to both the DLL project and//   any other projects that use the DLL.  You will also need to use MEMMGR.LIB//   if any other projects which use the DLL will be performing new or delete//   operations on any non-TObject-derived classes which are exported from the//   DLL. Adding MEMMGR.LIB to your project will change the DLL and its calling//   EXE's to use the BORLNDMM.DLL as their memory manager.  In these cases,//   the file BORLNDMM.DLL should be deployed along with your DLL.////   To avoid using BORLNDMM.DLL, pass string information using "char *" or//   ShortString parameters.////   If your DLL uses the dynamic version of the RTL, you do not need to//   explicitly add MEMMGR.LIB as this will be done implicitly for you//---------------------------------------#pragma argsusedint WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved){        return 1;}//---------------------------------------#include "dll_tree.h"void __stdcall Tree::printTreeInfo(void){    cout<<"编号:"<<id<<" 名字:"<<name<<" 高度:"<<high<<      "cm 直径:"<<zhijin<<"cm 种类:"<<zhonglei<<endl;}__stdcall Tree::Tree(int i,TREE_STRING n,int h,int zj,TREE_STRING zl){    this->id=i;   this->name=n;   this->high=h;   this->zhijin=zj;   this->zhonglei=zl;}Tree __stdcall Tree::copyTree(const Tree &a){    cx_Tree* b=new_Tree(1,"dd1",3600,20,"杉");    return *((Tree*)b);}cx_Tree* __stdcall new_Tree(int i,TREE_STRING n,int h,int zj,TREE_STRING zl){    return new Tree(i,n,h,zj,zl);}void __stdcall delete_Tree(cx_Tree* o){    delete o;}

[解决办法]
这个是导出类的,做成的DLL可以在VC中使用,相对于你的问题来说这个例子复杂了点,你只需要学会_declspec(dllexport)和_declspec(dllimport)就可以解决你的问题了
[解决办法]
帮顶一下!!

读书人网 >C++ Builder

热点推荐