dll的生成及使用
时间:2010-10-20 来源:agchen_1975
今天学习了使用VC6.0创建动态链接库,及使用vs2005编写了对动态链接库的调用。
1.使用vc6.0创建动态链接库项目dllTest,并添加文件lib.h
#ifndef LIB_H
#define LIB_H
extern "C" int __declspec(dllexport)add(int x, int y);
#endif
lib.cpp文件:
#include "lib.h"
int add(int x, int y)
{
return x+y;
}
2.使用vs2005创建使用动态链接库的解决方案UseDLL,并将1产生的dllTest.dll文件拷贝到debug目录下,解决方案下的Program.cs源码为:
using System;
using System.Runtime.InteropServices;
namespace UseDLL
{
public class Win32
{
[DllImport("dllTest.dll", CharSet = CharSet.Auto)]
public static extern int add(int x, int y);
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Win32.add(5, 3));
Console.ReadLine();
}
}
}