C#讀取C++編寫的DLL

#include "stdafx.h"
#define DLL1_API extern "C" _declspec(dllexport)
#include "Dll1.h"
#include <Windows.h>
#include <stdio.h>

int _stdcall add(int a,int b)
{
	return a+b;
}

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


#ifdef DLL1_API
#else
#define DLL1_API extern "C" _declspec(dllimport)
#endif

DLL1_API int _stdcall add(int a,int b);
DLL1_API int _stdcall subtract(int a,int b);


上面爲C++代碼;  編譯生成DLL 文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices; 
namespace test_dll_
{
    class Program
    {


        [DllImport("dll_.dll")]
        public static extern int add(int a, int b); 
        static void Main(string[] args)
        {
            int a = add(5,10);
        }
    }
}


發佈了151 篇原創文章 · 獲贊 126 · 訪問量 69萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章