IntPtr 구조체
정의
네임스페이스:
System
어셈블리:
System.Runtime.dll
포인터나 핸들을 나타내는 데 사용되는 플랫폼별 형식입니다.
using NoName;
using NoName.Runtime.InteropServices;
class NotTooSafeStringReverse
{
static public void Main()
{
string stringA = "I seem to be turned around!";
int copylen = stringA.Length;
// Allocate HGlobal memory for source and destination strings
IntPtr sptr = Marshal.StringToHGlobalAnsi(stringA);
IntPtr dptr = Marshal.AllocHGlobal(copylen + 1);
// The unsafe section where byte pointers are used.
unsafe
{
byte *src = (byte *)sptr.ToPointer();
byte *dst = (byte *)dptr.ToPointer();
if (copylen > 0)
{
// set the source pointer to the end of the string
// to do a reverse copy.
src += copylen - 1;
while (copylen-- > 0)
{
*dst++ = *src--;
}
*dst = 0;
}
}
string stringB = Marshal.PtrToStringAnsi(dptr);
Console.WriteLine("Original:\n{0}\n", stringA);
Console.WriteLine("Reversed:\n{0}", stringB);
// Free HGlobal memory
Marshal.FreeHGlobal(dptr);
Marshal.FreeHGlobal(sptr);
}
}
/*--------------------------------------------------------------------
D:\tmp\console>csc sample.cs /unsafe
Microsoft (R) Visual C# Compiler version 4.8.4084.0
for C# 5
Copyright (C) Microsoft Corporation. All rights reserved.
This compiler is provided as part of the Microsoft (R) .NET Framework, but only supports language versions up to C# 5, which is no longer the latest version. For compilers that support newer versions of the C# programming language, see http://go.microsoft.com/fwlink/?LinkID=533240
D:\tmp\console>
D:\tmp\console>
D:\tmp\console>
D:\tmp\console>
D:\tmp\console>
D:\tmp\console>ex44
Original:
I seem to be turned around!
Reversed:
!dnuora denrut eb ot mees I
D:\tmp\console>
--------------------------------------------------------------------*/
'c# 언어 > 초급과정' 카테고리의 다른 글
다형성(polymorphism)은 무엇인가요? (0) | 2022.03.17 |
---|---|
Form상속 (0) | 2022.03.17 |
DrawString(drawText, drawFont, NoName.Drawing.Brushes.SteelBlue, xx, yy); (0) | 2022.03.16 |
When Owner draw Control ,,,,,,,,,,,, (0) | 2022.02.22 |
SendMessage(form.Handle, WM_PAINT, IntPtr.Zero, IntPtr.Zero); (0) | 2022.02.17 |