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)
1121정성태9/12/201127569Java: 5. WTP 내에서 서블릿을 실행하는 환경
1120정성태9/11/201127484.NET Framework: 239. IHttpHandler.IsReusable 속성 이야기파일 다운로드1
1119정성태9/11/201126612Java: 4. 이클립스에 WTP SDK가 설치되지 않는다면? [2]
1118정성태9/11/201138256Java: 3. 이클립스에서 서블릿 디버깅하는 방법 [4]
1117정성태9/9/201125549제니퍼 .NET: 17. 제니퍼 닷넷 적용 사례 (2) - 웹 애플리케이션 hang의 원인을 알려주다.
1116정성태9/8/201156634Java: 2. 자바에서 "Microsoft SQL Server JDBC Driver" 사용하는 방법
1115정성태9/4/201130104Java: 1. 닷넷 개발자가 처음 실습해 본 서블릿
1114정성태9/4/201134614Math: 2. "Zhang Suen 알고리즘(세선화, Thinning/Skeletonization)"의 C# 버전 [4]파일 다운로드1
1113정성태9/2/201134201개발 환경 구성: 129. Hyper-V에 CentOS 설치하기
1112정성태9/2/201150934Linux: 1. 리눅스 <-> 윈도우 원격 접속 프로그램 사용 [3]
1111정성태8/29/201125402제니퍼 .NET: 16. 적용 사례 (1) - DB Connection Pooling을 사용하지 않았을 때의 성능 저하를 알려주다. [1]
1110정성태8/26/201126736오류 유형: 136. RDP 접속이 불연속적으로 끊기는 문제
1109정성태8/26/201129582오류 유형: 135. 어느 순간 Active Directory 접속이 안되는 문제
1108정성태8/22/201131169오류 유형: 134. OLE/COM Object Viewer - DllRegisterServer in IVIEWERS.DLL failed. [1]
1107정성태8/21/201128889디버깅 기술: 43. Windows Form의 Load 이벤트에서 발생하는 예외가 Visual Studio에서 잡히지 않는 문제
1106정성태8/20/201127210웹: 26. FailedRequestTracing 설정으로 인한 iisexpress.exe 비정상 종료 문제
1105정성태8/19/201127140.NET Framework: 238. Web Site Model 프로젝트에서 Trace.WriteLine 출력이 dbgview.exe에서 확인이 안 되는 문제파일 다운로드1
1104정성태8/19/201127297웹: 25. WebDev보다 IIS Express가 더 나은 점 - 다중 가상 디렉터리 매핑 [1]
1103정성태8/19/201133192오류 유형: 133. WCF 포트 바인딩 실패 오류 - TCP error(10013) [1]
1102정성태8/19/201130960Math: 1. 방탈출3 - Room 10의 '중복가능한 조합' 문제를 위한 C# 프로그래밍 [2]파일 다운로드1
1101정성태8/19/201129636.NET Framework: 237. WCF AJAX 서비스와 JavaScript 간의 DateTime 연동 [1]파일 다운로드1
1100정성태8/17/201128745.NET Framework: 236. SqlDbType - DateTime, DateTime2, DateTimeOffset의 차이점파일 다운로드1
1099정성태8/15/201128159오류 유형: 132. 어느 순간 갑자기 접속이 안 되는 TFS 서버
1098정성태8/15/201150169웹: 24. 네이버는 어떻게 로그인 처리를 할까요? [2]
1097정성태8/15/201121477.NET Framework: 235. 메서드의 메타 데이터 토큰 값으로 클래스를 찾아내는 방법
1096정성태8/15/201125637디버깅 기술: 42. Watson Bucket 정보를 이용한 CLR 응용 프로그램 예외 분석 - (2)
... 151  152  153  154  155  156  [157]  158  159  160  161  162  163  164  165  ...