01-06-2025 05:29 AM
Dear community,
in order to ensure portability of simple VIs with front panels containing "copy-pasted" images (used as decoration for the GUI) onto computers with different screen resolution, I thought about setting the resolution of the computer screen to a pre-set value at the start of the VI and reset the initial screen resolution when the VI is stopped (maybe there is a smarter way to ensure coherent rendering of VI front panels with images on different screen resolution while keeping exact distances between elements and images?).
After some research, I concluded that the best way to proceed is to use the windows API user32.dll.
I found a simple example using user32.dll to get the screen resolution and other system's metrics: very simple.
But when it comes to set screen resolution via the user32.dll, then comes the issue of parameter formatting, etc. I am not an expert at all with dll, and I could not find a recent example working for the recent version of LabVIEW and Win (only an old version that is not working, or other solution with custom-made DLLs). But I would like to use user32.dll (also a good way to learn how to use this useful resource).
With a bit of googling, window's doc and chatGPT, I managed to make a simple example that does not send error, but nothing seems to be changed.
Probably a very stupid mistake or a formatting error somewhere.
LabVIEW 2023 on Windows 10 Enterprise on 64-bit operating system, x64-based processor
VI attached.
Any help is very welcome 🙂
Thanks a lot
regards,
Hicham
01-07-2025 04:11 AM
@Cham35 wrote:After some research, I concluded that the best way to proceed is to use the windows API user32.dll.
Did you consider .NET?
c# - Get and Set Screen Resolution - Stack Overflow
Of course the might be user rights issue. I assume Windows doesn't allow just any application to change the resolution, .NET or PE DLL.
Even with Admin rights, your application might need to be started explicitly as an admin to get elevated rights.
01-07-2025 04:16 AM
wiebe@CARYA wrote:
@Cham35 wrote:After some research, I concluded that the best way to proceed is to use the windows API user32.dll.
Did you consider .NET?
c# - Get and Set Screen Resolution - Stack Overflow
Of course the might be user rights issue. I assume Windows doesn't allow just any application to change the resolution, .NET or PE DLL.
Even with Admin rights, your application might need to be started explicitly as an admin to get elevated rights.
Since most games can do it without admin rights i don't think it's harschly protected.
01-07-2025 09:34 AM
Thanks for the swift feedback.
I guess .NET could work then.
I am not very familiar with .NET.
It looks like I don't have access to System.Windows.Forms.Screen.
Am I missing something fundamental? Can I update or download the missing constructor?
The App property node are in read only.
01-07-2025 10:24 AM
You are creating a constructor. Skip that. Use a property node directly.
01-07-2025 11:36 AM - edited 01-07-2025 11:42 AM
@Cham35 wrote:
I guess .NET could work then.
...
If you read the linked article, the accepted answer goes like this:
For retrieving the screen resolution, you're going to want to use theSystem.Windows.Forms.Screen
class. [...]
For changing the resolution, things get a little more complicated. This article (or this one) provides a detailed implementation and explanation.
So it seems you were correct in your initial assessment of needing to call system32. At first glance, it seems like your DEVMODE is wrong and does not have the correct members (I think there is at least one string in there?). I don't remember off the cuff where it is, but there are a few thread in the forum archives on how to set up clusters for passing as complex structs. Also, check the return value and GetLastError functions to see what went wrong.
I also recommend not using any shortcuts by using chatbots. It is important to understand what you are doing when calling system32. Also, you might want to try this in a virtual machine first. Playing with the low-level stuff is a good way to mess up your system.
01-08-2025 08:24 AM
@LLindenbauer wrote:So it seems you were correct in your initial assessment of needing to call system32.
Only if you can get that working.
But I agree .net doesn't make it a lot easier. That first link is broken, the second uses Resolution Class (Iot.Device.Media) | Microsoft Learn, that seems to be completely out of context.
QRes @ Sourceforge might still work. It does, according to How to change screen resolution with command on Windows 10 - Pureinfotech. It would do all the heavy lifting. If it works from the command line, it's easy to call the command line from LabVIEW.
01-08-2025 09:20 AM - edited 01-08-2025 09:24 AM
According to MSDN the DEVMODEA structure is A LOT more complex than what your image shows:
typedef struct _devicemodeA {
BYTE 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;
} DUMMYSTRUCTNAME;
POINTL dmPosition;
struct {
POINTL dmPosition;
DWORD dmDisplayOrientation;
DWORD dmDisplayFixedOutput;
} DUMMYSTRUCTNAME2;
} DUMMYUNIONNAME;
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;
} DUMMYUNIONNAME2;
DWORD dmDisplayFrequency;
DWORD dmICMMethod;
DWORD dmICMIntent;
DWORD dmMediaType;
DWORD dmDitherType;
DWORD dmReserved1;
DWORD dmReserved2;
DWORD dmPanningWidth;
DWORD dmPanningHeight;
} DEVMODEA, *PDEVMODEA, *NPDEVMODEA, *LPDEVMODEA;
That doesn't even resemble your cluster you try to use in almost anything.
A C structure is NOT a Python dictionary where you can pick out individual elements by name and hope that the other elements are somehow initialize to anything useful, respective the consumer will not crash if a particular dictionary element is not present.