可以将自己写的类写成dll吗 如果可以的话,格式是什么样的,如何引用 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果你想动态加载: 从技术上讲,COM是最好的解决办法; 从效率上将,手动引出vTable也不是不可以,不过好像更难。如果静态连接也可以的话: 直接在类声明的class关键字前面加__declspec(dllexport),在调用者那里 加__declspec(dllimport)就可以了。具体做法自己去查msdn。 可以,输出整个类,就可以了,但是delphi不能调用你的类。 可以阿,VC支持的。创建dll工程,假如你得类,继承CCmdTarget就可以了阿。 在类声明的class关键字前面加AFX_CLASS_EXPORT就可以了。 不好意思,还是不太清楚主要问题是声明类的时候要加__declspec(dllexport),那么类中的一些数据成员和成员函数的格式是不是也这样,加上__declspec(dllexport);再有如何组织.h文件和.cpp文件,把类中函数的声明和实现分开时格式又如何?类中需要加的一些头文件,是不是跟平常的格式一样?比如说下面是我的在一个程序中加的一个类,如何把这个类写成dll?××××××××××××××头文件×××××××××#if !defined(AFX_CLIENTSOCKET_H__77CF4232_6320_486B_B14A_20A66DB4AC3E__INCLUDED_)#define AFX_CLIENTSOCKET_H__77CF4232_6320_486B_B14A_20A66DB4AC3E__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000// ClientSocket.h : header file////#include "NetClientView.h"/////////////////////////////////////////////////////////////////////////////// CClientSocket command targetclass CNetClientView;class CMsg;class CClientSocket : public CSocket{// Attributespublic: CNetClientView* m_pView; CSocketFile* m_pFile; CArchive* m_pArchiveIn; CArchive* m_pArchiveOut;// Operationspublic: CClientSocket(CNetClientView* pView); virtual ~CClientSocket();// Overridespublic: BOOL ReceiveMsg(CMsg *pMsg); BOOL SendMsg(CMsg *pMsg); void Initialize(); // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CClientSocket) public: virtual void OnReceive(int nErrorCode); //}}AFX_VIRTUAL // Generated message map functions //{{AFX_MSG(CClientSocket) // NOTE - the ClassWizard will add and remove member functions here. //}}AFX_MSG// Implementationprotected:};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_CLIENTSOCKET_H__77CF4232_6320_486B_B14A_20A66DB4AC3E__INCLUDED_)×××××××××××.cpp文件×××××××××××// ClientSocket.cpp : implementation file//#include "stdafx.h"#include "NetClient.h"#include "ClientSocket.h"#include "Msg.h"#include "NetClientView.h" //??????????????#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CClientSocket//#include "NetClientView.h"CClientSocket::CClientSocket(CNetClientView* pView){ m_pView=pView;}CClientSocket::~CClientSocket(){ if(m_pArchiveOut!=NULL) delete m_pArchiveOut; if(m_pArchiveIn!=NULL) delete m_pArchiveIn; if(m_pFile!=NULL) delete m_pFile;}// Do not edit the following lines, which are needed by ClassWizard.#if 0BEGIN_MESSAGE_MAP(CClientSocket, CSocket) //{{AFX_MSG_MAP(CClientSocket) //}}AFX_MSG_MAPEND_MESSAGE_MAP()#endif // 0/////////////////////////////////////////////////////////////////////////////// CClientSocket member functionsvoid CClientSocket::Initialize(){ m_pFile=new CSocketFile(this); m_pArchiveIn=new CArchive(m_pFile,CArchive::load); m_pArchiveOut=new CArchive(m_pFile,CArchive::store);}void CClientSocket::OnReceive(int nErrorCode) { // TODO: Add your specialized code here and/or call the base class m_pView->ReceiveFile(this); CSocket::OnReceive(nErrorCode);}BOOL CClientSocket::SendMsg(CMsg *pMsg){ if(!m_pArchiveOut==NULL) { pMsg->Serialize(*m_pArchiveOut); m_pArchiveOut->Flush(); return true; } return false;}BOOL CClientSocket::ReceiveMsg(CMsg *pMsg){ if(!m_pArchiveIn==NULL) { pMsg->Serialize(*m_pArchiveIn); m_pArchiveIn->Flush(); return true; } return false;} 肯定能行,但不知道DELPHI能不能用。 在VC中直接生成COM组件,然后在分别在三个对外接口中定义自己所需的功能,然后将自己的类代码写入组件的功能执行部分即可,最好不用静态加载,要不然程序的可移植性会大打折扣的。 /*主要问题是声明类的时候要加__declspec(dllexport),那么类中的一些数据成员和成员函数的格式是不是也这样,加上__declspec(dllexport);*/在定义类时加入__declspec(dllexport)就可以了,数据成员不用了。 加extern"C"_declspec(dllexport)如果需要,全局变量也可以引出,加_declspec(dllexport) 不知道用什么是通用的呢?COM?ACTIVEX? 请问CNewMenu的子菜单显示图标问题 CoCreateGuid也会重啊? 帮我看看对话框背景设置为PNG图片为何失败?在线等 关于编译器的问题?。。。怎么没人知道? 大家给个意见 菜鸟问题如何判断_variant_t 类型的变量为空? 多个表插入新记录的问题 关于 CMDIChildWnd 请指教 各位高手帮忙解决一个问题 IE控件问题 如何编写服务器程序阿? 请问用VC怎么读3ds文件,及3ds文件数据结构是怎样的,谢谢!急!
从技术上讲,COM是最好的解决办法;
从效率上将,手动引出vTable也不是不可以,不过好像更难。
如果静态连接也可以的话:
直接在类声明的class关键字前面加__declspec(dllexport),在调用者那里
加__declspec(dllimport)就可以了。具体做法自己去查msdn。
主要问题是声明类的时候要加__declspec(dllexport),那么类中的一些数据成员和成员函数的格式是不是也这样,加上__declspec(dllexport);再有如何组织.h文件和.cpp文件,把类中函数的声明和实现分开时格式又如何?类中需要加的一些头文件,是不是跟平常的格式一样?
比如说下面是我的在一个程序中加的一个类,如何把这个类写成dll?
××××××××××××××头文件×××××××××
#if !defined(AFX_CLIENTSOCKET_H__77CF4232_6320_486B_B14A_20A66DB4AC3E__INCLUDED_)
#define AFX_CLIENTSOCKET_H__77CF4232_6320_486B_B14A_20A66DB4AC3E__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// ClientSocket.h : header file
////#include "NetClientView.h"/////////////////////////////////////////////////////////////////////////////
// CClientSocket command target
class CNetClientView;
class CMsg;class CClientSocket : public CSocket
{
// Attributes
public:
CNetClientView* m_pView; CSocketFile* m_pFile;
CArchive* m_pArchiveIn;
CArchive* m_pArchiveOut;
// Operations
public:
CClientSocket(CNetClientView* pView);
virtual ~CClientSocket();// Overrides
public:
BOOL ReceiveMsg(CMsg *pMsg);
BOOL SendMsg(CMsg *pMsg);
void Initialize();
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CClientSocket)
public:
virtual void OnReceive(int nErrorCode);
//}}AFX_VIRTUAL // Generated message map functions
//{{AFX_MSG(CClientSocket)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG// Implementation
protected:
};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_CLIENTSOCKET_H__77CF4232_6320_486B_B14A_20A66DB4AC3E__INCLUDED_)
×××××××××××.cpp文件×××××××××××
// ClientSocket.cpp : implementation file
//#include "stdafx.h"
#include "NetClient.h"
#include "ClientSocket.h"
#include "Msg.h"
#include "NetClientView.h" //??????????????#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CClientSocket
//#include "NetClientView.h"CClientSocket::CClientSocket(CNetClientView* pView)
{
m_pView=pView;
}CClientSocket::~CClientSocket()
{
if(m_pArchiveOut!=NULL)
delete m_pArchiveOut;
if(m_pArchiveIn!=NULL)
delete m_pArchiveIn;
if(m_pFile!=NULL)
delete m_pFile;
}
// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CClientSocket, CSocket)
//{{AFX_MSG_MAP(CClientSocket)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif // 0/////////////////////////////////////////////////////////////////////////////
// CClientSocket member functionsvoid CClientSocket::Initialize()
{
m_pFile=new CSocketFile(this);
m_pArchiveIn=new CArchive(m_pFile,CArchive::load);
m_pArchiveOut=new CArchive(m_pFile,CArchive::store);}void CClientSocket::OnReceive(int nErrorCode)
{
// TODO: Add your specialized code here and/or call the base class
m_pView->ReceiveFile(this); CSocket::OnReceive(nErrorCode);
}BOOL CClientSocket::SendMsg(CMsg *pMsg)
{
if(!m_pArchiveOut==NULL)
{
pMsg->Serialize(*m_pArchiveOut);
m_pArchiveOut->Flush();
return true;
}
return false;
}BOOL CClientSocket::ReceiveMsg(CMsg *pMsg)
{
if(!m_pArchiveIn==NULL)
{
pMsg->Serialize(*m_pArchiveIn);
m_pArchiveIn->Flush();
return true;
}
return false;}
在定义类时加入__declspec(dllexport)就可以了,数据成员不用了。
如果需要,全局变量也可以引出,加_declspec(dllexport)