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)
13610정성태4/28/2024210닷넷: 2251. C# - 제네릭 인자를 가진 타입을 생성하는 방법 - 두 번째 이야기
13609정성태4/27/2024235닷넷: 2250. PInvoke 호출 시 참조 타입(class)을 마샬링하는 [IN], [OUT] 특성파일 다운로드1
13608정성태4/26/2024427닷넷: 2249. C# - 부모의 필드/프로퍼티에 대해 서로 다른 자식 클래스 간에 Reflection 접근이 동작할까요?파일 다운로드1
13607정성태4/25/2024501닷넷: 2248. C# - 인터페이스 타입의 다중 포인터를 인자로 갖는 C/C++ 함수 연동
13606정성태4/24/2024681닷넷: 2247. C# - tensorflow 연동 (MNIST 예제)파일 다운로드1
13605정성태4/23/2024793닷넷: 2246. C# - Python.NET을 이용한 파이썬 소스코드 연동파일 다운로드1
13604정성태4/22/2024846오류 유형: 901. Visual Studio - Unable to set the next statement. Set next statement cannot be used in '[Exception]' call stack frames.
13603정성태4/21/2024956닷넷: 2245. C# - IronPython을 이용한 파이썬 소스코드 연동파일 다운로드1
13602정성태4/20/2024974닷넷: 2244. C# - PCM 오디오 데이터를 연속(Streaming) 재생 (Windows Multimedia)파일 다운로드1
13601정성태4/19/2024994닷넷: 2243. C# - PCM 사운드 재생(NAudio)파일 다운로드1
13600정성태4/18/20241015닷넷: 2242. C# - 관리 스레드와 비관리 스레드
13599정성태4/17/2024951닷넷: 2241. C# - WAV 파일의 PCM 사운드 재생(Windows Multimedia)파일 다운로드1
13598정성태4/16/2024995닷넷: 2240. C# - WAV 파일 포맷 + LIST 헤더파일 다운로드2
13597정성태4/15/20241003닷넷: 2239. C# - WAV 파일의 PCM 데이터 생성 및 출력파일 다운로드1
13596정성태4/14/20241117닷넷: 2238. C# - WAV 기본 파일 포맷파일 다운로드1
13595정성태4/13/20241072닷넷: 2237. C# - Audio 장치 열기 (Windows Multimedia, NAudio)파일 다운로드1
13594정성태4/12/20241095닷넷: 2236. C# - Audio 장치 열람 (Windows Multimedia, NAudio)파일 다운로드1
13593정성태4/8/20241095닷넷: 2235. MSBuild - AccelerateBuildsInVisualStudio 옵션
13592정성태4/2/20241232C/C++: 165. CLion으로 만든 Rust Win32 DLL을 C#과 연동
13591정성태4/2/20241209닷넷: 2234. C# - WPF 응용 프로그램에 Blazor App 통합파일 다운로드1
13590정성태3/31/20241091Linux: 70. Python - uwsgi 응용 프로그램이 k8s 환경에서 OOM 발생하는 문제
13589정성태3/29/20241196닷넷: 2233. C# - 프로세스 CPU 사용량을 나타내는 성능 카운터와 Win32 API파일 다운로드1
13588정성태3/28/20241544닷넷: 2232. C# - Unity + 닷넷 App(WinForms/WPF) 간의 Named Pipe 통신 [2]파일 다운로드1
13587정성태3/27/20241397오류 유형: 900. Windows Update 오류 - 8024402C, 80070643
13586정성태3/27/20241608Windows: 263. Windows - 복구 파티션(Recovery Partition) 용량을 늘리는 방법
[1]  2  3  4  5  6  7  8  9  10  11  12  13  14  15  ...