2009. 9. 24. 15:35
* DllImport 어트리뷰트
DllImport 어트리뷰트는 C#안에서 Unmanaged 코드를 사용 할 수 있게 합니다. Unmanaged 코드란 닷넷 환경밖에서 개발된 코드를 말하며 예를들면 DLL 파일들안에 컴파일된 표준 , DllImport 어트리뷰트는 System.Runtime.InteropServices 네임스페이스안에서 정의 되어있습니다.
다음은 Win32 API중 User32.dll안에 있는 MessageBox함수를 호출형태 입니다.
※ extern 키워드 : 어떤 메서드가 현재 프로그램 외부에 있음을 나타내는 키워드
DllImport 어트리뷰트는 C#안에서 Unmanaged 코드를 사용 할 수 있게 합니다. Unmanaged 코드란 닷넷 환경밖에서 개발된 코드를 말하며 예를들면 DLL 파일들안에 컴파일된 표준 , DllImport 어트리뷰트는 System.Runtime.InteropServices 네임스페이스안에서 정의 되어있습니다.
using System
using System.Runtime.InteopServices;
class testClass
{
[DllImport("User32.dll"]
public static extern int MessageBox(int h, string m, string c, int type);
static void Main()
{
MessageBox(0, "Hello world!", "C#", 0);
}
}
using System.Runtime.InteopServices;
class testClass
{
[DllImport("User32.dll"]
public static extern int MessageBox(int h, string m, string c, int type);
static void Main()
{
MessageBox(0, "Hello world!", "C#", 0);
}
}
다음은 Win32 API중 User32.dll안에 있는 MessageBox함수를 호출형태 입니다.
int MessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR
lpCaption, UINT uType);
[DllImport("kernel32.dll")]
private static extern bool Beep(int freq, int dur);
[STAThread]
static void Main(string[] args)
{
MessageBox(0, "MessageBox Text", "DllImport Test", 2);
Beep(2600,1000);
}
위코드는 외부의
비관리(Unmanaged)코드에 있는 DLL을 특정 프로세스 메모리에 로딩하여 해당 메소드를 호출하여 사용한
예입니다.private static extern bool Beep(int freq, int dur);
[STAThread]
static void Main(string[] args)
{
MessageBox(0, "MessageBox Text", "DllImport Test", 2);
Beep(2600,1000);
}
※ extern 키워드 : 어떤 메서드가 현재 프로그램 외부에 있음을 나타내는 키워드
'GET > Attribute' 카테고리의 다른 글
참고 사이트. (0) | 2009.09.24 |
---|---|
간단 예제. (0) | 2009.09.24 |
* Conditional 어트리뷰트 사용 (0) | 2009.09.24 |
Attrubute(어트리뷰트) (0) | 2009.09.24 |
Attribute Oriented Programming (0) | 2009.09.24 |