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

CLR 4.0 환경에서 DLL 모듈의 로드 주소(Base address) 알아내는 방법

(예전에도 잠깐 언급했지만) CLR 4부터, 그러니까 .NET Framework 4부터 DLL 로딩을 Win32 API의 LoadLibrary를 이용하지 않도록 바뀌었기 때문에 네이티브 모듈 열거에서는 .NET 모듈이 누락되는 문제가 있습니다.

가령 다음의 코드를,

// 이 코드를 실행하는 콘솔 프로그램은 ClassLibrary1.dll을 참조해 사용하고 있음

foreach (ProcessModule item in Process.GetCurrentProcess().Modules)
{
    Console.WriteLine(item.FileName + ": 0x" +  item.BaseAddress.ToString("x"));
}

.NET 2.0 ~ 3.5에서 실행하면 다음과 같이 ClassLibrary1.dll이 목록에 나오지만,

F:\ConsoleApp1\ConsoleApp1\bin\Debug\ConsoleApp1.exe: 0x630000
C:\Windows\SYSTEM32\ntdll.dll: 0x7ff97e420000
C:\Windows\SYSTEM32\MSCOREE.DLL: 0x7ff963510000
...[생략: native module]...
C:\Windows\System32\powrprof.dll: 0x7ff97a8b0000
C:\Windows\System32\profapi.dll: 0x7ff97a900000
C:\Windows\assembly\NativeImages_v2.0.50727_64\mscorlib\4e0bf2aed484475a500c73b16dd54e3a\mscorlib.ni.dll: 0x7ff95cd60000
C:\Windows\System32\ole32.dll: 0x7ff97c340000
C:\Windows\Microsoft.NET\Framework64\v2.0.50727\mscorjit.dll: 0x7ff95c5e0000
F:\ConsoleApp1\ConsoleApp1\bin\Debug\ClassLibrary1.dll: 0xfa0000
C:\Windows\assembly\NativeImages_v2.0.50727_64\System\4bf531fbd3a73bf62d20ff592522c30b\System.ni.dll: 0x7ff9310a0000
C:\Windows\System32\psapi.dll: 0x7ff97def0000

.NET 4.0 이상에서 실행하면 다음과 같이 ClassLibrary1.dll을 찾아볼 수 없습니다.

F:\ConsoleApp1\ConsoleApp1\bin\Debug\ConsoleApp1.exe: 0x1ebf8130000
C:\Windows\SYSTEM32\ntdll.dll: 0x7ff97e420000
C:\Windows\SYSTEM32\MSCOREE.DLL: 0x7ff963510000
...[생략: native module]...
C:\Windows\SYSTEM32\VERSION.dll: 0x7ff9754f0000
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll: 0x7ff9636b0000
C:\Windows\SYSTEM32\MSVCR120_CLR0400.dll: 0x7ff963410000
C:\Windows\assembly\NativeImages_v4.0.30319_64\mscorlib\9aaf4ea9ded0a99b899b7bf4c971092d\mscorlib.ni.dll: 0x7ff961df0000
C:\Windows\System32\ole32.dll: 0x7ff97c340000
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clrjit.dll: 0x7ff961cd0000
C:\Windows\assembly\NativeImages_v4.0.30319_64\System\b6563094687cd99e0db057bdda2a8b90\System.ni.dll: 0x7ff930460000
C:\Windows\System32\psapi.dll: 0x7ff97def0000

그래도 혹시 알 수 있는 방법이 있지 않을까요? 검색해 보니 다음과 같은 글이 나옵니다. ^^

Finding the correct baseaddress
; https://stackoverflow.com/questions/8263135/finding-the-correct-baseaddress

결국 managed module 목록은 AppDomain에서 구하고, 그 DLL들의 로딩 주소를 Marshal.GetHINSTANCE 메서드를 이용하면 됩니다.

foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
{
    Console.WriteLine("[" + asm.FullName + "]");
    {
        IntPtr ptr = Marshal.GetHINSTANCE(asm.ManifestModule);
        Console.WriteLine("0x" + ptr.ToInt64().ToString("x"));
    }

    Console.WriteLine();
}

다음은 이에 대한 출력 결과입니다.

[mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]
0x1ac60000

[ConsoleApp1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]
0x60000

[ClassLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]
0x690000

[System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]
0x1b190000




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

[연관 글]






[최초 등록일: ]
[최종 수정일: 10/14/2017]

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

비밀번호

댓글 작성자
 




... 31  32  33  34  35  36  37  [38]  39  40  41  42  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
12677정성태6/17/20219555VC++: 144. 역공학을 통한 lxssmanager.dll의 ILxssSession 사용법 분석파일 다운로드1
12676정성태6/16/20219623VC++: 143. ionescu007/lxss github repo에 공개된 lxssmanager.dll의 CLSID_LxssUserSession/IID_ILxssSession 사용법파일 다운로드1
12675정성태6/16/20217637Java: 20. maven package 명령어 결과물로 (war가 아닌) jar 생성 방법
12674정성태6/15/20218401VC++: 142. DEFINE_GUID 사용법
12673정성태6/15/20219578Java: 19. IntelliJ - 자바(Java)로 만드는 Web App을 Tomcat에서 실행하는 방법
12672정성태6/15/202110700오류 유형: 725. IntelliJ에서 Java webapp 실행 시 "Address localhost:1099 is already in use" 오류
12671정성태6/15/202117411오류 유형: 724. Tomcat 실행 시 Failed to initialize connector [Connector[HTTP/1.1-8080]] 오류
12670정성태6/13/20218952.NET Framework: 1071. DLL Surrogate를 이용한 Out-of-process COM 개체에서의 CoInitializeSecurity 문제파일 다운로드1
12669정성태6/11/20218917.NET Framework: 1070. 사용자 정의 GetHashCode 메서드 구현은 C# 9.0의 record 또는 리팩터링에 맡기세요.
12668정성태6/11/202110676.NET Framework: 1069. C# - DLL Surrogate를 이용한 Out-of-process COM 개체 제작파일 다운로드2
12667정성태6/10/20219282.NET Framework: 1068. COM+ 서버 응용 프로그램을 이용해 CoInitializeSecurity 제약 해결파일 다운로드1
12666정성태6/10/20217917.NET Framework: 1067. 별도 DLL에 포함된 타입을 STAThread Main 메서드에서 사용하는 경우 CoInitializeSecurity 자동 호출파일 다운로드1
12665정성태6/9/20219246.NET Framework: 1066. Wslhub.Sdk 사용으로 알아보는 CoInitializeSecurity 사용 제약파일 다운로드1
12664정성태6/9/20217539오류 유형: 723. COM+ PIA 참조 시 "This operation failed because the QueryInterface call on the COM component" 오류
12663정성태6/9/20219026.NET Framework: 1065. Windows Forms - 속성 창의 디자인 설정 지원: 문자열 목록 내에서 항목을 선택하는 TypeConverter 제작파일 다운로드1
12662정성태6/8/20218203.NET Framework: 1064. C# COM 개체를 PIA(Primary Interop Assembly)로써 "Embed Interop Types" 참조하는 방법파일 다운로드1
12661정성태6/4/202118818.NET Framework: 1063. C# - MQTT를 이용한 클라이언트/서버(Broker) 통신 예제 [4]파일 다운로드1
12660정성태6/3/20219912.NET Framework: 1062. Windows Forms - 폼 내에서 발생하는 마우스 이벤트를 자식 컨트롤 영역에 상관없이 수신하는 방법 [1]파일 다운로드1
12659정성태6/2/202111197Linux: 40. 우분투 설치 후 MBR 디스크 드라이브 여유 공간이 인식되지 않은 경우 - Logical Volume Management
12658정성태6/2/20218631Windows: 194. Microsoft Store에 있는 구글의 공식 Youtube App
12657정성태6/2/20219911Windows: 193. 윈도우 패키지 관리자 - winget 설치
12656정성태6/1/20218137.NET Framework: 1061. 서버 유형의 COM+에 적용할 수 없는 Server GC
12655정성태6/1/20217683오류 유형: 722. windbg/sos - savemodule - Fail to read memory
12654정성태5/31/20217710오류 유형: 721. Hyper-V - Saved 상태의 VM을 시작 시 오류 발생
12653정성태5/31/202110338.NET Framework: 1060. 닷넷 GC에 새롭게 구현되는 DPAD(Dynamic Promotion And Demotion for GC)
12652정성태5/31/20218465VS.NET IDE: 164. Visual Studio - Web Deploy로 Publish 시 암호창이 매번 뜨는 문제
... 31  32  33  34  35  36  37  [38]  39  40  41  42  43  44  45  ...