Microsoft MVP성태의 닷넷 이야기
.NET Framework: 3.1. Interface를 사용하면. [링크 복사], [링크+제목 복사],
조회: 24509
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
부모글 보이기/감추기


.NET Class를 Native 클라이언트에게 제공하는 목적으로는 Interface를 사용하도록 권장하고 있습니다.
그래야 vtable의 순서 등을 정확하게 유지하는 contract를 보장할 수 있기 때문인데요.

방법은 그리 어렵지 않습니다. 해당 클래스를 Interface로부터 상속받도록 하고, ClassInterfaceType.None을 지정해 주시면 됩니다.

다음은 그에 대한 코드입니다.

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")]
    [ClassInterface(ClassInterfaceType.None)]
    public class SecureWrap : ISecure
    {
        public bool CheckLicense()
        {
            MessageBox.Show("1111111111", "222222222222", MessageBoxButtons.OK);
            return true;
        }
    }
}

이에 대한 TLB 파일을 Visual C++에서 import 시키면 다음과 같이 간결한 구조의 TLH 파일이 생성됩니다.

#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)

_TypePtr 등의 Smart Pointer 정의도 필요 없게 되었음을 확인할 수 있습니다.
이를 직접 사용하게 되는 Visual C++ 코드 역시 다음과 같이 간단하게 이뤄집니다.

#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();
}







[최초 등록일: ]
[최종 수정일: 3/20/2023]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2011-10-22 06시50분
CoInitialize 없이 C# COM 개체를 생성하는 경우, Visual Studio 출력 창에 다음과 같은 디버그 메시지가 나올 수 있습니다.

First-chance exception at 0x7561b9bc in [...].exe: Microsoft C++ exception: _com_error at memory location 0x002be9f4..
정성태

... 151  152  153  154  155  156  157  158  159  160  [161]  162  163  164  165  ...
NoWriterDateCnt.TitleFile(s)
1065정성태6/13/201134828개발 환경 구성: 125. Firebird - 유니코드 기본 문자셋 지정
1064정성태6/11/201129520웹: 22. Visual Studio 2010에서 CSS 3 인텔리센스(intellisense) 지원하는 방법 [1]
1063정성태6/10/201131150웹: 21. Sysnet 웹 사이트의 CSS 2.1 변환 기록 [1]
1062정성태6/9/201131430웹: 20. Sysnet 웹 사이트의 HTML5 변환 기록 [1]
1061정성태6/8/201129406오류 유형: 125. 인터넷 익스플로러 - 개발자 도구에서 정지점(BP: Breakpoint) 설정이 안 되는 경우 [1]
1060정성태6/8/201126124VC++: 51. PHP 모듈의 F5 디버깅
1059정성태6/6/201131404VC++: 50. PHP 모듈 - php_mysql 빌드하는 방법파일 다운로드1
1058정성태6/5/201134876개발 환경 구성: 124. .NET 개발자가 처음 해보는 PHP + MySQL 연동 [2]
1057정성태6/4/201132330VC++: 49. 소스 코드로부터 php5apache2_2.dll 생성하는 방법파일 다운로드1
1056정성태6/2/201130537VC++: 48. 윈도우에서 Apache Module - Content Handler 컴파일파일 다운로드1
1055정성태6/1/201127845오류 유형: 124. MVC 프로젝트의 Site.Master 관련 오류 정리
1054정성태5/31/201131795.NET Framework: 220. ASP.NET MVC Web Site 프로젝트 - 단위 테스트 작성파일 다운로드1
1053정성태5/31/201134459VC++: 47. Apache Module에 대한 'F5 디버그 (Start with debugging)' [2]
1052정성태5/30/201131996.NET Framework: 219. ASP.NET MVC Web Site 프로젝트 구성하기파일 다운로드1
1051정성태5/28/201140557VC++: 46. 윈도우에서 Apache Module 컴파일 (VC++)파일 다운로드1
1050정성태5/28/201126727오류 유형: 123. Firebird - Exception of type 'FirebirdSql.Data.Common.IscException' was thrown.
1049정성태5/28/201132404.NET Framework: 218. WCF REST 서비스 - 웹 브라우저 측 Ajax 호출 캐시 [1]
1048정성태5/27/201133902개발 환경 구성: 123. Apache 소스를 윈도우 환경에서 빌드하기
1047정성태5/27/201128168.NET Framework: 217. Firebird ALinq Provider - 날짜 필드에 대한 낙관적 동시성 쿼리 오류
1046정성태5/26/201132799.NET Framework: 216. 라이선스까지도 뛰어넘는 .NET Profiler [5]
1045정성태5/24/201133880.NET Framework: 215. 닷넷 System.ComponentModel.LicenseManager를 이용한 라이선스 적용 [1]파일 다운로드1
1044정성태5/24/201134513오류 유형: 122. zlib 빌드 오류 - inflate.obj : error LNK2001: unresolved external symbol _inflate_fast
1043정성태5/24/201134374.NET Framework: 214. 무료 Linq Provider - DbLinq를 이용한 Firebird 접근파일 다운로드1
1042정성태5/23/201139748개발 환경 구성: 122. PHP 소스를 윈도우 환경에서 빌드하기
1041정성태5/22/201130629.NET Framework: 213. Linq To SQL - ALinq Provider를 이용하여 Firebird 사용파일 다운로드1
1040정성태5/21/201141134개발 환경 구성: 121. .NET 개발자가 처음 설치해 본 Apache + PHP [2]
... 151  152  153  154  155  156  157  158  159  160  [161]  162  163  164  165  ...