728x90
반응형
728x170
■ Marshal 클래스의 WriteByte 정적 메소드를 사용해 비관리 메모리에 바이트를 쓰는 방법을 보여준다.
▶ 예제 코드 (C#)
using System.Runtime.InteropServices;
using System.Text;
byte[] sourceByteArray = Encoding.UTF8.GetBytes("안녕하세요");
IntPtr targetHandle = Marshal.AllocHGlobal(sourceByteArray.Length);
for(int i = 0; i < sourceByteArray.Length; i++)
{
Marshal.WriteByte(targetHandle, i, sourceByteArray[i]);
}
string target = Marshal.PtrToStringUTF8(targetHandle);
Console.WriteLine(target);
Marshal.FreeHGlobal(targetHandle);
728x90
반응형
그리드형(광고전용)
'C# > Common' 카테고리의 다른 글
[C#/COMMON/.NET6] 라이센스 키 사용하기 (0) | 2022.10.19 |
---|---|
[C#/COMMON/.NET6] String 클래스 : Substring 메소드를 사용해 문자열을 특정 크기로 나누기 (기능 개선) (0) | 2022.10.19 |
[C#/COMMON/.NET6] String 클래스 : Substring 메소드를 사용해 문자열을 특정 크기로 나누기 (0) | 2022.10.19 |
[C#/COMMON/.NET6] Enumerable 클래스 : Range 정적 메소드/Select 확장 메소드를 사용해 문자열을 특정 크기로 나누기 (0) | 2022.10.19 |
[C#/COMMON/.NET6] 규칙 엔진 만들기 (0) | 2022.10.19 |
[C#/COMMON/.NET6] GCHandle 클래스 사용하기 (0) | 2022.10.19 |
[C#/COMMON/.NET6] GCHandle 클래스 : AddrOfPinnedObject 메소드를 사용해 고정된 객체 데이터의 핸들 구하기 (0) | 2022.10.19 |
[C#/COMMON/.NET6] GCHandle 클래스 : Alloc 정적 메소드를 사용해 객체의 가비지 수집을 방지하는 핸들 구하기 (0) | 2022.10.19 |
[C#/COMMON/.NET6] Marshal 클래스 : PtrToStringUTF8 정적 메소드를 사용해 비관리 메모리에서 UTF8 문자열 구하기 (0) | 2022.10.19 |
[C#/COMMON/.NET6] Marshal 클래스 : Copy 정적 메소드를 사용해 바이트 배열을 비관리 메모리에 복사하기 (0) | 2022.10.19 |
댓글을 달아 주세요