Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 4개 있습니다.)

windbg - .NET Framework 스레드 개체의 COM Apartment 유형 확인하는 방법


.NET 스레드 개체의 COM Apartment 유형을 확인하는 방법이 있을까요? 검색해 보니, 다음의 글이 나왔습니다.

How to get the current apartment for a thread
; http://chenlailin.blogspot.kr/2008/02/how-to-get-current-apartment-for-thread.html

제 경우에는, Windows 8 x64 + .NET 2.0 x86 환경으로 windbg를 이용하여 실습해보았는데 약간 달라서 기록을 남겨 봅니다.




우선, windbg를 실행해서 .NET 2.0 x86 응용 프로그램에 "attach to process"로 디버거를 연결한 후, TEB 내용을 확인했습니다. (Ctrl + S 키를 눌러 심벌 경로를 미리 설정해 둡니다. SRV*c:\Symbols*http://msdl.microsoft.com/download/symbols;)

0:000> !teb
TEB at 7f1bf000
    ExceptionList:        0050eb88
    StackBase:            00510000
    StackLimit:           004e6000
    SubSystemTib:         00000000
    FiberData:            00001e00
    ArbitraryUserPointer: 00000000
    Self:                 7f1bf000
    EnvironmentPointer:   00000000
    ClientId:             000077f4 . 00003ae0
    RpcHandle:            00000000
    Tls Storage:          7f1bf02c
    PEB Address:          7f1b5000
    LastErrorValue:       2
    LastStatusValue:      c0000135
    Count Owned Locks:    0
    HardErrorMode:        0

TEB 주소를 알았으니 좀 더 구체적인 내용을 살펴보겠습니다.

0:000> dt _TEB 7f1bf000
ntdll!_TEB
   +0x000 NtTib            : _NT_TIB
   +0x01c EnvironmentPointer : (null) 
...[생략]...
   +0xf80 ReservedForOle   : 0x006fc720 Void
...[생략]...
   +0xfe4 ReservedForWdf   : (null) 

ReservedForOle 필드의 옵셋값이 여전히 0xf80으로 변함이 없군요. ^^ 좋은 소식입니다. 해당 필드의 타입이 "Void"라고 나오지만, How to get the current apartment for a thread 글에 따르면 "SOleTlsData"임을 알 수 있습니다. 그래서, 명령을 내려 보았는데... 오류가 나는군요. ^^

0:000> dt SOleTlsData 0x006fc720
Symbol SOleTlsData not found.

검색해 보니, 없어질 만한 구조체는 아닌 것 같았습니다.

SOleTlsData structure
; https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms690269(v=vs.85)

How to get the current apartment for a thread 글을 자세히 보니, "dt SOleTlsData"의 출력 결과에 ole32!SOleTlsData라는 것이 보이는군요. ^^ 이를 힌트 삼아 dll을 명시해 주니 원하는 결과를 얻을 수 있었습니다.

0:000> dt ole32!SOleTlsData 0x006fc720
...[생략]...
   +0x030 pObjServer       : (null) 
   +0x034 dwTIDCaller      : 0
   +0x038 pCurrentCtxForNefariousReaders : 0x006fcdb0 Void
   +0x03c pCurrentContext  : 0x006fcdb0 CObjectContext
   +0x040 pEmptyCtx        : 0x006fcdb0 CObjectContext
   +0x048 ContextId        : 2
   +0x050 pNativeApt       : 0x00700680 CComApartment
...[생략]...

본문에서는 이상하게도 pCurrentContext를 추적해서 들어가는데요. 그럴 필요 없이 0x50 옵셋에 위치한 CComApartment 구조체를 바로 추적하는 것이 더 낫다는 것을 알게 되었습니다.

CComApartment Class
; https://learn.microsoft.com/en-us/cpp/atl/reference/ccomapartment-class

최종적으로 우리가 원하는 Apartment 유형을 구할 수 있습니다.

0:000> dt CComApartment 0x00700680 
ole32!CComApartment
   +0x000 __VFN_table : 0x77573dac 
   +0x004 _cRefs           : 4
   +0x008 _dwState         : 0
   +0x00c _AptKind         : 2 ( APTKIND_MULTITHREADED )
   ...[생략]...
   =76e20000 s_ulApartmentIdentifierCounter : 0x905a4d
   =76e20000 s_ShutdownRegLock : COleStaticMutexSem

0x0c 옵셋에 있는 _AptKind의 값으로는 다음과 같은 유형들이 있습니다.

// combase.h
// ; http://www.brandonfa.lk/win8/win8_devrel_head_x86/combase.h

enum tagAPTKIND {
    APTKIND_NEUTRALTHREADED = 1,
    APTKIND_MULTITHREADED = 2,
    APTKIND_APARTMENTTHREADED = 4,
    APTKIND_APPLICATION_STA = 8
};

본문에서는 명령어 하나로 알아내는 것이 다음과 같이 복잡했는데요.

// FIELD_OFFSET
// ; https://jeho.page/programming/2011/03/01/FIELD_OFFSET-%EB%A7%A4%ED%81%AC%EB%A1%9C.html

dt ole32!CComApartment poi(@@C++(#FIELD_OFFSET(ole32!CObjectContext,_pApartment))+poi(poi($thread+@@C++(#FIELD_OFFSET(ole32!_TEB, ReservedForOle)))+@@C++(#FIELD_OFFSET(ole32!SOleTlsData,pCurrentContext))))))

이것을 다음과 같이 좀 더 간단하게 고쳐줄 수 있습니다. ^^

dt ole32!CComApartment poi(@@C++(#FIELD_OFFSET(ole32!SOleTlsData,pNativeApt))+poi($thread+@@C++(#FIELD_OFFSET(ole32!_TEB, ReservedForOle))))




이 과정을 이용해서 .NET 응용 프로그램에서도 쉽게 결과를 구할 수 있습니다.

TEB 구하는 것은 예전에도 한번 언급해 드렸죠? ^^

.NET System.Threading.Thread 개체에서 Native Thread Id를 구할 수 있을까?
; https://www.sysnet.pe.kr/2/0/1244

이를 이용한 전체 소스 코드는 다음과 같습니다.

class Program
{
    [DllImport("ntdll.dll")]
    static extern IntPtr NtCurrentTeb();

    static void Main(string[] args)
    {
        if (IntPtr.Size == 8)
        {
            return; // not supported yet.
        }

        IntPtr pTeb = NtCurrentTeb();
        Console.WriteLine("TEB: " + pTeb.ToInt64().ToString("x"));
        Console.WriteLine("dt _TEB " + pTeb.ToInt64().ToString("x"));

        IntPtr reservedForOle = new IntPtr(pTeb.ToInt64() + 0xf80);
        int ReservedForOle = Marshal.ReadInt32(reservedForOle); // TEB.ReservedForOle
        Console.WriteLine("TEB.ReservedForOle: " + ReservedForOle.ToString("x"));
        Console.WriteLine("dt ole32!SOleTlsData " + ReservedForOle.ToString("x"));

        IntPtr pNativeApt = new IntPtr(ReservedForOle + 0x50);
        int NativeApt = Marshal.ReadInt32(pNativeApt); // SOleTlsData.pNativeApt
        Console.WriteLine("SOleTlsData.pNativeApt: " + NativeApt.ToString("x"));
        Console.WriteLine("dt CComApartment " + NativeApt.ToString("x"));

        IntPtr pAptKind = new IntPtr(NativeApt + 0x0c);
        int AptKind = Marshal.ReadInt32(pAptKind); // CComApartment._AptKind
        Console.WriteLine("CComApartment._AptKind: " + AptKind.ToString("x"));

        Console.WriteLine("Press any key to exit...");
        Console.ReadLine();
    }
}





[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 4/29/2023]

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

비밀번호

댓글 작성자
 




1  2  3  4  5  6  7  8  9  10  11  12  13  [14]  15  ...
NoWriterDateCnt.TitleFile(s)
13296정성태3/25/20234103Windows: 234. IsDialogMessage와 협업하는 WM_GETDLGCODE Win32 메시지 [1]파일 다운로드1
13295정성태3/24/20234334Windows: 233. Win32 - modeless 대화창을 modal처럼 동작하게 만드는 방법파일 다운로드1
13294정성태3/22/20234494.NET Framework: 2105. LargeAddressAware 옵션이 적용된 닷넷 32비트 프로세스의 가용 메모리 - 두 번째
13293정성태3/22/20234527오류 유형: 853. dumpbin - warning LNK4048: Invalid format file; ignored
13292정성태3/21/20234722Windows: 232. C/C++ - 일반 창에도 사용 가능한 IsDialogMessage파일 다운로드1
13291정성태3/20/20235054.NET Framework: 2104. C# Windows Forms - WndProc 재정의와 IMessageFilter 사용 시의 차이점
13290정성태3/19/20234584.NET Framework: 2103. C# - 윈도우에서 기본 제공하는 FindText 대화창 사용법파일 다운로드1
13289정성태3/18/20233813Windows: 231. Win32 - 대화창 템플릿의 2진 리소스를 읽어들여 자식 윈도우를 생성하는 방법파일 다운로드1
13288정성태3/17/20233934Windows: 230. Win32 - 대화창의 DLU 단위를 pixel로 변경하는 방법파일 다운로드1
13287정성태3/16/20234089Windows: 229. Win32 - 대화창 템플릿의 2진 리소스를 읽어들여 윈도우를 직접 띄우는 방법파일 다운로드1
13286정성태3/15/20234553Windows: 228. Win32 - 리소스에 포함된 대화창 Template의 2진 코드 해석 방법
13285정성태3/14/20234087Windows: 227. Win32 C/C++ - Dialog Procedure를 재정의하는 방법파일 다운로드1
13284정성태3/13/20234342Windows: 226. Win32 C/C++ - Dialog에서 값을 반환하는 방법파일 다운로드1
13283정성태3/12/20233780오류 유형: 852. 파이썬 - TypeError: coercing to Unicode: need string or buffer, NoneType found
13282정성태3/12/20234098Linux: 58. WSL - nohup 옵션이 필요한 경우
13281정성태3/12/20234116Windows: 225. 윈도우 바탕화면의 아이콘들이 넓게 퍼지는 경우 [2]
13280정성태3/9/20234844개발 환경 구성: 670. WSL 2에서 호스팅 중인 TCP 서버를 외부에서 접근하는 방법
13279정성태3/9/20234317오류 유형: 851. 파이썬 ModuleNotFoundError: No module named '_cffi_backend'
13278정성태3/8/20234352개발 환경 구성: 669. WSL 2의 (init이 아닌) systemd 지원 [1]
13277정성태3/6/20235058개발 환경 구성: 668. 코드 사인용 인증서 신청 및 적용 방법(예: Digicert)
13276정성태3/5/20234711.NET Framework: 2102. C# 11 - ref struct/ref field를 위해 새롭게 도입된 scoped 예약어
13275정성태3/3/20234950.NET Framework: 2101. C# 11의 ref 필드 설명
13274정성태3/2/20234542.NET Framework: 2100. C# - ref 필드로 ref struct 타입을 허용하지 않는 이유
13273정성태2/28/20234314.NET Framework: 2099. C# - 관리 포인터로서의 ref 예약어 의미
13272정성태2/27/20234554오류 유형: 850. SSMS - mdf 파일을 Attach 시킬 때 Operating system error 5: "5(Access is denied.)" 에러
13271정성태2/25/20234536오류 유형: 849. Sql Server Configuration Manager가 시작 메뉴에 없는 경우
1  2  3  4  5  6  7  8  9  10  11  12  13  [14]  15  ...