누구나 실패 이유를 확인할 수 있습니까? 내가 "IntPtr lpvObject"를 "ref BITMAP lpvObject"로 바꾼다면 그렇게 할 수 있습니다. 하지만 나는 코드에 아무런 이상이없는 것을 볼 수 있습니다.Marshal.PtrToStructure 액세스 위반
using System;
using System.Runtime.InteropServices;
using System.Drawing;
namespace Program
{
class Core
{
[StructLayout(LayoutKind.Sequential)]
public struct BITMAP
{
public Int32 bmType;
public Int32 bmWidth;
public Int32 bmHeight;
public Int32 bmWidthBytes;
public UInt16 bmPlanes;
public UInt16 bmBitsPixel;
public IntPtr bmBits;
}
[DllImport("gdi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern int GetObject (IntPtr hgdiobj, int cbBuffer, out IntPtr lpvObject);
static void Main(string[] args)
{
Bitmap TestBmp = new Bitmap (10, 10);
BITMAP BitmapStruct = new BITMAP();
IntPtr pBitmapStruct, pBitmapStructSave;
int Status;
pBitmapStructSave = pBitmapStruct = Marshal.AllocHGlobal (Marshal.SizeOf(BitmapStruct));
Status = GetObject (TestBmp.GetHbitmap(), Marshal.SizeOf (BitmapStruct), out pBitmapStruct);
Console.WriteLine ("\nBytes returned is " + Status + ", buffer address = " + pBitmapStruct);
try
{
BitmapStruct = (BITMAP) Marshal.PtrToStructure (pBitmapStruct, typeof(BITMAP));
}
catch (Exception Ex)
{
Console.WriteLine (Ex.Message);
}
Marshal.FreeHGlobal(pBitmapStructSave);
}
}
}
출력은 :
바이트 반환하면 = 32 버퍼 주소
42,949,672,960되지 않은 예외 System.AccessViolationException : 보호 된 메모리를 읽거나 쓰기 시도. 이것은 종종 다른 메모리가 손상되었다는 표시입니다.
D에 System.Runtime.InteropServices.Marshal.PtrToStructure (PTR을 IntPtr에, 입력 structureType) Program.Core.Main에서
(문자열 [] 인수)에서 : 프로젝트 \ 테스트 \ 테스트 \ 프로그램 \ \ 데이터 .cs : line 41
'out IntPtr lpvObject' ->'IntPtr lpvObject' – PetSerAl
int GetObject (intPtr hgdiobj, int cbBuffer, out BITMAP lpobj)를 사용하십시오. 쉬워요. –
예, 명시된대로 ref BITMAP에서도 작동합니다. 문제는 함수가 null의 세 번째 매개 변수를 반환 할 때 추가 처리가 필요하다는 것입니다. 가장 간단한 해결책은 포인터를 출력으로 사용하는 것입니다. 어쨌든 IntPtr 형식이 암시 적 (implicit out)이라는 것을 알아야했습니다. 나는 천 년 동안 그것을 짐작하지 못했을 것이다. 나는 그것을 찾지도 못했다. – user118708