dll的生成及使用

今天學習了使用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();
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章