Dll 파일을 Import하기 위해서는 아래와 같은 사항을 참조해야한다.
using System.Runtime.InteropServices;
이를 참조하면 아래와 같이 DllImport를 사용하는 것이 가능해진다.
[DllImport("DllTest.dll")]
public static extern int TestFunc(int a, int b);
위의 코드는 메서드선언(혹은 구현)과 같은 레벨에서 이루어져야 한다.
이렇게 Dll을 import 하고 Dll의 메서드를 extern을 통해 선언한다.
이제 Dll에 있는 메서드를 사용할 준비가 됐다. 하지만 VS2010에서는 이 상태에서 빌드를 할 경우 런타임에서 다음과 같은 오류가 발생한다.
"Managed Debugging Assistant 'LoaderLock' has detected a problem in 'C:\Users\Maruti\Desktop\WPF\Vecs\bin\Debug\Vecs.vshost.exe'.
Additional Information: Attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang."
Loder lock이라는 디버그 매니저가 dll import에 대한 잠재적인 위험을 발견한 것이다. 해당 오류가 발생하면 정상적인 dll import가 이루어지지 않으니 이 기능을 끄도록 한다. 상단 메뉴의 Debug -> Exceptions (ctrl+alt+e) 로 들어간 후 Managed Debugging Assistants를 확장하여 Loader Lock의 체크를 해제해준다.
이제 실행을 하면 다른 오류가 발생한다.
"Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'C:\Users\Maruti\Desktop\WPF\Vecs\bin\Debug\Vecs.vshost.exe'.
Additional Information: A call to PInvoke function 'Vecs!Vecs.Window1::TestFunc' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
'C#' 카테고리의 다른 글
C# 시간을 Tick으로 변환 또는 Tick을 시간형태로 변환 (0) | 2013.06.03 |
---|---|
C# DateTime.Parse (0) | 2013.06.01 |
C# Tick Count사용해서 성능테스트하기 (0) | 2013.05.28 |
C# Time (0) | 2013.05.28 |
C# 가비지컬렉터와 가비지컬렉션 (0) | 2013.05.14 |