아래의 질문은 "dotnetguru("http://www.dotnetguru.co.kr/")에 올린 질문이었습니다. 도움이 되지 않을까 싶어서 제 홈페이지에도 올려 봅니다. ^^
다음은 managed 환경의 샘플 Type입니다.
public class CTest
{
public void CallMethod( string text1, string text2 )
{
if ( text1 == "Test" )
{
MessageBox.Show( "text1 == 'Test'" );
}
if ( text2 == "Test" )
{
MessageBox.Show( "text2 == 'Test'" );
}
}
}
이제 위의 모듈을 컴파일해서 나온 DLL을 tlbexp.exe로 TLB 파일을 생산해 내고.
다음은 unmanaged 환경에서의 VC++ 코딩입니다.
#import "TestLib.tlb" raw_interfaces_only
// CLR 호스트하고.
// Default Domain 로드하고.
// Default Domain에서 타입 로드하고.
pDefultDomain->CreateInstance( "TestLib", "TestLib.CTest", &pHandle );
pHandle->Unwrap( &v );
CComQIPtr<TestLib::_CTest> pLibrary = v.pdispVal;
// 인자 한 개는 SysAllocString으로 할당하고.
BSTR bstrTest = ::SysAllocString( L"Test" );
pLibraray->CallMethod( bstrTest, L"Test" ); // 또 다른 인자 한 개는 그냥 전달.
---------------------------------------------------------
위와 같이 실행하면, 정상적인 경우라면 MessageBox가 2번이 떠야 합니다.
"text1 == 'Test'"
"text2 == 'Test'"
실제 실행결과는, SysAllocString 후에 전달된 인자만이 "Test"와의 비교 시에 True가 나옵니다.
제가 뭔가 알지 못하는 Interop 관련 지식이 있는 걸까요?
(Managed인 경우 .NET 1.1(VS.NET 2003)이고, Unmanaged VC++은 VS.NET 2005에서 한 것입니다. Managed를 .NET 2.0으로는 테스트 안 해봤습니다. 비슷하지 않을까 싶어서.)