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

비밀번호

댓글 작성자
 




... 151  152  153  154  155  156  157  158  159  [160]  161  162  163  164  165  ...
NoWriterDateCnt.TitleFile(s)
1045정성태5/24/201131719.NET Framework: 215. 닷넷 System.ComponentModel.LicenseManager를 이용한 라이선스 적용 [1]파일 다운로드1
1044정성태5/24/201132293오류 유형: 122. zlib 빌드 오류 - inflate.obj : error LNK2001: unresolved external symbol _inflate_fast
1043정성태5/24/201131241.NET Framework: 214. 무료 Linq Provider - DbLinq를 이용한 Firebird 접근파일 다운로드1
1042정성태5/23/201137588개발 환경 구성: 122. PHP 소스를 윈도우 환경에서 빌드하기
1041정성태5/22/201128482.NET Framework: 213. Linq To SQL - ALinq Provider를 이용하여 Firebird 사용파일 다운로드1
1040정성태5/21/201138832개발 환경 구성: 121. .NET 개발자가 처음 설치해 본 Apache + PHP [2]
1039정성태5/17/201131547.NET Framework: 212. Firebird 데이터베이스와 ADO.NET [2]파일 다운로드1
1038정성태5/16/201133499개발 환경 구성: 120. .NET 프로그래머에게도 유용한 Firebird 무료 데이터베이스 [2]
1037정성태5/11/201128326개발 환경 구성: 119. Visual Studio Professional 이하 버전에서도 TFS의 정적 코드 분석 정책 연동이 가능할까? [3]
1036정성태5/7/201194184오류 유형: 121. Access DB에 대한 32bit/64bit OLE DB Provider 관련 오류 [11]
1035정성태5/7/201128869오류 유형: 120. File cannot be opened. Ensure it is a valid Data Link file.
1034정성태5/2/201125922.NET Framework: 211. 파일 잠금 없이 .NET 어셈블리의 버전을 구하는 방법 [2]파일 다운로드1
1033정성태5/1/201131654웹: 19. IIS Express - appcmd.exe를 이용한 applicationHost.config 변경 [2]
1032정성태5/1/201128280웹: 18. IIS Express를 NT 서비스로 변경
1031정성태4/30/201129423웹: 17. IIS Express - "IIS Installed Versions Manager Interface"의 IIISExpressProcessUtility 구하는 방법 [1]파일 다운로드1
1030정성태4/30/201151749개발 환경 구성: 118. IIS Express - localhost 이외의 호스트 이름으로 접근하는 방법 [4]파일 다운로드1
1029정성태4/28/201140873개발 환경 구성: 117. XCopy에서 파일/디렉터리 확인 질문 없애기 [2]
1028정성태4/27/201138243오류 유형: 119. Visual Studio 2010 SP1 설치 후 Windows Phone 개발자 도구로 인한 재설치 문제 [3]
1027정성태4/25/201127416디버깅 기술: 40. 상황별 GetFunctionPointer 반환값 정리 - x86파일 다운로드1
1026정성태4/25/201145687디버깅 기술: 39. DebugDiag 1.1을 사용한 덤프 분석 [7]
1025정성태4/24/201127749개발 환경 구성: 116. IIS 7 관리자 - Active Directory Certification Authority로부터 SSL 사이트 인증서 받는 방법 [2]
1024정성태4/22/201129159오류 유형: 118. Windows 2008 서버에서 Event Viewer / PowerShell 실행 시 비정상 종료되는 문제 [1]
1023정성태4/20/201130004.NET Framework: 210. Windbg 환경에서 확인해 본 .NET 메서드 JIT 컴파일 전과 후 [1]
1022정성태4/19/201125586디버깅 기술: 38. .NET Disassembly 창에서의 F11(Step-into) 키 동작파일 다운로드1
1021정성태4/18/201127836디버깅 기술: 37. .NET 4.0 응용 프로그램의 Main 함수에 BreakPoint 걸기
1020정성태4/18/201128477오류 유형: 117. Failed to find runtime DLL (mscorwks.dll), 0x80004005
... 151  152  153  154  155  156  157  158  159  [160]  161  162  163  164  165  ...