먼저 Visual Studio에서 C++ Win32 DLL 프로젝트를 만든다.
그리고 아래 코드를 넣고 빌드한다.

 
1
2
3
4
5
6
7
8
9
10
11
12
extern "C" __declspec(dllexport) int getINT()
{
	return -1;
}
extern "C" __declspec(dllexport) void getINTPtr(int *val)
{
	*val = -1;
}
extern "C" __declspec(dllexport) void getINTRef(int &val)
{
	val = -1;
}


다음으로 Visual Studio에서 C# Console Application프로젝트를 만든다.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

class DllWrapper
{
	[DllImport("test_c_dll.dll")]
	public static extern int getINT();

	[DllImport("test_c_dll.dll")]
	public static extern int getINTPtr(ref int num);

	[DllImport("test_c_dll.dll")]
	public static extern int getINTRef(ref int num);
}

class Program
{
	static void Main(string[] args)
	{
	    int num = 0;
	    num = DllWrapper.getINT();
	    Console.WriteLine("getINT(): " + num.ToString());

	    num = 0;
	    DllWrapper.getINTPtr(ref num);
	    Console.WriteLine("getINTPtr(): " + num.ToString());

	    num = 0;
	    DllWrapper.getINTRef(ref num);
	    Console.WriteLine("getINTPtr(): " + num.ToString());
	}
}

Output:
1
2
3
getINT(): -1
getINTPtr(): -1
getINTPtr(): -1




2011/11/04 - [C# .NET] - [C#] PInvoke 1탄. 데이터 타입 변환표
2011/11/04 - [C# .NET] - [C#] PInvoke 2탄. Normal Data Type
2011/11/04 - [C# .NET] - [C#] PInvoke 3탄. String 타입
2011/11/04 - [C# .NET] - [C#] PInvoke 4탄. ByteArray
2011/11/04 - [C# .NET] - [C#] PInvoke 5탄. Callback Function

+ Recent posts