CS0308 Marshal.SizeOf<> error, how do I fix that? - c#

I have error in this code:
intPtr = Marshal.AllocHGlobal(Marshal.SizeOf<Win32API.OBJECT_BASIC_INFORMATION>(object_BASIC_INFORMATION));
Win32API.NtQueryObject(zero, 0, intPtr, Marshal.SizeOf<Win32API.OBJECT_BASIC_INFORMATION>(object_BASIC_INFORMATION), ref num);
object_BASIC_INFORMATION = (Win32API.OBJECT_BASIC_INFORMATION)Marshal.PtrToStructure(intPtr, object_BASIC_INFORMATION.GetType());
Marshal.FreeHGlobal(intPtr);
intPtr2 = Marshal.AllocHGlobal(object_BASIC_INFORMATION.TypeInformationLength);
num = object_BASIC_INFORMATION.TypeInformationLength;
This line:
intPtr = Marshal.AllocHGlobal(Marshal.SizeOf<Win32API.OBJECT_BASIC_INFORMATION>(object_BASIC_INFORMATION));
What am I doing wrong?
OBJECT_BASIC_INFORMATION:
public struct OBJECT_BASIC_INFORMATION
{
public int Attributes;
public int GrantedAccess;
public int HandleCount;
public int PointerCount;
public int PagedPoolUsage;
public int NonPagedPoolUsage;
public int Reserved1;
public int Reserved2;
public int Reserved3;
public int NameInformationLength;
public int TypeInformationLength;
public int SecurityDescriptorLength;
public System.Runtime.InteropServices.ComTypes.FILETIME CreateTime;
}
Error:
CS0308 C# The non-generic method cannot be used with type arguments

Related

dmDisplayFrequency showing 0. Any alternative?

I'm trying to use win32 EnumDisplaySettings to get all available resolutions and refresh rates. But when printing dmDisplayFrequency I always get 0 which means that it cannot get it. Is there any alternatives to enumDisplaySettings ? If windows is able to provide a list in display setting window then there must be another way ?
using System;
using System.Linq;
using System.Runtime.InteropServices;
namespace ListResolutions
{
class Program
{
[DllImport("user32.dll")]
public static extern bool EnumDisplaySettings(
string deviceName, int modeNum, ref DEVMODE devMode);
const int ENUM_CURRENT_SETTINGS = -1;
const int ENUM_REGISTRY_SETTINGS = -2;
[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE
{
private const int CCHDEVICENAME = 0x20;
private const int CCHFORMNAME = 0x20;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public int dmPositionX;
public int dmPositionY;
public int dmDisplayFixedOutput;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
public string dmFormName;
public short dmLogPixels;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
public int dmICMMethod;
public int dmICMIntent;
public int dmMediaType;
public int dmDitherType;
public int dmReserved1;
public int dmReserved2;
public int dmPanningWidth;
public int dmPanningHeight;
}
static void Main(string[] args)
{
DEVMODE vDevMode = new DEVMODE();
int i = 0;
while (EnumDisplaySettings(null, i, ref vDevMode))
{
Console.WriteLine("Width:{0} Height:{1} Color:{2} Frequency:{3}",
vDevMode.dmPelsWidth,
vDevMode.dmPelsHeight,
1 << vDevMode.dmBitsPerPel, vDevMode.dmDisplayFrequency
);
i++;
}
}
}
}

Switch between Landscape and Portrait mode in two screens

I can switch between Landsacpe and Portrait mode in one screen by using the following code (That I have done this morning). And the problem is I have two screens in a computer. And I want the screen 1 is LANDSCAPE mode, and the screen 2 is PORTRAIT mode. But I don't know how to modify the code to make it work. (Sorry about my English)
class ModeSetting
{
public const int LANDSCAPE = 0;
public const int PORTRAIT = 1;
public static bool ChangeMode(int screenID, int modeID)
{
//I'm not using screenID here. Let me know how to do that. How to get screenID
//Or anything that help me.
DEVMODE dm = new DEVMODE();
dm.dmDeviceName = new string(new char[32]);
dm.dmFormName = new string(new char[32]);
//dm.dmSize = Marshal.SizeOf(dm);
if (modeID != PORTRAIT) modeID = LANDSCAPE;
if (0 != NativeMethods.EnumDisplaySettings(
null,
NativeMethods.ENUM_CURRENT_SETTINGS,
ref dm))
{
int temp = dm.dmPelsHeight;
dm.dmPelsHeight = dm.dmPelsWidth;
dm.dmPelsWidth = temp;
dm.dmDisplayOrientation = modeID;
int iRet = NativeMethods.ChangeDisplaySettings(ref dm, 0);
Console.WriteLine(iRet);
if (NativeMethods.DISP_CHANGE_SUCCESSFUL != iRet)
{
return false;
}
return true;
}
return false;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct DEVMODE
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public int dmPositionX;
public int dmPositionY;
public int dmDisplayOrientation;
public int dmDisplayFixedOutput;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string dmFormName;
public short dmLogPixels;
public short dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
public int dmICMMethod;
public int dmICMIntent;
public int dmMediaType;
public int dmDitherType;
public int dmReserved1;
public int dmReserved2;
public int dmPanningWidth;
public int dmPanningHeight;
}
internal class NativeMethods
{
// PInvoke declaration for EnumDisplaySettings Win32 API
[DllImport("user32.dll", CharSet = CharSet.Ansi)]
public static extern int EnumDisplaySettings(
string lpszDeviceName,
int iModeNum,
ref DEVMODE lpDevMode);
// PInvoke declaration for ChangeDisplaySettings Win32 API
[DllImport("user32.dll", CharSet = CharSet.Ansi)]
public static extern int ChangeDisplaySettings(
ref DEVMODE lpDevMode,
int dwFlags);
// constants
public const int ENUM_CURRENT_SETTINGS = -1;
public const int DMDO_DEFAULT = 0;
public const int DMDO_90 = 1;
public const int DMDO_180 = 2;
public const int DMDO_270 = 3;
public const int DISP_CHANGE_SUCCESSFUL = 0;
}
}
And I can switch between LANDSCAPE and PORTRAIT mode by using:
ModeSetting.ChangeMode(0, ModeSetting.LANDSCAPE);//0 here is screen ID that I cannot do when two screens
ModeSetting.ChangeMode(0, ModeSetting.PORTRAIT);

How to get all monospaced fonts in Windows?

I've seen How do I get all installed fixed-width fonts?, but I can't make it work:
internal class NativeMethods
{
public const Int32 LF_FACESIZE = 32;
public const Int32 FIXED_PITCH = 1;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class LOGFONT
{
public Int32 lfHeight = 0;
public Int32 lfWidth = 0;
public Int32 lfEscapement = 0;
public Int32 lfOrientation = 0;
public Int32 lfWeight = 0;
public Byte lfItalic = 0;
public Byte lfUnderline = 0;
public Byte lfStrikeOut = 0;
public Byte lfCharSet = 0;
public Byte lfOutPrecision = 0;
public Byte lfClipPrecision = 0;
public Byte lfQuality = 0;
public Byte lfPitchAndFamily = 0;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)]
public String lfFaceName = string.Empty;
}
}
public partial class MainForm : Form
{
private string font_names = null;
public MainForm()
{
InitializeComponent();
StringBuilder sb = new StringBuilder();
foreach (var font_family in FontFamily.Families)
{
if (font_family.IsStyleAvailable(FontStyle.Regular))
{
var lf = new NativeMethods.LOGFONT();
Font font = new Font(font_family, 9.0f);
font.ToLogFont(lf);
if ((lf.lfPitchAndFamily & 0x3) == NativeMethods.FIXED_PITCH)
{
sb.AppendLine(font_family.Name);
}
}
}
font_names = sb.ToString();
}
private void MainForm_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawString(font_names, SystemFonts.MessageBoxFont, SystemBrushes.WindowText, 10.0f, 10.0f);
}
}
It seem no matter what font it is, the lfPitchAndFamily is always zero.
So how get all monospaced fonts?
I think I'll just use P/Invoke to do this:
internal class NativeMethods
{
public const Int32 LF_FACESIZE = 32;
public const Int32 LF_FULLFACESIZE = 64;
public const Int32 DEFAULT_CHARSET = 1;
public const Int32 FIXED_PITCH = 1;
public const Int32 TRUETYPE_FONTTYPE = 0x0004;
public delegate Int32 FONTENUMPROC(ref ENUMLOGFONT lpelf, ref NEWTEXTMETRIC lpntm, UInt32 FontType, IntPtr lParam);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct LOGFONT
{
public Int32 lfHeight;
public Int32 lfWidth;
public Int32 lfEscapement;
public Int32 lfOrientation;
public Int32 lfWeight;
public Byte lfItalic;
public Byte lfUnderline;
public Byte lfStrikeOut;
public Byte lfCharSet;
public Byte lfOutPrecision;
public Byte lfClipPrecision;
public Byte lfQuality;
public Byte lfPitchAndFamily;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)]
public String lfFaceName;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct TEXTMETRIC
{
public Int32 tmHeight;
public Int32 tmAscent;
public Int32 tmDescent;
public Int32 tmInternalLeading;
public Int32 tmExternalLeading;
public Int32 tmAveCharWidth;
public Int32 tmMaxCharWidth;
public Int32 tmWeight;
public Int32 tmOverhang;
public Int32 tmDigitizedAspectX;
public Int32 tmDigitizedAspectY;
public Char tmFirstChar;
public Char tmLastChar;
public Char tmDefaultChar;
public Char tmBreakChar;
public Byte tmItalic;
public Byte tmUnderlined;
public Byte tmStruckOut;
public Byte tmPitchAndFamily;
public Byte tmCharSet;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct ENUMLOGFONT
{
public LOGFONT elfLogFont;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FULLFACESIZE)]
public String elfFullName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = LF_FACESIZE)]
public String elfStyle;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct NEWTEXTMETRIC
{
public Int32 tmHeight;
public Int32 tmAscent;
public Int32 tmDescent;
public Int32 tmInternalLeading;
public Int32 tmExternalLeading;
public Int32 tmAveCharWidth;
public Int32 tmMaxCharWidth;
public Int32 tmWeight;
public Int32 tmOverhang;
public Int32 tmDigitizedAspectX;
public Int32 tmDigitizedAspectY;
public Char tmFirstChar;
public Char tmLastChar;
public Char tmDefaultChar;
public Char tmBreakChar;
public Byte tmItalic;
public Byte tmUnderlined;
public Byte tmStruckOut;
public Byte tmPitchAndFamily;
public Byte tmCharSet;
public UInt32 ntmFlags;
public UInt32 ntmSizeEM;
public UInt32 ntmCellHeight;
public UInt32 ntmAvgWidth;
}
[DllImport("gdi32.dll", CharSet = CharSet.Auto)]
public extern static Int32 EnumFontFamiliesEx(IntPtr hdc, ref LOGFONT lpLogfont, FONTENUMPROC lpEnumFontFamExProc, IntPtr lParam, UInt32 dwFlags);
}
internal static class Program
{
private static void Main()
{
Graphics graphics = Graphics.FromHwnd(IntPtr.Zero);
IntPtr hdc = graphics.GetHdc();
var logfont = new NativeMethods.LOGFONT() { lfCharSet = NativeMethods.DEFAULT_CHARSET };
NativeMethods.EnumFontFamiliesEx(hdc, ref logfont, new NativeMethods.FONTENUMPROC(EnumFontFamExProc), IntPtr.Zero, 0);
graphics.ReleaseHdc();
}
private static int EnumFontFamExProc(ref NativeMethods.ENUMLOGFONT lpelf, ref NativeMethods.NEWTEXTMETRIC lpntm, uint FontType, IntPtr lParam)
{
if ((lpelf.elfLogFont.lfPitchAndFamily & 0x3) == NativeMethods.FIXED_PITCH)
{
Console.WriteLine(lpelf.elfLogFont.lfFaceName);
}
return 1;
}
}

Get the supported screen resolutions in XNA?

How can I get the supported screen resolutions in XNA? Like when you change the screen resolution in windows, and instead of giving you a list of all the possible choices, it gives you just a few of them.
Just use this:
foreach (DisplayMode mode in GraphicsAdapter.DefaultAdapter.SupportedDisplayModes) {
//mode.whatever (and use any of avaliable information)
}
But it will give you few duplicates, because it also takes into accound refrash rate, so you might include that aswel, or do some filtering.
I'm not that up-to-date with XNA, but I didn't think there was a quick and easy function. There is a way using the old WinForms API, but as I personally don't want to link against that in other applications the easiest way is to use the native functions.
First, define the native struct that will be used:
[StructLayout(LayoutKind.Sequential)]
internal struct DEVMODE
{
private const int CCHDEVICENAME = 0x20;
private const int CCHFORMNAME = 0x20;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public int dmPositionX;
public int dmPositionY;
public int dmDisplayOrientation;
public int dmDisplayFixedOutput;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
public string dmFormName;
public short dmLogPixels;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
public int dmICMMethod;
public int dmICMIntent;
public int dmMediaType;
public int dmDitherType;
public int dmReserved1;
public int dmReserved2;
public int dmPanningWidth;
public int dmPanningHeight;
}
We also need to define the two native functions we will use:
[DllImport("user32.dll")]
private static extern bool EnumDisplaySettings(string lpszDeviceName, int iModeNum, ref DEVMODE lpDevMode);
[DllImport("user32.dll")]
private static extern int GetSystemMetrics(int nIndex);
And last, our functions to list all screen resolutions and one to get the current screen resolution:
public static List<string> GetScreenResolutions()
{
var resolutions = new List<string>();
try
{
var devMode = new DEVMODE();
int i = 0;
while (EnumDisplaySettings(null, i, ref devMode))
{
resolutions.Add(string.Format("{0}x{1}", devMode.dmPelsWidth, devMode.dmPelsHeight));
i++;
}
resolutions = resolutions.Distinct(StringComparer.InvariantCulture).ToList();
}
catch (Exception ex)
{
Console.WriteLine("Could not get screen resolutions.");
}
return resolutions;
}
public static string GetCurrentScreenResolution()
{
int width = GetSystemMetrics(0x00);
int height = GetSystemMetrics(0x01);
return string.Format("{0}x{1}", width, height);
}

How to list available video modes using C#?

I've found nice examples using C++ (http://www.codeproject.com/KB/tips/resswitch.aspx), but not in C#.
Can someone help, please?
Edit:
The exact function that list the video modes is:
BOOL CVideoModes::GetAvailableVideoModes(CAvailableVideoModes& modes)
{
modes.SetSize(0, 5);
int i=0;
DEVMODE dm;
while (EnumDisplaySettings(NULL, i, &dm))
{
CVideoMode thismode(dm.dmBitsPerPel, dm.dmPelsWidth,
dm.dmPelsHeight, dm.dmDisplayFrequency);
modes.SetAtGrow(i, thismode);
++i;
}
modes.FreeExtra();
return (i>0);
}
But sincerelly I cannot understand that C++ code. Where I can find that "thismode" function?
If you mean video modes are available resolutions, try to invoke EnumDisplaySettingsEx
details can be found here:
http://msdn.microsoft.com/en-us/library/dd162612(VS.85).aspx
small program that lists available resolutions:
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ListResolutions
{
class Program
{
[DllImport("user32.dll")]
public static extern bool EnumDisplaySettings(
string deviceName, int modeNum, ref DEVMODE devMode);
const int ENUM_CURRENT_SETTINGS = -1;
const int ENUM_REGISTRY_SETTINGS = -2;
[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE
{
private const int CCHDEVICENAME = 0x20;
private const int CCHFORMNAME = 0x20;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public int dmPositionX;
public int dmPositionY;
public ScreenOrientation dmDisplayOrientation;
public int dmDisplayFixedOutput;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x20)]
public string dmFormName;
public short dmLogPixels;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
public int dmICMMethod;
public int dmICMIntent;
public int dmMediaType;
public int dmDitherType;
public int dmReserved1;
public int dmReserved2;
public int dmPanningWidth;
public int dmPanningHeight;
}
static void Main(string[] args)
{
DEVMODE vDevMode = new DEVMODE();
int i = 0;
while (EnumDisplaySettings(null, i, ref vDevMode))
{
Console.WriteLine("Width:{0} Height:{1} Color:{2} Frequency:{3}",
vDevMode.dmPelsWidth,
vDevMode.dmPelsHeight,
1 << vDevMode.dmBitsPerPel, vDevMode.dmDisplayFrequency
);
i++;
}
}
}
}

Categories

Resources