C++側
► スポイラーを表示
//MyDLL.h
#pragma once
#define DLL_EXPORT __declspec(dllexport)
#include
extern "C" {
INT DLL_EXPORT Add(INT x, INT y);
INT DLL_EXPORT Sub(INT x, INT y);
INT DLL_EXPORT Mul(INT x, INT y);
INT DLL_EXPORT Div(INT x, INT y);
}
[album]955[/album]
C#側
► スポイラーを表示
//MyDLLCall.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MMFrame.Diagnostics;
namespace DllCallTest
{
public class MyDll : UnManagedDll
{
private class Functions
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate int Add(int x, int y);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate int Sub(int x, int y);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate int Mul(int x, int y);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate int Div(int x, int y);
}
public MyDll(string dllPath) : base(dllPath) { }
public int Add(int x, int y)
{
return GetProcAddress()(x, y);
}
public int Sub(int x, int y)
{
return GetProcAddress()(x, y);
}
public int Mul(int x, int y)
{
return GetProcAddress()(x, y);
}
public int Div(int x, int y)
{
return GetProcAddress()(x, y);
}
}
}
//Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DllCallTest
{
class Program
{
static void Main(string[] args)
{
try
{
MyDll dll = new MyDll("MyDLL.dll");
Console.WriteLine("{0}", dll.Add(10, 9)); //19
Console.WriteLine("{0}", dll.Sub(10, 9)); //1
Console.WriteLine("{0}", dll.Mul(10, 9)); //90
Console.WriteLine("{0}", dll.Div(10, 9)); //1
}
catch (Exception ex)
{
Console.WriteLine("{0}", ex.ToString());
}
Console.ReadKey();
}
}
}
C#でMyDLL.dllを動かすにはbinフォルダのDebugフォルダまたはReleaseフォルダに入れる必要があります。
[hr]
- 2015/8/16 - [UnmanagedFunctionPointer(CallingConvention.Cdecl)] を追加しました。