Call api EnumDisplaySettings error

  • Thread starter Thread starter zengou
  • Start date Start date
Z

zengou

MSDN:
BOOL EnumDisplaySettings(
LPCTSTR lpszDeviceName, // display device
DWORD iModeNum, // graphics mode
LPDEVMODE lpDevMode // graphics mode settings
);

typedef struct _devicemode {
BCHAR dmDeviceName[CCHDEVICENAME];
WORD dmSpecVersion;
WORD dmDriverVersion;
WORD dmSize;
WORD dmDriverExtra;
DWORD dmFields;
union {
struct {
short dmOrientation;
short dmPaperSize;
short dmPaperLength;
short dmPaperWidth;
short dmScale;
short dmCopies;
short dmDefaultSource;
short dmPrintQuality;
};
POINTL dmPosition;
DWORD dmDisplayOrientation;
DWORD dmDisplayFixedOutput;
};

short dmColor;
short dmDuplex;
short dmYResolution;
short dmTTOption;
short dmCollate;
BYTE dmFormName[CCHFORMNAME];
WORD dmLogPixels;
DWORD dmBitsPerPel;
DWORD dmPelsWidth;
DWORD dmPelsHeight;
union {
DWORD dmDisplayFlags;
DWORD dmNup;
}
DWORD dmDisplayFrequency;
#if(WINVER >= 0x0400)
DWORD dmICMMethod;
DWORD dmICMIntent;
DWORD dmMediaType;
DWORD dmDitherType;
DWORD dmReserved1;
DWORD dmReserved2;
#if (WINVER >= 0x0500) || (_WIN32_WINNT >= 0x0400)
DWORD dmPanningWidth;
DWORD dmPanningHeight;
#endif
#endif /* WINVER >= 0x0400 */
} DEVMODE;

I Have this C# code:

[DllImport("user32.dll",
EntryPoint="EnumDisplaySettingsA")]
public static extern bool EnumDisplaySettings (
string lpszDeviceName,
Int32 iModeNum,
ref DEVMODE lpDevMode);

[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE
{
//[ MarshalAs( UnmanagedType.LPStr)]
public string dmDeviceName;
public int dmSpecVersion;
public int dmDriverVersion;
public int dmSize;
public int dmDriverExtra;
public int dmFields;
public int dmOrientation;
public int dmPaperSize;
public int dmPaperLength;
public int dmPaperWidth;
public int dmScale;
public int dmCopies;
public int dmDefaultSource;
public int dmPrintQuality;
public int dmColor;
public int dmDuplex;
public int dmYResolution;
public int dmTTOption;
public int dmCollate;
//[ MarshalAs( UnmanagedType.LPStr)]
public string dmFormName;
public int dmUnusedPadding;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
}

private void button1_Click(object sender,
System.EventArgs e)
{
DEVMODE DevM=new DEVMODE();
bool mybool;
mybool=EnumDisplaySettings("", 0,ref DevM);
MessageBox.Show(result.ToString()
+DevM.dmPelsWidth.ToString()+DevM.dmPelsHeight.ToString
());

}


But the MessageBox return mybool=false!!
I don't know where I make mistake???
 
[DllImport("user32.dll",
EntryPoint="EnumDisplaySettingsA")]

Change this to

[DllImport("user32.dll", CharSet=CharSet.Auto)]

[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE
{
//[ MarshalAs( UnmanagedType.LPStr)]
public string dmDeviceName;
public int dmSpecVersion;
public int dmDriverVersion;
public int dmSize;
public int dmDriverExtra;
public int dmFields;
public int dmOrientation;
public int dmPaperSize;
public int dmPaperLength;
public int dmPaperWidth;
public int dmScale;
public int dmCopies;
public int dmDefaultSource;
public int dmPrintQuality;
public int dmColor;
public int dmDuplex;
public int dmYResolution;
public int dmTTOption;
public int dmCollate;
//[ MarshalAs( UnmanagedType.LPStr)]
public string dmFormName;
public int dmUnusedPadding;
public int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
}

Make that

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
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 short dmOrientation;
public short dmPaperSize;
public short dmPaperLength;
public short dmPaperWidth;
public short dmScale;
public short dmCopies;
public short dmDefaultSource;
public short dmPrintQuality;
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 int dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
}

private void button1_Click(object sender,
System.EventArgs e)
{
DEVMODE DevM=new DEVMODE();
bool mybool;
mybool=EnumDisplaySettings("", 0,ref DevM);

"" is not the same as NULL if that was what you intended to pass as
the first parameter. Use null instead.

You must also set the DevM.dmSize member before the call, with

DevM.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));



Mattias
 
thank your very much!
now i can get mybool=true!
but,whatever my screen set is,the "dmPelsWidth " always is 320,and the
"dmPelsHeight" always is 200,why??

and,how to use ChangeDisplaySettings,is that correct:

[DllImport("user32.dll",CharSet=CharSet.Auto)]
private static extern long ChangeDisplaySettings(ref DEVMODE
lpDevMode,int dwflags);

DevM.dmPelsWidth = 800;
DevM.dmPelsHeight = 600;
long result=ChangeDisplaySettings(ref DevM, 0);

thank you !!
 
thank you very much!
now i can get mybool=true!
but whatever my screen set is ,the "dmPelsWidth" always is 320,and the
"dmPelsHeight" always is 200,why???

and ,how to use ChangeDisplaySettings,is that correct:

[DllImport("user32.dll",CharSet=CharSet.Auto)]
private static extern long ChangeDisplaySettings(ref DEVMODE
lpDevMode,int dwflags);

DEVMODE DevM=new DEVMODE();
DevM.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
bool mybool;
mybool=EnumDisplaySettings(null,0,ref DevM);
MessageBox.Show(mybool.ToString()+DevM.dmPelsWidth.ToString()+","+DevM.d
mPelsHeight.ToString());
DevM.dmPelsWidth = 800;
DevM.dmPelsHeight = 600;
long result=ChangeDisplaySettings(ref DevM, 0);

after excute it ,my screen is change to 800*600,but the color set is
change to 8bit,it isn't my excepted,i don't want to change my color set
,how can i do ,thank you!!
 
thank you very much!
now i can get mybool=true!
but whatever my screen set is ,the "dmPelsWidth" always is 320,and the
"dmPelsHeight" always is 200,why???

and ,how to use ChangeDisplaySettings,is that correct:

[DllImport("user32.dll",CharSet=CharSet.Auto)]
private static extern long ChangeDisplaySettings(ref DEVMODE
lpDevMode,int dwflags);

DEVMODE DevM=new DEVMODE();
DevM.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
bool mybool;
mybool=EnumDisplaySettings(null,0,ref DevM);
MessageBox.Show(mybool.ToString()+DevM.dmPelsWidth.ToString()+","+DevM.d
mPelsHeight.ToString());
DevM.dmPelsWidth = 800;
DevM.dmPelsHeight = 600;
long result=ChangeDisplaySettings(ref DevM, 0);

after excute it ,my screen is change to 800*600,but the color set is
change to 8bit,it isn't my excepted,i don't want to change my color set
,how can i do ,thank you!!
 
thank you very much!
now i can get mybool=true!
but whatever my screen set is ,the "dmPelsWidth" always is 320,and the
"dmPelsHeight" always is 200,why???

and ,how to use ChangeDisplaySettings,is that correct:

[DllImport("user32.dll",CharSet=CharSet.Auto)]
private static extern long ChangeDisplaySettings(ref DEVMODE
lpDevMode,int dwflags);

DEVMODE DevM=new DEVMODE();
DevM.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
bool mybool;
mybool=EnumDisplaySettings(null,0,ref DevM);
MessageBox.Show(mybool.ToString()+DevM.dmPelsWidth.ToString()+","+DevM.d
mPelsHeight.ToString());
DevM.dmPelsWidth = 800;
DevM.dmPelsHeight = 600;
long result=ChangeDisplaySettings(ref DevM, 0);

after excute it ,my screen is change to 800*600,but the color set is
change to 8bit,it isn't my excepted,i don't want to change my color set
,how can i do ,thank you!!
 
Back
Top