2012년 10월 24일 수요일

C++ DLL에서 생성한 클래스 인스턴스를 C#에서 사용하기 위한 삽질.

http://www.codeproject.com/Articles/18032/How-to-Marshal-a-C-Class
  1. struct ITutorialWrapping  
  2. {  
  3.     virtual int Func1(int a) = 0;  
  4. };  
  5.   
  6. class TutorialWrappingClass : public ITutorialWrapping  
  7. {  
  8.     int a;  
  9.   
  10. public:  
  11.     virtual int Func1(int a);  
  12. };  
  13.   
  14. int TutorialWrappingClass::Func1(int a)  
  15. {  
  16.     return (a + 10);  
  17. }  
  18.   
  19. extern "C" __declspec(dllexportHRESULT __stdcall GetInstance(void** ppv)  
  20. {  
  21.     ITutorialWrapping* inst = new TutorialWrappingClass();  
  22.   
  23.     *ppv = inst;  
  24.   
  25.     return S_OK;  
  26. }   
  1. using System;  
  2. using System.Runtime.InteropServices;  
  3.   
  4. namespace TutorialWrapping  
  5. {  
  6.     class Program  
  7.     {  
  8.         [DllImport("TutorialWrappingDLL.dll")]
  9.             , EntryPoint = "GetInstance"  
  10.             , CharSet = CharSet.Ansi  
  11.             , CallingConvention = CallingConvention.StdCall  
  12.             )]  
  13.         [return: MarshalAs(UnmanagedType.I4)]  
  14.         extern private unsafe static int GetInstance(void** ppv);  
  15.   
  16.         static void Main(string[] args)  
  17.         {  
  18.             unsafe  
  19.             {  
  20.                 void* p = null;  
  21.                 GetInstance(&p);  
  22.             }  
  23.         }  
  24.     }  
  25. }  
 그냥 생각 나는대로 돌직구 코딩. 왈도 코딩. ㅋ
 하고나서 보니 대략 난감. 포인터 주소는 받아왔다만, 어떻게 해야 Func1을 호출 할 수 있는지 못 찾음. 삽질 좀 더 하게 될듯.

댓글 없음:

댓글 쓰기