VS2010环境下用ANSI C创建DLL和使用方法(转)

时间:2024-04-29 13:38:25

源:VS2010环境下用ANSI C创建DLL和使用方法

. 创建DLL工程
1.1 启动VS
1.2 创建一个dll工程。
操作:a.文件->新建->项目->Win32控制台应用程序.
b.输入工程名称,这里我们用dll,点击确定按钮.
c.点击下一步,在"应用程序设置界面设置"勾选DLL(D)项和空项目,点击完成按钮.
d.视图->解决方案资源管理器,右键点击"头文件",添加->新建项,这里咱们用dll.h
右键点击"源文件",添加->新建项,这里我们添加dll.c,到此dll工程搭建完毕.
1.3 dll.h的内容如下
#ifndef AXLPLUGIN_H
#define AXLPLUGIN_H
/**/
#ifdef _WINDOWS
#define DLL_DECLARE __declspec(dllexport)
#else
#define DLL_DECLARE
#endif
DLL_DECLARE int Min(int a, int b);
/* 把所有的函数声明都列在这里 */
#endif
1.4 dll.c的内容如下
#include "dll.h"
#include <stdio.h>
/*根据需要添加相应的头文件*/
DLL_DECLARE int Min(int a, int b)
{
if (a >= b)
return b;
else
return a;
}
/* 把所有声明的函数都在这里实现*/
. DLL文件的使用
2.1 试验证明dll.dll文件要和dll.lib以及dll.h文件一起使用
2.2 新建一个测试用的"Win32应用程序"
操作:a.文件->新建->项目->Win32控制台应用程序.
b.输入工程名称,这里我们用test_dll,点击确定按钮.
c.点击下一步,在"应用程序设置界面设置"控制台应用程序"和"空项目",点击完成按钮.
d.将工程dll目录里的dll.h/dll.dll/dll.lib拷贝到工程test_dll目录里。
e.视图->解决方案资源管理器,右键点击"头文件",添加->新建项,这里咱们用dll.h
右键点击"源文件",添加->新建项,这里我们添加test_dll.c,右键点击“资源文件”,
添加->"现有项",选择dll.lib,到此test_dll工程搭建完毕.
2.3 编辑test_dll.c文件,内容如下
#include "dll.h"
#include <stdio.h>
int main()
{
printf("Min(2, 4) = %d/n", Min(, ));
printf("Min(5, 2) = %d/n", Min(, ));
return ;
}
2.4 dll和test_dll工程的目录结构
2.4. dll工程目录结构
../dll/
│ dll.sdf
│ dll.sln
│ dll_dir.txt
├─Debug
│ dll.dll
│ dll.exp
│ dll.ilk
│ dll.lib
│ dll.pdb
├─dll
│ │ dll.cpp
│ │ dll.h
│ │ dll.vcxproj
│ │ dll.vcxproj.filters
│ │ dll.vcxproj.user
│ └─Debug
│ cl.command..tlog
│ CL.read..tlog
│ CL.write..tlog
│ dll.Build.CppClean.log
│ dll.dll.embed.manifest
│ dll.dll.embed.manifest.res
│ dll.dll.intermediate.manifest
│ dll.lastbuildstate
│ dll.log
│ dll.obj
│ dll.write..tlog
│ dll_manifest.rc
│ link-cvtres.read..tlog
│ link-cvtres.write..tlog
│ link.-cvtres.read..tlog
│ link.-cvtres.write..tlog
│ link..read..tlog
│ link..write..tlog
│ link.command..tlog
│ link.read..tlog
│ link.write..tlog
│ mt.command..tlog
│ mt.read..tlog
│ mt.write..tlog
│ rc.command..tlog
│ rc.read..tlog
│ rc.write..tlog
│ RCa02504
│ vc100.idb
│ vc100.pdb
└─ipch
└─dll-75fa4624
dll-129995a8.ipch 2.4. test_dll工程的目录结构
../test_dll/
│ test_dll.sdf
│ test_dll.sln
│ test_dll_dir.txt
├─Debug
│ test_dll.exe
│ test_dll.ilk
│ test_dll.pdb
├─ipch
│ └─test_dll-eb5063a1
│ test_dll-c06c53e7.ipch
└─test_dll
│ dll.dll
│ dll.h
│ dll.lib
│ test_dll.c
│ test_dll.vcxproj
│ test_dll.vcxproj.filters
│ test_dll.vcxproj.user
└─Debug
cl.command..tlog
CL.read..tlog
CL.write..tlog
link-cvtres.read..tlog
link-cvtres.write..tlog
link.-cvtres.read..tlog
link.-cvtres.write..tlog
link..read..tlog
link..write..tlog
link.command..tlog
link.read..tlog
link.write..tlog
mt.command..tlog
mt.read..tlog
mt.write..tlog
rc.command..tlog
rc.read..tlog
rc.write..tlog
test_dll.Build.CppClean.log
test_dll.exe.embed.manifest
test_dll.exe.embed.manifest.res
test_dll.exe.intermediate.manifest
test_dll.lastbuildstate
test_dll.log
test_dll.obj
test_dll_manifest.rc
vc100.idb
vc100.pdb