1.vc做的win32的dll,其中dll2.cpp文件代码如下:

 

int add(int a,int b)
{

 return a+b+1;
}

int subtract(int a,int b)
{
 return a-b;
}

2.dll2.def代码如下:

LIBRARY dll2
EXPORTS
add
subtract

3.将dll2.lib和dll2.dll拷到c#当前目录下

4.建立测试用的.net程序,代码如下:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;   

namespace WindowsApplication1
{


    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {


        }

        private void button1_Click(object sender, EventArgs e)
        {
           int nindex=Name1.add(2,0);
        }
    }

    public class Name1
    {

        //导入dll

        [DllImport("dll2.dll", CallingConvention = CallingConvention.StdCall)]

        //声明接口函数

        public static extern int add(int a, int b);

    }
}

 

 

posted on 2009-06-03 00:43  jasonM  阅读(546)  评论(0)    收藏  举报