身为学渣,总是学完就忘,所以写下来方便自己以后查看,也是为其他小白提供一点思路。
参考的资料:http://blog.csdn.NET/u011221078/article/details/41308217
http://blog.sina.com.cn/s/blog_7939cf980102v052.html
一、生成DLL
VS2013——建立win32项目——DLL——完成
-
-
- #include "stdafx.h"
-
-
-
- extern "C"_declspec(dllexport) void maopao(int *p,int count);
-
- void maopao(int *p,int count)
-
- {
-
- int temp=0;
-
- for(int i=1;i<count;i++)
-
- {
-
- for(int j=count-1;j>=i;j--)
-
- {
-
- if(p[j]>p[j-1])
-
- {
-
- temp=p[j];
-
- p[j]=p[j-1];
-
- p[j-1]=temp;
-
- }
-
- }
-
- }
-
- }
编译生成,在Debug文件下生成了.dll和.lib两个文件;
二、调用DLL
VS2013——建立win32控制台项目——完成
把第一步生成的.dll和.lib两个文件拷入控制台程序的Debug文件夹下
添加引用:
项目——属性——链接器——常规——附加库目录——添加dll
项目——属性——链接器——输入——附加依赖项——添加lib
- #include<iostream>
-
- #include<time.h>
-
- using namespace std;
-
-
-
- extern "C"_declspec(dllimport) void maopao(int *p,int count);
-
- int main()
-
- {
-
- int a[10];
-
- srand(time(0));
-
- for(int i=0;i<10;i++)
-
- a[i]=rand()%50;
-
- maopao(a,10);
-
- for(int i=0;i<10;i++)
-
- cout<<a[i]<<endl;
-
- getchar();
-
- return 0;
-
- }
运行OK ~