성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Roll A Lisp In C - Reading ; https...
[정성태] Java - How to use the Foreign Funct...
[정성태] 제가 큰 실수를 했군요. ^^; Delegate를 통한 Bein...
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<br /> .NET Class를 Native 클라이언트에게 제공하는 목적으로는 Interface를 사용하도록 권장하고 있습니다.<br /> 그래야 vtable의 순서 등을 정확하게 유지하는 contract를 보장할 수 있기 때문인데요.<br /> <br /> 방법은 그리 어렵지 않습니다. 해당 클래스를 Interface로부터 상속받도록 하고, ClassInterfaceType.None을 지정해 주시면 됩니다.<br /> <br /> 다음은 그에 대한 코드입니다.<br /> <br /> <pre class="code"> namespace ClassLibrary1 { [ComVisible(true)] [Guid("23172f2f-a3d3-4180-97ae-7805f74a5a46")] [InterfaceType(ComInterfaceType.InterfaceIsDual)] public interface ISecure { bool CheckLicense(); } [ComVisible(true)] [Guid("23172f2f-a3d3-4180-97ae-7805f74a5a45")] <b>[ClassInterface(ClassInterfaceType.None)]</b> public class SecureWrap : <b>ISecure</b> { public bool CheckLicense() { MessageBox.Show("1111111111", "222222222222", MessageBoxButtons.OK); return true; } } } </pre> <br /> 이에 대한 TLB 파일을 Visual C++에서 import 시키면 다음과 같이 간결한 구조의 TLH 파일이 생성됩니다.<br /> <br /> <pre class="code"> #pragma once #pragma pack(push, 8) #include <comdef.h> struct __declspec(uuid("23172f2e-a3d3-4180-97ae-7805f74a5a49")) /* LIBID */ __ClassLibrary1; struct __declspec(uuid("23172f2f-a3d3-4180-97ae-7805f74a5a46")) /* dual interface */ ISecure; struct /* coclass */ SecureWrap; _COM_SMARTPTR_TYPEDEF(ISecure, __uuidof(ISecure)); struct __declspec(uuid("23172f2f-a3d3-4180-97ae-7805f74a5a46")) ISecure : IDispatch { VARIANT_BOOL CheckLicense ( ); virtual HRESULT __stdcall raw_CheckLicense ( /*[out,retval]*/ VARIANT_BOOL * pRetVal ) = 0; }; struct __declspec(uuid("23172f2f-a3d3-4180-97ae-7805f74a5a45")) SecureWrap; #include "d:\temp2\consolecpp\debug\classlibrary1.tli" #pragma pack(pop) </pre> <br /> _TypePtr 등의 Smart Pointer 정의도 필요 없게 되었음을 확인할 수 있습니다.<br /> 이를 직접 사용하게 되는 Visual C++ 코드 역시 다음과 같이 간단하게 이뤄집니다.<br /> <br /> <pre class="code"> #include "stdafx.h" #import "..\ClassLibrary1\bin\Debug\ClassLibrary1.tlb" no_namespace void _tmain(int argc, _TCHAR* argv[]) { CoInitialize( NULL ); { ISecurePtr ptr = ISecurePtr( __uuidof( SecureWrap ) ); ptr->CheckLicense(); } CoUninitialize(); } </pre>
첨부파일
스팸 방지용 인증 번호
1200
(왼쪽의 숫자를 입력해야 합니다.)