본문 바로가기

programming/C, C++, C#

[C#] DLL 가져오기

이미 등록이 된 경우에는 다음과 같이 간편하게 사용이 가능하다.

 

[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

 

그러나 DLL 가져오는 것을 인터넷으로 찾아보는 이유는 이미 위 같은 방법으로 되지 않았기 때문이라 본다.

.Net 3.5 이상부터는 적당히 인자와 형태가 맞아서는 돌아가지 않는다.

"호출 규칙" 이라는 것을 지켜줘야 할 수 있다.

아래는 Cpp 에서 컴파일된 동적라이브러리 파일(DLL) 을 C# 에서 가져오는 방법이다.

 

// 인자가 없는경우.
[DllImport(FileName, CallingConvention = CallingConvention.Cdecl)]
static public extern void TCPConnect();

// 인자가 있는경우
[DllImport(FileName, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
static public extern void TCPConnect(string server, int port);

일반적으로 작성하지 않은 경우 default 호출규약은 StdCall 이다.

나머지 호출규약은 아직 사용해보지 않았다.

반응형

'programming > C, C++, C#' 카테고리의 다른 글

[C#] 프로젝트 절대 경로  (0) 2019.05.26
[CPP] Generics  (0) 2017.10.09
[CPP] Queues and Stacks (30 days of learn)  (0) 2017.10.09