Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 1개 있습니다.)
(시리즈 글이 6개 있습니다.)
개발 환경 구성: 386. .NET Framework Native compiler 프리뷰 버전 사용법
; https://www.sysnet.pe.kr/2/0/11563

.NET Framework: 2069. .NET 7 - AOT(ahead-of-time) 컴파일
; https://www.sysnet.pe.kr/2/0/13162

닷넷: 2175. C# - DllImport 메서드의 AOT 지원을 위한 LibraryImport 옵션
; https://www.sysnet.pe.kr/2/0/13466

닷넷: 2184. C# - 하나의 resource 파일을 여러 프로그램에서 (AOT 시에도) 사용하는 방법
; https://www.sysnet.pe.kr/2/0/13483

개발 환경 구성: 696. C# - 리눅스용 AOT 빌드를 docker에서 수행
; https://www.sysnet.pe.kr/2/0/13487

닷넷: 2202. C# - PublishAot의 glibc에 대한 정적 링킹하는 방법
; https://www.sysnet.pe.kr/2/0/13529




C# - DllImport 메서드의 AOT 지원을 위한 LibraryImport 옵션

P/Invoke는 DllImport 호출 시 .NET 런타임 측에서 동적으로 코드를 생성해 연결하는 식으로 동작합니다. 이에 대해서는 전에 한번 다룬 적이 있습니다.

windbg - C# PInvoke 호출 시 마샬링을 담당하는 함수 분석
; https://www.sysnet.pe.kr/2/0/12065

이러한 동적 코드 생성이 AOT 시에는 문제가 된다고 하는데요,

Source generation for platform invokes
; https://learn.microsoft.com/en-us/dotnet/standard/native-interop/pinvoke-source-generation

The IL stub handles marshalling of parameters and return values and calling the unmanaged code while respecting settings on DllImportAttribute that affect how the unmanaged code should be invoked (for example, SetLastError). Since this IL stub is generated at run time, it isn't available for ahead-of-time (AOT) compiler or IL trimming scenarios


그래서 그 해법으로 .NET 7부터 LibraryImport를 이용한 방법을 소개하고 있습니다. 간단하게, LibraryImport 속성이 어떻게 동작하는지 한번 파헤쳐 볼까요? ^^

LibraryImportAttribute
; https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.libraryimportattribute

예를 들어, 기존에 다음과 같이 DllImport를 사용하던 경우라면,

public class Program
{
    [DllImport(@"ClassLibrary2.dll")]
    public static extern void mymethod([MarshalAs(UnmanagedType.LPWStr)] string text);
}

이것을 LibraryImport로 바꾸기 위해서는 class를 partial로 만들고,

public partial class Program
{
    // ...[생략]...
}

DllImport를 적용했던 메서드를 partial과 함께 LibraryImport 특성으로 대체한 다음, 마샬링이 필요한 참조 형식의 인자에 대해서는 어떻게 처리해야 하는지를 알려주는 옵션을 적용하면 됩니다. (string 형식에 대해서만 지원합니다.)

// "string text" 인자가 참조형이므로 StringMarshalling 옵션을 이용해 Utf16으로 하도록 명시

[LibraryImport(@"ClassLibrary2.dll", StringMarshalling = StringMarshalling.Utf16)]
public static partial void mymethod(string text);

이렇게 바꾸면 C# 컴파일러는 LibraryImport 특성이 있는 partial 메서드에 대해 다음과 같은 식으로 partial class + partial method를 적용해 소스 코드를 생성해 줍니다.

// unsafe가 들어가기 때문에, LibraryImport를 적용한 경우 프로젝트 옵션에 AllowUnsafeBlocks 값을 true로 설정해야 함

// <auto-generated/>
namespace ConsoleApp1
{
    internal unsafe partial class Program
    {
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Interop.LibraryImportGenerator", "8.0.9.3103")]
        [global::System.Runtime.CompilerServices.SkipLocalsInitAttribute]
        private static partial void mymethod(string text)
        {
            // Pin - Pin data in preparation for calling the P/Invoke.
            fixed (void* __text_native = &global::System.Runtime.InteropServices.Marshalling.Utf16StringMarshaller.GetPinnableReference(text))
            {
                __PInvoke((ushort*)__text_native);
            }

            // Local P/Invoke
            [global::System.Runtime.InteropServices.DllImportAttribute("ClassLibrary2.dll", EntryPoint = "mymethod", ExactSpelling = true)]
            static extern unsafe void __PInvoke(ushort* __text_native);
        }
    }
}

그러니까, 참조형 타입에 대해 미리 pinning 등의 작업을 한 후, 원래의 DllImport를 감싼 __PInvoke를 호출하는 식으로 바뀝니다. 결국, DllImport를 다음과 같이 사용하던 것과 별반 다를 게 없습니다.

[DllImport(@"ClassLibrary2.dll")]
public static extern void mymethod(char* text);

fixed (char* ptr = text)
{
    mymethod(ptr);
}




보는 바와 같이 LibraryImport는 간단한 래퍼에 불과한 것으로, DllImport를 사용하는 유형이긴 한데, 전달되는 인자에 대해 부가적인 마샬링 코드를 런타임 측에서 동적으로 생성할 필요가 없도록 강제하는 효과를 갖는 것입니다.

일례로, (마샬링이 필요한) 참조 형식을 전달하려 하면,

[StructLayout(LayoutKind.Sequential)]
public class MyType
{
    public int Age { get; set; }
}

[LibraryImport(@"ClassLibrary2.dll")]
public static partial void InitMyType(MyType instance);

SYSLIB1051 컴파일 오류를 발생시킵니다.

SYSLIB1051: The type 'ConsoleApp1.MyType' is not supported by source-generated P/Invokes. The generated source will not handle marshalling of parameter 'instance'. (https://learn.microsoft.com/dotnet/fundamentals/syslib-diagnostics/syslib1051)


반면 마샬링이 필요 없도록 struct로 바꾸면,

public struct MyType
{
    public int Age { get; set; }
}

/*
// 참고로, 이때 자동 생성하는 partial 메서드는, DllImport로 유형만 바뀐 채로 처리됩니다.
public unsafe partial class Program
{
    [global::System.Runtime.InteropServices.DllImportAttribute("ClassLibrary2.dll", EntryPoint = "InitMyType", ExactSpelling = true)]
    public static extern partial void InitMyType(global::ConsoleApp1.MyType instance);
}
*/

다시 AOT 빌드까지 잘 되지만, 만약 저 struct의 필드에 참조 형식을 포함하게 되면,

public struct MyType
{
    public int Age { get; set; }
    public string Text { get; set; } // 참조 형식
}

재차 SYSLIB1051 오류가 발생합니다. 메서드의 인자에 string이 있는 경우에는 "StringMarshalling = StringMarshalling.Utf16" 옵션을 적용해 LibraryImportGenerator 소스 코드 생성기로 하여금 마샬링 코드를 컴파일 시점에 생성하도록 만들 수 있지만, 저렇게 사용자 타입에 들어가는 유형은 (아직인지는 모르겠지만) LibraryImportGenerator에서 지원하지 않습니다.

검색해 보면, MarshalUsing을 이용해 Custom marshaler까지 제공하면 된다고 하는데요,

Marshalling Function Pointers with .NET 7 LibraryImport
; https://stackoverflow.com/questions/75304403/marshalling-function-pointers-with-net-7-libraryimport

정리하자면, LibraryImport를 사용함으로써 인자의 마샬링 작업만큼은 PInvoke 호출 전에, 즉 컴파일 시점에 끝내겠다는 ^^ 확고한 의지를 보여주는 것과 같다고 하겠습니다. (덤으로 C# 컴파일러로 하여금 유효성 체크를 받을 수도 있고!)

결국 마샬링이 필요 없는 경우라면 DllImport를 기존과 다름없이 사용하셔도 됩니다. 심지어, 문서에서 예를 든 ToLower 메서드도,

[DllImport(
    "nativelib",
    EntryPoint = "to_lower",
    CharSet = CharSet.Unicode)]
internal static extern string ToLower(string str);

// string lower = ToLower("StringToConvert");

PublishAot로 빌드할 때 아무런 문제 없이 잘 실행이 됩니다. 사실, 저 경우는 string에 대해 CharSet 및 pinning 처리를 수반하는 코드가 생성될 텐데도, ... string 정도는 감안하고 특별히 AOT 컴파일러 측에서 처리를 해주는 것인지는 알 수 없지만, 어쨌든 빌드 및 실행까지 됩니다.




돌이켜 보면, P/Inovke 관련해서는 정말 많은 변화가 있었군요. ^^

가령, Microsoft.Windows.CsWin32 패키지를 통한 DllImport를 자동 생성해 주는 것도 있었고,

C# - Win32 API에 대한 P/Invoke를 대신하는 Microsoft.Windows.CsWin32 패키지
; https://www.sysnet.pe.kr/2/0/12540

SuppressGCTransition을 이용한 속도 향상을 꾀하기도 했습니다.

.NET 5+ 환경에서 P/Invoke의 성능을 높이기 위한 SuppressGCTransition 특성
; https://www.sysnet.pe.kr/2/0/13401




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

[연관 글]






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

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)
12855정성태11/13/20216239개발 환경 구성: 605. Azure App Service - Kudu SSH 환경에서 FTP를 이용한 파일 전송
12854정성태11/13/20217796개발 환경 구성: 604. Azure - 윈도우 VM에서 FTP 여는 방법
12853정성태11/10/20216149오류 유형: 766. Azure App Service - JBoss 호스팅 생성 시 "This region has quota of 0 PremiumV3 instances for your subscription. Try selecting different region or SKU."
12851정성태11/1/20217548스크립트: 34. 파이썬 - MySQLdb 기본 예제 코드
12850정성태10/27/20218691오류 유형: 765. 우분투에서 pip install mysqlclient 실행 시 "OSError: mysql_config not found" 오류
12849정성태10/17/20217822스크립트: 33. JavaScript와 C#의 시간 변환 [1]
12848정성태10/17/20218802스크립트: 32. 파이썬 - sqlite3 기본 예제 코드 [1]
12847정성태10/14/20218652스크립트: 31. 파이썬 gunicorn - WORKER TIMEOUT 오류 발생
12846정성태10/7/20218405스크립트: 30. 파이썬 __debug__ 플래그 변수에 따른 코드 실행 제어
12845정성태10/6/20218233.NET Framework: 1120. C# - BufferBlock<T> 사용 예제 [5]파일 다운로드1
12844정성태10/3/20216240오류 유형: 764. MSI 설치 시 "... is accessible and not read-only." 오류 메시지
12843정성태10/3/20216687스크립트: 29. 파이썬 - fork 시 기존 클라이언트 소켓 및 스레드의 동작파일 다운로드1
12842정성태10/1/202125040오류 유형: 763. 파이썬 오류 - AttributeError: type object '...' has no attribute '...'
12841정성태10/1/20218512스크립트: 28. 모든 파이썬 프로세스에 올라오는 특별한 파일 - sitecustomize.py
12840정성태9/30/20218615.NET Framework: 1119. Entity Framework의 Join 사용 시 다중 칼럼에 대한 OR 조건 쿼리파일 다운로드1
12839정성태9/15/20219666.NET Framework: 1118. C# 11 - 제네릭 타입의 특성 적용파일 다운로드1
12838정성태9/13/20219309.NET Framework: 1117. C# - Task에 전달한 Action, Func 유형에 따라 달라지는 async/await 비동기 처리 [2]파일 다운로드1
12837정성태9/11/20218219VC++: 151. Golang - fmt.Errorf, errors.Is, errors.As 설명
12836정성태9/10/20217827Linux: 45. 리눅스 - 실행 중인 다른 프로그램의 출력을 확인하는 방법
12835정성태9/7/20219070.NET Framework: 1116. C# 10 - (15) CallerArgumentExpression 특성 추가 [2]파일 다운로드1
12834정성태9/7/20217458오류 유형: 762. Visual Studio 2019 Build Tools - 'C:\Program' is not recognized as an internal or external command, operable program or batch file.
12833정성태9/6/20216908VC++: 150. Golang - TCP client/server echo 예제 코드파일 다운로드1
12832정성태9/6/20217770VC++: 149. Golang - 인터페이스 포인터가 의미 있을까요?
12831정성태9/6/20216298VC++: 148. Golang - 채널에 따른 다중 작업 처리파일 다운로드1
12830정성태9/6/20218564오류 유형: 761. Internet Explorer에서 파일 다운로드 시 "Your current security settings do not allow this file to be downloaded." 오류
12829정성태9/5/202110199.NET Framework: 1115. C# 10 - (14) 구조체 타입에 기본 생성자 정의 가능파일 다운로드1
... [31]  32  33  34  35  36  37  38  39  40  41  42  43  44  45  ...