LoginSignup
2
2

More than 3 years have passed since last update.

C#用にC++のDllを作る方法

Last updated at Posted at 2020-03-31

僕忘れやすいからメモとして残しておきます

VisualStudio2019で空のプロジェクトの作成を行い以下の二つのファイルを追加し、プロパティでdll向けに設定をして,ビルドを行います。

Test.cpp
#include "Test.h"

//Test用メソッド
int Test(int a, int b){
	return (a + b);
}
Test.h
#pragma once
#define DLLEXPORT _declspec (dllexport)

extern "C" {
	DLLEXPORT int Test(int a, int b);
}

出力されたdllをC#側で以下のようにして読むと実行できます

Test.cs
using System.Runtime.InteropServices;

[DllImport("Test.dll")]]
private static extern int Test(int a, int b);

void main(){
     Console.WriteLine(Test(1,2)); 
}
2
2
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2