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)
13397정성태7/23/20233990닷넷: 2134. C# - 문자열 연결 시 string.Create를 이용한 GC 할당 최소화
13396정성태7/22/20233758스크립트: 54. 파이썬 pystack 소개 - 메모리 덤프로부터 콜 스택 열거
13395정성태7/20/20233563개발 환경 구성: 685. 로컬에서 개발 중인 ASP.NET Core/5+ 웹 사이트에 대해 localhost 이외의 호스트 이름으로 접근하는 방법
13394정성태7/16/20233536오류 유형: 873. Oracle.ManagedDataAccess.Client - 쿼리 수행 시 System.InvalidOperationException
13393정성태7/16/20233797닷넷: 2133. C# - Oracle 데이터베이스의 Sleep 쿼리 실행하는 방법
13392정성태7/16/20233652오류 유형: 872. Oracle - ORA-01031: insufficient privileges
13391정성태7/14/20233704닷넷: 2132. C# - sealed 클래스의 메서드를 callback 호출했을 때 인라인 처리가 될까요?
13390정성태7/12/20233574스크립트: 53. 파이썬 - localhost 호출 시의 hang 현상
13389정성태7/5/20233634개발 환경 구성: 684. IIS Express로 호스팅하는 웹을 WSL 환경에서 접근하는 방법
13388정성태7/3/20233864오류 유형: 871. 윈도우 탐색기에서 열리지 않는 zip 파일 - The Compressed (zipped) Folder '[...].zip' is invalid. [1]파일 다운로드1
13387정성태6/28/20233928오류 유형: 870. _mysql - Commands out of sync; you can't run this command now
13386정성태6/27/20234035Linux: 61. docker - 원격 제어를 위한 TCP 바인딩 추가
13385정성태6/27/20234235Linux: 60. Linux - 외부에서의 접속을 허용하기 위한 TCP 포트 여는 방법
13384정성태6/26/20233951.NET Framework: 2131. C# - Source Generator로 해결하는 enum 박싱 문제파일 다운로드1
13383정성태6/26/20233704개발 환경 구성: 683. GPU 런타임을 사용하는 Colab 노트북 설정
13382정성태6/25/20233742.NET Framework: 2130. C# - Win32 API를 이용한 윈도우 계정 정보 (예: 마지막 로그온 시간)파일 다운로드1
13381정성태6/25/20234145오류 유형: 869. Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
13380정성태6/24/20233555스크립트: 52. 파이썬 3.x에서의 동적 함수 추가
13379정성태6/23/20233594스크립트: 51. 파이썬 2.x에서의 동적 함수 추가
13378정성태6/22/20233466오류 유형: 868. docker - build 시 "CANCELED ..." 뜨는 문제
13377정성태6/22/20237352오류 유형: 867. 파이썬 mysqlclient 2.2.x 설치 시 "Specify MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS env vars manually" 오류
13376정성태6/21/20233743.NET Framework: 2129. C# - Polly를 이용한 클라이언트 측의 요청 재시도파일 다운로드1
13375정성태6/20/20233409스크립트: 50. Transformers (신경망 언어모델 라이브러리) 강좌 - 2장 코드 실행 결과
13374정성태6/20/20233538오류 유형: 866. 파이썬 - <class 'AttributeError'> module 'flask.json' has no attribute 'JSONEncoder'
13373정성태6/19/20234824오류 유형: 865. 파이썬 - pymssql 설치 관련 오류 정리
13372정성태6/15/20233361개발 환경 구성: 682. SQL Server TLS 통신을 위해 사용되는 키 길이 확인 방법
1  2  3  4  5  6  7  8  9  [10]  11  12  13  14  15  ...