dll 링크 세번째 이번에는 포인터 ㅡ.ㅡ; 별쓰데 없는것에 다 한다는 생각이 든다. 불현듯 아참 C#은 포인터를 안쓰지?? 라는 생각에서 그럼 포인터를 사용한 dll을 이용할수 있을까나??? 이런 생각이 또 들었다. 아참~~ 이 꼬리에 꼬리는 무는 잡생각들~~
어째던 아래와 같이 dll을 만들었다.
extern "C" __declspec(dllexport)void IntSwap(int * a,int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
extern "C" __declspec(dllexport)void LongSwap(long * a,long * b)
{
long temp;
temp = *a;
*a = *b;
*b = temp;
}
포이터의 단골 주제인 Swap이다. 음...
이제는 C#코드인데..... 조금 생각을 해봤다. 과연 포인터의 인자값을 어떻게 넘길것인가에 대한 것. 여러 궁리 끝에 드디어 ref를 이용해서 dll 함수를 쓸수 있게 되었다. 음.. 역시 그럼 이제 C#으로 안되는것은 별로 없네.. 라는 생각이 들었다.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace DllPointer
{
class Program
{
[DllImport("DllSwap.dll",CharSet=CharSet.Ansi)]
public static extern void IntSwap(ref int a,ref int b);
[DllImport("DllSwap.dll", CharSet = CharSet.Ansi)]
public static extern void LongSwap(ref long a, ref long b);
static void Main(string[] args)
{
int a = 10;
int b = 20;
IntSwap(ref a, ref b);
Console.WriteLine("a = {0}, b = {1}",a,b);
long c = 10000000;
long d = 20000000;
LongSwap(ref c, ref d);
Console.WriteLine("c = {0}, d = {1}",c,d);
}
}
}

Trackback url :: http://hahakbs.dothost.co.kr/trackback/43

댓글을 달아 주세요