- HOME
- Post in | and so on/c#
- Post at | 2012. 9. 26. 18:08 | by 밀크빵.
- View comment
C++ DLL 함수 char*를 C#에서 읽어 오기
- C++에서 만든 dll 함수
extern "C" __declspec(dllexport) char* Suma(char* a, char*b)
{
char* result = (char*)LocalAlloc(LPTR, strlen(a) + strlen(b) +1);
strcat(result, a);
strcat(result, b);
LocalFree(result);
return result;
}
- C#에서 dll함수 적용하기
class Program
{
[DllImport("C:\\DllName.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern System.IntPtr Suma(string a, string b);
static void Main()
{
string a = "a";
string b = "b";
IntPtr p = Suma(a,b);
string c = Marshal.PtrToStringAnsi(p);
Marshal.FreeHGlobal(p);
Console.WriteLine(c);
}
}
C++ 에서는
LocalAlloc을 이용 메모리 할당 해줘야 함
C#에서는
IntPtr 구조체를 선언하여 dll 함수 저장 후
Marshal.PtrToStringAnsi() 메소드 이용하여 관리되는 string, 관리되지 않는 ANSI 문자열 복사
Marshal.FreeHGlobal() 메소드 이용하여 할당한 메모리 해제
출처 - http://blog.naver.com/PostView.nhn?blogId=nickpooh&logNo=50108296076&redirect=Dlog&widgetTypeCall=true
'and so on > c#' 카테고리의 다른 글
c# 오피스 참조한 프로젝트 배포 시 필요한 dll (0) | 2013.01.07 |
---|---|
c#dll c++에서 사용 (2) | 2013.01.02 |
C# - Excel 내용 읽어오기 (0) | 2012.09.17 |
C# - Excel 창 접근하기 (0) | 2012.09.03 |
C# - 파일 선택 다이얼로그 띄우기 (0) | 2012.08.30 |