Codejie's C++ Space

Using C++

吃了空:DLL轮子

今天又是吃了空,开始想着把Lingos的内容抓出来,然后保存起来。想着到用HOOK来做,以前用Delphi写过HOOK,还有些印象,于是开始写HOOK的DLL。但打开VC忽然想起,好像自己从来没有从头写过DLL,于是便开始写DLL。。。
都说DLL要有DllMain,要都有.def,真的需要吗?不是我不想写这些,而是不知道该怎么写,一直都是让VC自动生成DLL工程的,这些必要的过程还真的不知道。于是我写了下面的代码。

DLLTest.h
 1#ifndef __DLLMAIN_H__
 2#define __DLLMAIN_H__
 3
 4#define DllExport __declspec(dllexport)
 5
 6extern "C" int DllExport Func(int x);
 7
 8extern "C" class DllExport CA
 9{
10public:
11    CA(int x);
12    virtual ~CA();
13
14    int Func(int x);
15private:
16    int _x;
17}
;
18
19#endif

DLLTest.cpp
 1#include "DllMain.h"
 2
 3int Func(int x)
 4{
 5    return x * 10;
 6}

 7
 8CA::CA(int x)
 9   : _x(x)
10{
11}

12
13CA::~CA()
14{
15}

16
17int CA::Func(int x)
18{
19    return x * _x;
20}

编译好,用Depends查看,竟然也有函数导出。。。


这里是测试代码:
 1#include <iostream>
 2
 3#include "DllMain.h"
 4
 5int main()
 6{
 7    std::cout << Func(10<< std::endl;
 8    CA a(100);
 9    std::cout << a.Func(2<< std::endl;
10
11    return 0;
12}

结果还算正常,只是不知道这样的DLL有啥问题不,会不会对系统有啥影响。。。

<----无知的分割线---->
有人知道怎么使用Library方式,显式调用一个DLL的Class吗?麻烦给解释下吧。。。

posted on 2009-09-23 12:04 codejie 阅读(242) 评论(0)  编辑 收藏 引用 所属分类: C++随笔而已


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

Using C++

导航

统计

留言簿(73)

随笔分类(513)

积分与排名

最新评论

阅读排行榜

评论排行榜