2009/2009년 계획2009. 5. 5. 16:50
1. PPT 자료 준비
2. GDI+ 정리
3. GDI+ 책보고 조금 공부하기.
------------- 에혀 좀 푹 쉬고 싶은데.
Posted by penguindori
카테고리 없음2009. 4. 30. 14:36
http://msdn.microsoft.com/en-us/library/aa645738(vs.71).aspx [참조]
 File 1: CSharpServer.cs
using System;
using System.Runtime.InteropServices;
namespace CSharpServer
{
   // Since the .NET Framework interface and coclass have to behave as
   // COM objects, we have to give them guids.
   [Guid("DBE0E8C4-1C61-41f3-B6A4-4E2F353D3D05")]
   public interface IManagedInterface
   {
      int PrintHi(string name);
   }

   [Guid("C6659361-1625-4746-931C-36014B146679")]
   public class InterfaceImplementation : IManagedInterface
   {
      public int PrintHi(string name)
      {
         Console.WriteLine("Hello, {0}!", name);
         return 33;
      }
   }
}

File 2: COMClient.cpp
// COMClient.cpp
// Build with "cl COMClient.cpp"
// arguments: friend

#include <windows.h>
#include <stdio.h>

#pragma warning (disable: 4278)

// To use managed-code servers like the C# server,
// we have to import the common language runtime:
#import <mscorlib.tlb> raw_interfaces_only

// For simplicity, we ignore the server namespace and use named guids:
#if defined (USINGPROJECTSYSTEM)
#import "..\RegisterCSharpServerAndExportTLB\CSharpServer.tlb" no_namespace named_guids
#else  // Compiling from the command line, all files in the same directory
#import "CSharpServer.tlb" no_namespace named_guids
#endif
int main(int argc, char* argv[])
{
   IManagedInterface *cpi = NULL;
   int retval = 1;

   // Initialize COM and create an instance of the InterfaceImplementation class:
   CoInitialize(NULL);
   HRESULT hr = CoCreateInstance(CLSID_InterfaceImplementation,
               NULL, CLSCTX_INPROC_SERVER,
               IID_IManagedInterface, reinterpret_cast<void**>(&cpi));

   if (FAILED(hr))
   {
      printf("Couldn't create the instance!... 0x%x\n", hr);
   }
   else
   {
      if (argc > 1)
      {
         printf("Calling function.\n");
         fflush(stdout);
         // The variable cpi now holds an interface pointer
         // to the managed interface.
         // If you are on an OS that uses ASCII characters at the
         // command prompt, notice that the ASCII characters are
         // automatically marshaled to Unicode for the C# code.
         if (cpi->PrintHi(argv[1]) == 33)
            retval = 0;
         printf("Returned from function.\n");
      }
      else
         printf ("Usage:  COMClient <name>\n");
      cpi->Release();
      cpi = NULL;
   }

   // Be a good citizen and clean up COM:
   CoUninitialize();
   return retval;
}
위의 링크에 설명된 내용 입니다.

2. DLL 컴에 등록 시키기
regasm  명령어로 해당 dll을 a.tlb파일도 만들고 레지스터에 등록도 시킵니다.

3. MFC DLL 프로젝트 
Mailclient.cpp

//C# Instance 선언
InetSmtp *csi=NULL;
HRESULT Hr = NULL;


CMailClientApp::CMailClientApp()
{
 // TODO: 여기에 생성 코드를 추가합니다.
 // InitInstance에 모든 중요한 초기화 작업을 배치합니다.

 CoInitialize(NULL);
 Hr = CoCreateInstance(CLSID_NetSmtpLibrary,NULL,CLSCTX_INPROC_SERVER,
       IID_InetSmtp,reinterpret_cast<void**>(&csi));
 }


#if defined
(USINGPROJECTSYSTEM)
#import "..\RegisterCSharpServerAndExportTLB\netSmtpClient.tlb" no_namespace named_guids
#else
#import "netSmtpClient.tlb" no_namespace named_guids
#endif

extern "C" __declspec(dllexport) int __stdcall smtp_set_server(void *outbuf, int port)

 csi->smtpServer((wchar_t *) outbuf, port); 
 
return 0;
}

extern "C" __declspec(dllexport) int __stdcall smtp_set_address(void *outbuf, void *outbuf2)

 csi->address((wchar_t *) outbuf,(wchar_t *) outbuf2); 
 
return 0;
}


extern "C" __declspec(dllexport) int __stdcall smtp_set_subject(void *outbuf)
{
 //CString s;
 //s.Format(_T("%s"),(wchar_t *) outbuf);
 //AfxMessageBox(s);  

 
csi->Subject = (wchar_t *) outbuf; 
 return 0;
}

extern "C" __declspec(dllexport) int __stdcall smtp_set_body(void *outbuf)

 csi->Body = (wchar_t *) outbuf;
 
return 0; 
}

extern "C" __declspec(dllexport) int __stdcall smtp_set_file_append(void *outbuf)
{
  csi->FileAppend((wchar_t *) outbuf);
  return 0;
}

extern "C" __declspec(dllexport) int __stdcall smtp_send()
{
  csi->smtpSend();
  return 0;
}


MailClient.h

 #define BUFFER_SIZE_DEFAULT 1024
extern "C" __declspec(dllexport) int __stdcall smtp_set_server(void *outbuf, int port);
extern "C" __declspec(dllexport) int __stdcall smtp_set_address(void *outbuf, void *outbuf2);
extern "C" __declspec(dllexport) int __stdcall smtp_set_subject(void *outbuf);
extern "C" __declspec(dllexport) int __stdcall smtp_set_body(void *outbuf);
extern "C" __declspec(dllexport) int __stdcall smtp_set_file_append(void *outbuf);
extern "C" __declspec(dllexport) int __stdcall smtp_send();


MailClient.def

 ; MailClient.def : DLL에 대한 모듈 매개 변수를 정의합니다.
LIBRARY      "MailClient"

EXPORTS
 smtp_set_server   @1
 smtp_set_address  @2
 smtp_set_subject  @3
 smtp_set_body   @4
 smtp_set_file_append @5
 smtp_send    @6
    ; 명시적 내보내기를 여기에 사용할 수 있습니다.

위에 명시를 해줘야 다른 데서 이 MFC DLL Method 읽을 수 있다.

이제 가따 쓰면 끝 ^^;

아래는 MFC에서 대충 가따 쓰는 방법이다.
HINSTANCE hInst;

 hInst = AfxLoadLibrary(_T("MailClient.dll")); // dll 로드

 if( hInst == NULL )
 {
  AfxMessageBox( _T(" dll 로드 할 수 없습니다. "));
  return;
 }

 typedef int  (__stdcall *funcptr)(void *, int *);
 typedef int  (__stdcall *funcptr2)(void *);
 typedef int  (__stdcall *funcptr3)(void *,void *);
 typedef int  (__stdcall *funcptr4)();

funcptr2 fp;
 fp = (funcptr2)GetProcAddress(hInst,LPCSTR("smtp_set_subject"));
 funcptr2 fp2;
 fp2 = (funcptr2)GetProcAddress(hInst,LPCSTR("smtp_set_body"));
 funcptr4 fp3;
 fp3 = (funcptr4)GetProcAddress(hInst,LPCSTR("smtp_send"));
 funcptr fun2;
 fun2 = (funcptr)GetProcAddress(hInst,LPCSTR("smtp_set_server"));
 funcptr3 fun3;
 fun3 = (funcptr3)GetProcAddress(hInst,LPCSTR("smtp_set_address"));
 
 funcptr2 fp22;
 fp22 = (funcptr2)GetProcAddress(hInst,LPCSTR("smtp_set_file_append"));
 wchar_t buf[1024] = {0,};
 wchar_t buf1[1024] = {0,};
 wchar_t buf2[1024] = {0,};
 wchar_t buf3[1024] = {0,};
 wchar_t buf4[1024] = {0,};
 wchar_t buf5[1024] = {0,};

 wcscpy(buf2, _T("192.168.10.13")); //server host

 wcscpy(buf3, _T("sojung@naver.com")); //from addr

 wcscpy(buf4, _T("sewooim@naver.com")); //to addr

 wcscpy(buf, _T("세우야 반갑습니다.")); // title

 wcscpy(buf1, _T("하룽~")); // body
  
 wcscpy(buf5, _T("C:\\Project\\개발TEST\\Mail\\Mail\\Mail.aps")); //File
 
  int len = 25;
  int ret2 = fun2(buf2,&len); // 서버설정
  int ret3 = fun3(buf3,buf4); // 주소 설정
  int ret = fp(buf); // title 설정
  int ret1 = fp2(buf1); // body 설정
  int ret22 = fp22(buf5); //첨부파일

  int ret4 = fp3(); // 메일 발송
  
 AfxFreeLibrary( hInst );
이런식으로 사용 한다.

Posted by penguindori