Shoutbox

[Question] Call SystemParametersInfo - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: [Question] Call SystemParametersInfo (/showthread.php?tid=81011)

[Question] Call SystemParametersInfo by mynetx on 01-17-2008 at 04:58 PM

My next problem is one regarding the SystemParametersInfo Function (Windows).

I got the code

code:
var SPI_GETWORKAREA = 48;
var intScreenX, intScreenY;
// some code not found out yet
Interop.Call("user32", "SystemParametersInfo", SPI_GETWORKAREA, 0, outRect, 0);
// some code not found out yet
Debug.Trace(intScreenX + ", " + intScreenY);

Should return the desktop size minus taskbar etc. (in difference to Interop.Call('user32','GetSystemMetrics',0x4e);).
Anybody wants to help me out in this regard as how to solve it?
RE: [Question] Call SystemParametersInfo by Matti on 01-17-2008 at 05:24 PM

I highly recommend you to take a look at other scripts when you're learning to use the Win32 API functions. Mainly, it's about understanding what parameters require what type of variables (lpsz = string buffer pointer, long = integer,...) and converting it to a Plus! script.

So, here's what I'd like to say to you:

  • The SystemParametersInfo function is implemented as both Unicode and ANSI version in the API, as noted in the Requirements section at the bottom of that MSDN page. That means, that we should use the "SystemParametersInfoW" function because JScript uses Unicode. ;)
  • When the documentation says that the pvParam must be a RECT structure for SPI_GETWORKAREA, you should create one! It won't know that you have 2 variables to retrieve the information, and it doesn't expect that anyway! You should therefore create a DataBloc which can hold 4 values (left, top, right, bottom) of 4 bytes each (as they're LONG types), thus 16 bytes in total for your DataBloc. Then, you can pass that DataBloc to the API function. :)
  • It's recommended that you check the return value of the function. When it's zero, an error has occured and thus your script shouldn't continue with the retrieving.
  • The RECT structure will be filled with all the data you need then. Since the right and bottom edges are exclusive, you can simply substract right from left to get the width and bottom from top to get the height. After you safely stored this information in 2 variables, you can clear the RECT to save some memory. Imagine having all programs forgetting to clean their used memory, now that would cause some memory overflows! :O
code:
var SPI_GETWORKAREA = 48;
//Create a RECT structure to receive the work area rectangle in
var RECT = Interop.Allocate(16);
//Call the API function
var Result = Interop.Call("user32", "SystemParametersInfoW", SPI_GETWORKAREA, 0, RECT, 0);
//Check the result
if(Result == 0) {
   Debug.Trace("SystemParametersInfo failed!");
   //Do your error handling stuff here...
}
//Read the data from the structure
var ScreenWidth = RECT.ReadDWORD(8) - RECT.ReadDWORD(0);
var ScreenHeight = RECT.ReadDWORD(12) - RECT.ReadDWORD(4);
//Clear the structure, because we no longer need it
RECT.Size = 0;
//Do whatever you want with the ScreenWidth and ScreenHeight variables...

RE: [Question] Call SystemParametersInfo by mynetx on 01-17-2008 at 05:33 PM

Thank you Mattike for your tutorial (Y).