Saya mencoba mengubah resolusi dalam proyek inti.
var devmode = default(Devmode);
devmode.DmDeviceName = new string(new char[32]);
devmode.DmFormName = new string(new char[32]);
devmode.Dmsize = (short)Marshal.SizeOf(devmode);
Pada baris ke-3 dari kode di atas, kode tersebut gagal dan menghasilkan kesalahan "tidak dapat disusun sebagai struktur yang tidak dikelola; tidak ada ukuran atau offset yang berarti yang dapat dihitung".
Adakah yang bisa memberikan wawasan tentang bagaimana saya harus memperbaikinya? Yang aneh adalah kode ini berfungsi dengan baik sebelum saya menarik beberapa kode lain yang seharusnya tidak mempengaruhi kode ini.
Struktur:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct Devmode
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string DmFormName;
public string DmDeviceName;
public short Dmsize;
public int DmPelsWidth;
public int DmPelsHeight;
private readonly short dmlogPixels;
private readonly short dmbitsPerPel;
private readonly int dmdisplayFlags;
private readonly int dmdisplayFrequency;
private readonly int dmiCMMethod;
private readonly int dmiCMIntent;
private readonly int dmmediaType;
private readonly int dmditherType;
private readonly int dmreserved1;
private readonly int dmreserved2;
private readonly int dmpanningWidth;
private readonly int dmpanningHeight;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
private readonly short dmspecVersion;
private readonly short dmdriverVersion;
private readonly short dmdriverExtra;
private readonly int dmfields;
private readonly int dmpositionX;
private readonly int dmpositionY;
private readonly int dmdisplayOrientation;
private readonly int dmdisplayFixedOutput;
private readonly short dmcolor;
private readonly short dmduplex;
private readonly short dmyResolution;
private readonly short dmtTOption;
private readonly short dmcollate;
}
EDIT: Sekarang saya telah mengubah kode menjadi ini:
[StructLayout(LayoutKind.Sequential)]
public struct Devmode
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
private readonly string dmdeviceName;
private readonly short dmspecVersion;
private readonly short dmdriverVersion;
public short Dmsize;
private readonly short dmdriverExtra;
private readonly int dmfields;
private readonly int dmpositionX;
private readonly int dmpositionY;
private readonly int dmdisplayOrientation;
private readonly int dmdisplayFixedOutput;
private readonly short dmcolor;
private readonly short dmduplex;
private readonly short dmyResolution;
private readonly short dmtTOption;
private readonly short dmcollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
private readonly string dmformName;
private readonly short dmlogPixels;
private readonly short dmbitsPerPel;
public int dmpelsWidth;
public int dmpelsHeight;
private readonly int dmdisplayFlags;
private readonly int dmdisplayFrequency;
private readonly int dmiCMMethod;
private readonly int dmiCMIntent;
private readonly int dmmediaType;
private readonly int dmditherType;
private readonly int dmreserved1;
private readonly int dmreserved2;
private readonly int dmpanningWidth;
private readonly int dmpanningHeight;
}
Namun saya mendapatkan kesalahan untuk dmpelsWidth, dmpelsHeight dan Dmsize. Anggota 'publik' harus didahulukan sebelum anggota 'pribadi'...
Ada ide?