Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 


다음과 같이 VS.NET 2005에서 C#으로 간단하게 Class Library를 만들었습니다.

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ClassLibrary1
{
    [ComVisible(true)]
    [Guid("23172f2f-a3d3-4180-97ae-7805f74a5a45")]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class SecureWrap
    {
        public bool CheckLicense()
        {
            MessageBox.Show("1111111111", "222222222222", MessageBoxButtons.OK);
            return true;
        }
    }
}

컴파일해서 나온 내용을 아래와 같이 레지스트리에 등록하고 TLB 파일을 얻어냈습니다.

    regasm ClassLibrary1.dll /codebase /tlb:ClassLibrary1.tlb

SecureWrap 클래스를 Native VC++에서 사용하는 예제를 만들었습니다. 간단한 Console Application입니다.
아래와 같이 간단하게 코드를 작성하고 컴파일을 하였습니다.

#include "stdafx.h"
#import "..\ClassLibrary1\bin\Debug\ClassLibrary1.tlb" no_namespace

void _tmain(int argc, _TCHAR* argv[])
{
	CoInitialize( NULL );

	{	
		_SecureWrapPtr ptr;
		ptr.CreateInstance( __uuidof( SecureWrap ) );

		VARIANT_BOOL vtBool;
		ptr->CheckLicense( &vtBool );
	}

	CoUninitialize();
}

하지만, 위와 같이 하게 되면 다음과 같은 오류들이 발생하게 됩니다.

d:\temp2\consolecpp\debug\classlibrary1.tlh(62) : error C2146: syntax error : missing ';' before identifier 'GetType'
d:\temp2\consolecpp\debug\classlibrary1.tlh(62) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\temp2\consolecpp\debug\classlibrary1.tlh(62) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\temp2\consolecpp\debug\classlibrary1.tlh(62) : warning C4183: 'GetType': missing return type; assumed to be a member function returning 'int'
d:\temp2\consolecpp\debug\classlibrary1.tli(35) : error C2143: syntax error : missing ';' before '_SecureWrap::GetType'
d:\temp2\consolecpp\debug\classlibrary1.tli(35) : error C2433: '_TypePtr' : 'inline' not permitted on data declarations
d:\temp2\consolecpp\debug\classlibrary1.tli(35) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\temp2\consolecpp\debug\classlibrary1.tli(35) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\temp2\consolecpp\debug\classlibrary1.tli(39) : error C2064: term does not evaluate to a function taking 2 arguments
d:\temp2\consolecpp\consolecpp.cpp(19) : error C2660: '_SecureWrap::CheckLicense' : function does not take 1 arguments

첫 번째 오류의 GetType 메서드 정의를 TLH 파일에서 찾아보면 아래와 같이 나오는 것을 볼 수 있습니다.

    _TypePtr GetType ( );

_Type에 대해서 Smart Pointer 처리가 된 것을 볼 수 있습니다. 하지만, 위의 콘솔 애플리케이션의 어느 곳에서도 _Type에 대한 Smart Pointer 정의를 발견할 수 없기 때문에 이러한 오류가 나오는 것인데요. 이를 해결하기 위해서는 2가지 방법을 선택할 수 있습니다.

첫 번째 방법은, _TypePtr 형식이 사용되지 않도록 Raw Interface 유형으로 TLH 파일을 생성하는 것입니다. 다음과 같이 import 지시자에 옵션을 하나 더 주게 되면 이를 가능하게 해줍니다.

#import "..\ClassLibrary1\bin\Debug\ClassLibrary1.tlb" raw_interfaces_only  no_namespace

두 번째 방법은, _TypePtr 형식이 컴파일되도록 Smart Pointer 처리를 해주는 것입니다. 이러한 처리는 개발자가 직접 _COM_SMARTPTR_TYPEDEF을 해줄 수도 있겠지만, import로 생성된 TLH 파일의 상단 주석 부분을 보면 어떻게 해야 하는지 친절하게 가르쳐 주고 있습니다.

// compiler-generated file created 05/02/06 at 23:08:07 - DO NOT EDIT!

//
// Cross-referenced type libraries:
//
//  #import "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorlib.tlb"
//

이를 적용하여 다음과 같이 간단하게 호출을 할 수 있습니다.

#include "stdafx.h"


#import "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorlib.tlb" no_namespace
#import "..\ClassLibrary1\bin\Debug\ClassLibrary1.tlb" no_namespace

void _tmain(int argc, _TCHAR* argv[])
{
	CoInitialize( NULL );

	{	
		_SecureWrapPtr ptr;
		ptr.CreateInstance( __uuidof( SecureWrap ) );
		ptr->CheckLicense();
	}

	CoUninitialize();
}







[최초 등록일: ]
[최종 수정일: 6/26/2021]

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

비밀번호

댓글 작성자
 




... 181  182  183  [184]  185  186  187  188  189  190  191  192  193  194  195  ...
NoWriterDateCnt.TitleFile(s)
354정성태10/23/200635815    답변글 개발 환경 구성: 18.1. 윈도우즈 인증서 서비스 설치
372정성태12/31/200637653    답변글 개발 환경 구성: 18.2. 웹 사이트에 SSL을 적용 [3]
373정성태10/24/200629053    답변글 개발 환경 구성: 18.3. 사용자 입장에서의 HTTPS 접근 (1)
374정성태10/25/200626324    답변글 개발 환경 구성: 18.4. 사용자 입장에서의 HTTPS 접근 (2)
391정성태11/7/200630446    답변글 개발 환경 구성: 18.5. 사용자 인증서 발급
392정성태11/11/200643715    답변글 개발 환경 구성: 18.6. 인증서 관리 (1) - 내보내기/가져오기
394정성태11/9/200628213    답변글 개발 환경 구성: 18.7. 인증서 관리 (2) - 개인키를 내보낼 수 있는 유형의 인증서 발급 [1]
395정성태11/9/200640321    답변글 개발 환경 구성: 18.8. 인증서 관리 (3) - 인증서 MMC 관리자 사용
414정성태12/23/200632003    답변글 개발 환경 구성: 18.9. CRL(Certificate Revocation List) 관리
428정성태12/31/200644919    답변글 개발 환경 구성: 18.10. IIS 7 - SSL 사이트 설정하는 방법 [4]
429정성태12/31/200630849    답변글 개발 환경 구성: 18.11. 서비스를 위한 인증서 설치
352정성태10/2/200620526개발 환경 구성: 17. VPC에 Linux 설치하는 방법 [1]
351정성태10/8/200623086개발 환경 구성: 16. 성태의 무식한(!) 리눅스 탐방기. [4]
349정성태9/26/200621888디버깅 기술: 10. C++/CLI에서 제공되는 명시적인 파괴자의 비밀
347정성태10/6/200625642디버깅 기술: 9. .NET IDisposable 처리 정리 [1]
346정성태9/23/200619170개발 환경 구성: 15. 툴박스에 컨트롤이 자동으로 나타나도록 해주는 옵션 설정
345정성태9/20/200618315오류 유형: 12. WCF 오류 메시지 - Error while trying to reflect on attribute 'MessageContractAttribute'
343정성태10/18/200630179개발 환경 구성: 14. SandCastle 사용법 (NDoc을 대체하는 문서화 도구) [1]파일 다운로드1
344정성태9/20/200620445    답변글 개발 환경 구성: 14.1. 오류 유형 - GAC 에 등록된 DLL 에 대한 문서화 시 오류
340정성태9/15/200619618개발 환경 구성: 13. ISO 파일을 가상 CD-ROM으로 매핑해주는 프로그램
339정성태9/14/200619104오류 유형: 11. ProtocolsSection?
338정성태2/4/200727344개발 환경 구성: 12. BUG: 웹 서비스에서 DataTable 사용하기 [2]파일 다운로드1
350정성태10/2/200620557    답변글 개발 환경 구성: 12.1. ASMX 2.0 and SchemaImporterExtensions파일 다운로드1
335정성태8/20/200628291디버깅 기술: 8. COM+ 서버 응용 프로그램에 대한 F5 디버깅 방법
334정성태8/20/200623524디버깅 기술: 7. VS.NET 2003/2005의 다중 프로젝트 디버깅
333정성태8/20/200624013개발 환경 구성: 11. COM+ 서버 활성화 보안 설정
... 181  182  183  [184]  185  186  187  188  189  190  191  192  193  194  195  ...