Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
(연관된 글이 4개 있습니다.)
(시리즈 글이 5개 있습니다.)
.NET Framework: 1066. Wslhub.Sdk 사용으로 알아보는 CoInitializeSecurity 사용 제약
; https://www.sysnet.pe.kr/2/0/12665

.NET Framework: 1067. 별도 DLL에 포함된 타입을 STAThread Main 메서드에서 사용하는 경우 CoInitializeSecurity 자동 호출
; https://www.sysnet.pe.kr/2/0/12666

.NET Framework: 1068. COM+ 서버 응용 프로그램을 이용해 CoInitializeSecurity 제약 해결
; https://www.sysnet.pe.kr/2/0/12667

.NET Framework: 1071. DLL Surrogate를 이용한 Out-of-process COM 개체에서의 CoInitializeSecurity 문제
; https://www.sysnet.pe.kr/2/0/12670

COM 개체 관련: 23. CoInitializeSecurity의 전역 설정을 재정의하는 CoSetProxyBlanket 함수 사용법
; https://www.sysnet.pe.kr/2/0/12679




별도 DLL에 포함된 타입을 STAThread Main 메서드에서 사용하는 경우 CoInitializeSecurity 자동 호출

재미있는 현상이군요. ^^; 지난 글의 마지막에,

Wslhub.Sdk 사용으로 알아보는 CoInitializeSecurity 사용 제약
; https://www.sysnet.pe.kr/2/0/12665

다음의 코드를 .NET Framework 환경에서 수행하면 결괏값이 RPC_E_TOO_LATE가 나온다고 했는데요,

using System;
using WslSdk.Interop;

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        var result = NativeMethods.CoInitializeSecurity(
            IntPtr.Zero,
            (-1),
            IntPtr.Zero,
            IntPtr.Zero,
            NativeMethods.RpcAuthnLevel.None,
            NativeMethods.RpcImpLevel.Impersonate,
            IntPtr.Zero,
            NativeMethods.EoAuthnCap.StaticCloaking,
            IntPtr.Zero);

        Console.WriteLine(result); // 출력 결과: 0
    }
}

그런데, 실제로 저렇게 테스트해 보면 0이 나옵니다. 단지 영향이 없을 것 같은 코드를 저기에 싣지 않았던 것뿐인데요, 원래는 아래와 같았습니다.

using System;
using WslSdk.Interop;

// Install-Package Wslhub.Sdk
class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        var result = NativeMethods.CoInitializeSecurity(
            IntPtr.Zero,
            (-1),
            IntPtr.Zero,
            IntPtr.Zero,
            NativeMethods.RpcAuthnLevel.None,
            NativeMethods.RpcImpLevel.Impersonate,
            IntPtr.Zero,
            NativeMethods.EoAuthnCap.StaticCloaking,
            IntPtr.Zero);

        Console.WriteLine(result); // 출력 결과: -2147417831

        Wsl.GetDistroListFromRegistry();
    }
}

그러니까, 저렇게 WslSdk의 메서드 호출만 추가한 것으로 CoInitializeSecurity가 실패하는 것입니다. 오~~~ 신기하지 않나요? ^^




위의 현상을 간단하게 재현하는 것이 가능합니다. 우선, .NET Framework 콘솔 프로젝트를 생성하고 NativeMethods.CoInitializeSecurity를 호출하는 상태로 만들어 둡니다. 그다음 아래의 소스 코드만 가진 .NET Framework 라이브러리를,

public class Class1
{
    public static void Test() { }
}

콘솔 프로젝트에서 참조 추가해 Test 메서드를 호출하는 코드를 넣어 주면 상황을 재현할 수 있습니다.

using System;

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        var result = NativeMethods.CoInitializeSecurity(
            IntPtr.Zero, (-1), IntPtr.Zero, IntPtr.Zero, NativeMethods.RpcAuthnLevel.None,
            NativeMethods.RpcImpLevel.Impersonate, IntPtr.Zero, NativeMethods.EoAuthnCap.StaticCloaking, IntPtr.Zero);

        Console.WriteLine(result); // 출력 결과: -2147417831

        Class1.Test();
    }
}

만약 저 코드를 JIT 컴파일러가 알지 못하도록 분리시켜 놓으면,

using System;

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        var result = NativeMethods.CoInitializeSecurity(
            IntPtr.Zero, (-1), IntPtr.Zero, IntPtr.Zero, NativeMethods.RpcAuthnLevel.None,
            NativeMethods.RpcImpLevel.Impersonate, IntPtr.Zero, NativeMethods.EoAuthnCap.StaticCloaking, IntPtr.Zero);

        Console.WriteLine(result); // 출력 결과: 0
            
        CallMethod();
    }

    static void CallMethod()
    {
        Class1.Test();
    }
}

이번에는 다시 0이 반환됩니다. 도대체 ^^; 무슨 일이 있는 걸까요?

어쨌든 현상만을 정리해 보면, ^^ JIT 컴파일러는 외부 DLL이 사용되었다는 것만으로 STAThread Main 시작 전에 CoInitializeSecurity를 호출하고 있는 것입니다. 참고로, 해당 현상은 .NET Core 프로젝트에서는 발생하지 않습니다.

(첨부 파일은 이 글의 예제 코드를 포함합니다.)




C# - CoCreateInstance 관련 Inteop 오류 정리
; https://www.sysnet.pe.kr/2/0/12678

Wslhub.Sdk 사용으로 알아보는 CoInitializeSecurity 사용 제약
; https://www.sysnet.pe.kr/2/0/12665

별도 DLL에 포함된 타입을 STAThread Main 메서드에서 사용하는 경우 CoInitializeSecurity 자동 호출
; https://www.sysnet.pe.kr/2/0/12666

COM+ 서버 응용 프로그램을 이용해 CoInitializeSecurity 제약 해결
; https://www.sysnet.pe.kr/2/0/12667

ionescu007/lxss github repo에 공개된 lxssmanager.dll의 CLSID_LxssUserSession/IID_ILxssSession 사용법
; https://www.sysnet.pe.kr/2/0/12676

역공학을 통한 lxssmanager.dll의 ILxssSession 사용법 분석
; https://www.sysnet.pe.kr/2/0/12677

C# - DLL Surrogate를 이용한 Out-of-process COM 개체 제작
; https://www.sysnet.pe.kr/2/0/12668

DLL Surrogate를 이용한 Out-of-process COM 개체에서의 CoInitializeSecurity 문제
; https://www.sysnet.pe.kr/2/0/12670

CoInitializeSecurity의 전역 설정을 재정의하는 CoSetProxyBlanket 함수 사용법
; https://www.sysnet.pe.kr/2/0/12679




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

[연관 글]






[최초 등록일: ]
[최종 수정일: 2/4/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)
13864정성태1/15/20257958Linux: 114. eBPF를 위해 필요한 SELinux 보안 정책
13863정성태1/14/20255914Linux: 113. Linux - 프로세스를 위한 전용 SELinux 보안 문맥 지정
13862정성태1/13/20256931Linux: 112. Linux - 데몬을 위한 SELinux 보안 정책 설정
13861정성태1/11/20257335Windows: 276. 명령행에서 원격 서비스를 동기/비동기로 시작/중지
13860정성태1/10/20256480디버깅 기술: 216. WinDbg - 2가지 유형의 식 평가 방법(MASM, C++)
13859정성태1/9/20258007디버깅 기술: 215. Windbg - syscall 이후 실행되는 KiSystemCall64 함수 및 SSDT 디버깅
13858정성태1/8/20258347개발 환경 구성: 738. PowerShell - 원격 호출 시 "powershell.exe"가 아닌 "pwsh.exe" 환경으로 명령어를 실행하는 방법
13857정성태1/7/20258391C/C++: 187. Golang - 콘솔 응용 프로그램을 Linux 데몬 서비스를 지원하도록 변경파일 다운로드1
13856정성태1/6/20256302디버깅 기술: 214. Windbg - syscall 단계까지의 Win32 API 호출 (예: Sleep)
13855정성태12/28/20248849오류 유형: 941. Golang - os.StartProcess() 사용 시 오류 정리
13854정성태12/27/20248657C/C++: 186. Golang - 콘솔 응용 프로그램을 NT 서비스를 지원하도록 변경파일 다운로드1
13853정성태12/26/20246867디버깅 기술: 213. Windbg - swapgs 명령어와 (Ring 0 커널 모드의) FS, GS Segment 레지스터
13852정성태12/25/20249347디버깅 기술: 212. Windbg - (Ring 3 사용자 모드의) FS, GS Segment 레지스터파일 다운로드1
13851정성태12/23/20247206디버깅 기술: 211. Windbg - 커널 모드 디버깅 상태에서 사용자 프로그램을 디버깅하는 방법
13850정성태12/23/20249272오류 유형: 940. "Application Information" 서비스를 중지한 경우, "This file does not have an app associated with it for performing this action."
13849정성태12/20/20249200디버깅 기술: 210. Windbg - 논리(가상) 주소를 Segmentation을 거쳐 선형 주소로 변경
13848정성태12/18/20248261디버깅 기술: 209. Windbg로 알아보는 Prototype PTE파일 다운로드2
13847정성태12/18/20247855오류 유형: 939. golang - 빌드 시 "unknown directive: toolchain" 오류 빌드 시 이런 오류가 발생한다면?
13846정성태12/17/20249602디버깅 기술: 208. Windbg로 알아보는 Trans/Soft PTE와 2가지 Page Fault 유형파일 다운로드1
13845정성태12/16/20246830디버깅 기술: 207. Windbg로 알아보는 PTE (_MMPTE)
13844정성태12/14/202410123디버깅 기술: 206. Windbg로 알아보는 PFN (_MMPFN)파일 다운로드1
13843정성태12/13/20247402오류 유형: 938. Docker container 내에서 빌드 시 error MSB3021: Unable to copy file "..." to "...". Access to the path '...' is denied.
13842정성태12/12/20247770디버깅 기술: 205. Windbg - KPCR, KPRCB
13841정성태12/11/20248477오류 유형: 937. error MSB4044: The "ValidateValidArchitecture" task was not given a value for the required parameter "RemoteTarget"
13840정성태12/11/20247559오류 유형: 936. msbuild - Your project file doesn't list 'win' as a "RuntimeIdentifier"
13839정성태12/11/20249150오류 유형: 936. msbuild - error CS1617: Invalid option '12.0' for /langversion. Use '/langversion:?' to list supported values.
1  2  3  4  5  6  [7]  8  9  10  11  12  13  14  15  ...