下面是C创建与调用DLL的:
dll 的源码 testdll.c
CODE:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */
/*定义了一个 dll 函数 addxy,返回2个数的和*/
DLLIMPORT int addxy ( int x, int y)
{
return (x+y);
}
BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ ,
DWORD reason /* Reason this function is being called. */ ,
LPVOID reserved /* Not used. */ )
{
switch (reason)
{
case DLL_PROCESS_ATTACH: /*dll 进程加载的时候执行的事情*/
break;
case DLL_PROCESS_DETACH: /*dll 进程卸载的时候执行的事情*/
break;
case DLL_THREAD_ATTACH: /*dll 线程加载的时候执行的事情*/
break;
case DLL_THREAD_DETACH: /*dll 线程卸载的时候执行的事情*/
break;
}
/* Returns TRUE on success, FALSE on failure */
return TRUE;
}
#include <stdio.h>
#include <stdlib.h>
#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport)
#else /* Not BUILDING_DLL */
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */
/*定义了一个 dll 函数 addxy,返回2个数的和*/
DLLIMPORT int addxy ( int x, int y)
{
return (x+y);
}
BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ ,
DWORD reason /* Reason this function is being called. */ ,
LPVOID reserved /* Not used. */ )
{
switch (reason)
{
case DLL_PROCESS_ATTACH: /*dll 进程加载的时候执行的事情*/
break;
case DLL_PROCESS_DETACH: /*dll 进程卸载的时候执行的事情*/
break;
case DLL_THREAD_ATTACH: /*dll 线程加载的时候执行的事情*/
break;
case DLL_THREAD_DETACH: /*dll 线程卸载的时候执行的事情*/
break;
}
/* Returns TRUE on success, FALSE on failure */
return TRUE;
}
用 gcc-for-win 3.3.1 编译,获得一个 testdll.dll
然后编写 rundll.c ,源码
CODE:
#include <stdio.h>
#include <windows.h>
typedef int(*lpAddFun)(int, int); //宏定义函数指针类型
int main(int argc, char *argv[])
{
HINSTANCE hDll; //DLL句柄
lpAddFun addFun; //函数指针
int result;
hDll = LoadLibrary("testdll.dll"); /*加载 testdll.dll*/
if (hDll != NULL)
{
addFun = (lpAddFun)GetProcAddress(hDll, "addxy");
if (addFun != NULL)
{
result = addFun(654, 212); /* 654 + 212*/
printf("%d", result);
}
FreeLibrary(hDll);
}
return 0;
}
#include <windows.h>
typedef int(*lpAddFun)(int, int); //宏定义函数指针类型
int main(int argc, char *argv[])
{
HINSTANCE hDll; //DLL句柄
lpAddFun addFun; //函数指针
int result;
hDll = LoadLibrary("testdll.dll"); /*加载 testdll.dll*/
if (hDll != NULL)
{
addFun = (lpAddFun)GetProcAddress(hDll, "addxy");
if (addFun != NULL)
{
result = addFun(654, 212); /* 654 + 212*/
printf("%d", result);
}
FreeLibrary(hDll);
}
return 0;
}
编译,获得 rundll.exe
把 testdll 放入 rundll.exe 相同工作目录,或者 $PATH 能够到达的地方
然后运行 rundll.exe,可以看到 rundll.exe 装载 testdll.dll,调用里面的 addxy 函数,
QUOTE:
D:\document\studio\c\use_c_dll>rundll
866
D:\document\studio\c\use_c_dll>rundll
866
D:\document\studio\c\use_c_dll>rundll
C++的
先写生成TestDLL.DLL的,这里用了两种方式进行:
保存成TestDLL.cpp然后进行编译。呵呵,因为是用Editplus写的代码,所以要手动编译哦^^
我用的是VS 2005的CL编译接口CL.exe。在Cmd下:CL/c TestDLL.cpp
此时就生成了TestDLL.obj,然后进行链接:link TestDLL.obj /DLL
此时将生成TestDLL.dll,TestDll.exp,TestDll.lib三个文件
工作暂告一个段落。。。。
然后开始写调用TestDLL.dll的CallDLL.exe文件:
保存成CallDLL.cpp文件开始进行编译链接。
这次一步到位:cl CallDll.cpp TestDLL.lib
现在就可以直接运行CallDLL.exe了。。。。
http://hi.baidu.com/dl88250/blog/item/cd571c4c2dda76f9d62afce1.html
先写生成TestDLL.DLL的,这里用了两种方式进行:
CODE:
#include <iostream>
using namespace std;
class DLLClass{
public:
// exported member function
__declspec(dllexport) void functionA(void)
{
cout << "In Function A of the exported function" << endl;
return;
}
};
// exported class
class __declspec(dllexport) ExportDLLClass{
public:
void functionB(void)
{
cout << "In Function B of the exported class" << endl;
return;
}
};
// exported instance of the DLLClass
__declspec(dllexport) DLLClass test;
using namespace std;
class DLLClass{
public:
// exported member function
__declspec(dllexport) void functionA(void)
{
cout << "In Function A of the exported function" << endl;
return;
}
};
// exported class
class __declspec(dllexport) ExportDLLClass{
public:
void functionB(void)
{
cout << "In Function B of the exported class" << endl;
return;
}
};
// exported instance of the DLLClass
__declspec(dllexport) DLLClass test;
保存成TestDLL.cpp然后进行编译。呵呵,因为是用Editplus写的代码,所以要手动编译哦^^
我用的是VS 2005的CL编译接口CL.exe。在Cmd下:CL/c TestDLL.cpp
此时就生成了TestDLL.obj,然后进行链接:link TestDLL.obj /DLL
此时将生成TestDLL.dll,TestDll.exp,TestDll.lib三个文件
工作暂告一个段落。。。。
然后开始写调用TestDLL.dll的CallDLL.exe文件:
CODE:
class DLLClass{
public:
// imported member function
__declspec(dllimport) void functionA(void);
};
// imported class
class __declspec(dllimport) ExportDLLClass{
public:
void functionB(void);
};
// imported instance of the DLLClass
__declspec(dllimport) DLLClass test;
int main(void)
{
ExportDLLClass TestClass;
test.functionA();
TestClass.functionB();
return 0;
}
public:
// imported member function
__declspec(dllimport) void functionA(void);
};
// imported class
class __declspec(dllimport) ExportDLLClass{
public:
void functionB(void);
};
// imported instance of the DLLClass
__declspec(dllimport) DLLClass test;
int main(void)
{
ExportDLLClass TestClass;
test.functionA();
TestClass.functionB();
return 0;
}
保存成CallDLL.cpp文件开始进行编译链接。
这次一步到位:cl CallDll.cpp TestDLL.lib
现在就可以直接运行CallDLL.exe了。。。。
http://hi.baidu.com/dl88250/blog/item/cd571c4c2dda76f9d62afce1.html