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 컴에 등록 시키기
3. MFC DLL 프로젝트
Mailclient.cpp
//C# Instance 선언 extern "C" __declspec(dllexport) int __stdcall smtp_set_address(void *outbuf, void *outbuf2)
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.h
#define BUFFER_SIZE_DEFAULT 1024 |
MailClient.def
; MailClient.def : DLL에 대한 모듈 매개 변수를 정의합니다. |
위에 명시를 해줘야 다른 데서 이 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 );
이런식으로 사용 한다.