Using SystemParameterInfo API call
Author |
Message |
Frank #1 / 8
|
 Using SystemParameterInfo API call
Can someone please help me with the following code? I'm having trouble with the out variable. using System; using System.ComponentModel; using System.Runtime.InteropServices; using System.Collections; using System.Windows.Forms; public class WinAPI { [DllImport("user32.dll", CharSet=CharSet.Auto)] public static extern int SystemParametersInfo (int uAction,int uParam,bool lpvParam,int fuWinIni); static void Main() { int nResult0; nResult0 = WinAPI.SystemParametersInfo (16,0,SC_STAT,0); Console.WriteLine(nResult0); } Quote: }
|
Tue, 24 May 2005 01:04:25 GMT |
|
 |
Randy A. Ynchaust #2 / 8
|
 Using SystemParameterInfo API call
Frank, What trouble are you having? Assuming that: SC_STAT = true And making the appropriate substitution, the code runs for me without errors. nResult0 returns a 0. Regards, Randy Quote: > Can someone please help me with the following code? I'm > having trouble with the out variable. > using System; > using System.ComponentModel; > using System.Runtime.InteropServices; > using System.Collections; > using System.Windows.Forms; > public class WinAPI > { > [DllImport("user32.dll", CharSet=CharSet.Auto)] > public static extern int SystemParametersInfo > (int uAction,int uParam,bool lpvParam,int fuWinIni); > static void Main() > { > int nResult0; > nResult0 = WinAPI.SystemParametersInfo > (16,0,SC_STAT,0); > Console.WriteLine(nResult0); > } > }
|
Tue, 24 May 2005 01:40:43 GMT |
|
 |
Frank #3 / 8
|
 Using SystemParameterInfo API call
In this particular call a return of 0 is a failure. SC_STAT is supposed to return true or false but I get an error of: Use of unassigned local variable 'SC_STAT' when I try to compile. Thanks Quote: >-----Original Message----- >Frank, >What trouble are you having? Assuming that: >SC_STAT = true >And making the appropriate substitution, the code runs for me without >errors. nResult0 returns a 0. >Regards, >Randy >> Can someone please help me with the following code? I'm >> having trouble with the out variable. >> using System; >> using System.ComponentModel; >> using System.Runtime.InteropServices; >> using System.Collections; >> using System.Windows.Forms; >> public class WinAPI >> { >> [DllImport("user32.dll", CharSet=CharSet.Auto)] >> public static extern int SystemParametersInfo >> (int uAction,int uParam,bool lpvParam,int fuWinIni); >> static void Main() >> { >> int nResult0; >> nResult0 = WinAPI.SystemParametersInfo >> (16,0,SC_STAT,0); >> Console.WriteLine(nResult0); >> } >> } >.
|
Tue, 24 May 2005 01:55:02 GMT |
|
 |
Martin Deche #4 / 8
|
 Using SystemParameterInfo API call
Hi, Frank, I didn't manage to do it with safe code, but with this unsafe code it returns non-zero: using System; using System.Runtime.InteropServices; unsafe class WinAPI { [DllImport("user32.dll")] static extern int SystemParametersInfo( uint uiAction, uint uiParam, int* pvParam, uint fWinIni) static void Main() { int a = 0; int* p = &a; int result = SystemParametersInfo(16, 0, p, 0); Console.WriteLine( "Returned: \t{0}\nResult: \t{1}", result != 0, a != 0); Quote: } }
Hope this helps Marty
Quote: > Can someone please help me with the following code? I'm > having trouble with the out variable. > using System; > using System.ComponentModel; > using System.Runtime.InteropServices; > using System.Collections; > using System.Windows.Forms; > public class WinAPI > { > [DllImport("user32.dll", CharSet=CharSet.Auto)] > public static extern int SystemParametersInfo > (int uAction,int uParam,bool lpvParam,int fuWinIni); > static void Main() > { > int nResult0; > nResult0 = WinAPI.SystemParametersInfo > (16,0,SC_STAT,0); > Console.WriteLine(nResult0); > } > }
|
Tue, 24 May 2005 03:16:37 GMT |
|
 |
Randy A. Ynchaust #5 / 8
|
 Using SystemParameterInfo API call
Frank, Quote: > In this particular call a return of 0 is a failure. > SC_STAT is supposed to return true or false but I get an > error of: Use of unassigned local variable 'SC_STAT' > when I try to compile.
Yep, SC_STAT is not assigned, or event declared, except in the function call. I compiled and ran the code by replacing "SC_STAT" with true. Therefore, if you put the following line in the Main method, then the code will compile and run: System.Boolean SC_STAT = true; Is that the problem you were having? Regards, Randy
|
Tue, 24 May 2005 03:18:02 GMT |
|
 |
Frank #6 / 8
|
 Using SystemParameterInfo API call
This did the trick. Thanks to you Martin and Randy as well for responding. Frank Quote: >-----Original Message----- >Hi, Frank, >I didn't manage to do it with safe code, but with this unsafe code it >returns non-zero: >using System; >using System.Runtime.InteropServices; >unsafe class WinAPI >{ >[DllImport("user32.dll")] >static extern int SystemParametersInfo( > uint uiAction, > uint uiParam, > int* pvParam, > uint fWinIni) >static void Main() >{ > int a = 0; > int* p = &a; > int result = SystemParametersInfo(16, 0, p, 0); > Console.WriteLine( > "Returned: \t{0}\nResult: \t{1}", > result != 0, > a != 0); >} >} >Hope this helps >Marty
>> Can someone please help me with the following code? I'm >> having trouble with the out variable. >> using System; >> using System.ComponentModel; >> using System.Runtime.InteropServices; >> using System.Collections; >> using System.Windows.Forms; >> public class WinAPI >> { >> [DllImport("user32.dll", CharSet=CharSet.Auto)] >> public static extern int SystemParametersInfo >> (int uAction,int uParam,bool lpvParam,int fuWinIni); >> static void Main() >> { >> int nResult0; >> nResult0 = WinAPI.SystemParametersInfo >> (16,0,SC_STAT,0); >> Console.WriteLine(nResult0); >> } >> } >.
|
Tue, 24 May 2005 04:48:31 GMT |
|
 |
Tom Shelto #7 / 8
|
 Using SystemParameterInfo API call
Frank, I'm not sure exactly what your trying to accomplish, but since it looks like your trying to determine if the user has an active screen saver, here is a little code that I just did up for a co-worker the other day. You should avoid using unsafe (actually P/Invoke for that matter)... Any way the following code starts the active screens saver.... using System; using System.Runtime.InteropServices; namespace ConsoleApplication11 { class Class1 { [STAThread] static void Main(string[] args) { Class1.StartScreenSaver(); } static void StartScreenSaver() { bool screenSaverActive; // determine if the screen saver is active... Class1.SystemParametersInfo( Class1.SPI_GETSCREENSAVEACTIVE, 0, out screenSaverActive, 0); if (screenSaverActive) { Class1.SendMessage( Class1.GetDesktopWindow(), Class1.WM_SYSCOMMAND, Class1.SC_SCREENSAVE, 0); } else { Console.WriteLine("No Active Screen Saver"); } } private const uint SPI_GETSCREENSAVEACTIVE = 0x0010; private const uint WM_SYSCOMMAND = 0x0112; private const uint SC_SCREENSAVE = 0xF140; [DllImport("user32", CharSet=CharSet.Auto, SetLastError=true)] private extern static bool SystemParametersInfo( uint uiAction, // system parameter to retrieve or set uint uiParam, // depends on action to be taken out bool pvParam, // depends on action to be taken uint fWinIni // user profile update option ); [DllImport("user32", SetLastError=true)] private extern static IntPtr GetDesktopWindow(); [DllImport("user32", CharSet=CharSet.Auto, SetLastError=true)] private extern static IntPtr SendMessage( IntPtr hwnd, uint message, uint wParam, uint lParam); } Quote: }
HTH, Tom Shelton
|
Tue, 24 May 2005 05:24:22 GMT |
|
 |
Frank #8 / 8
|
 Using SystemParameterInfo API call
Thanks for the code. This is great. Quote: >-----Original Message----- >Frank, >I'm not sure exactly what your trying to accomplish, but since it looks like >your trying to determine if the user has an active
screen saver, here is a Quote: >little code that I just did up for a co-worker the other day. You should >avoid using unsafe (actually P/Invoke for that
matter)... Any way the Quote: >following code starts the active screens saver.... >using System; >using System.Runtime.InteropServices; >namespace ConsoleApplication11 >{ > class Class1 > { > [STAThread] > static void Main(string[] args) > { > Class1.StartScreenSaver(); > } > static void StartScreenSaver() > { > bool screenSaverActive; > // determine if the screen saver is active... > Class1.SystemParametersInfo( > Class1.SPI_GETSCREENSAVEACTIVE, > 0, > out screenSaverActive, > 0); > if (screenSaverActive) > { > Class1.SendMessage( > Class1.GetDesktopWindow(), > Class1.WM_SYSCOMMAND, > Class1.SC_SCREENSAVE, > 0); > } > else > { > Console.WriteLine("No Active Screen Saver"); > } > } > private const uint SPI_GETSCREENSAVEACTIVE = 0x0010; > private const uint WM_SYSCOMMAND = 0x0112; > private const uint SC_SCREENSAVE = 0xF140; > [DllImport("user32", CharSet=CharSet.Auto, SetLastError=true)] > private extern static bool SystemParametersInfo( > uint uiAction, // system parameter to retrieve or set > uint uiParam, // depends on action to be taken > out bool pvParam, // depends on action to be taken > uint fWinIni // user profile update option > ); > [DllImport("user32", SetLastError=true)] > private extern static IntPtr GetDesktopWindow(); > [DllImport("user32", CharSet=CharSet.Auto, SetLastError=true)] > private extern static IntPtr SendMessage( > IntPtr hwnd, > uint message, > uint wParam, > uint lParam); > } >} >HTH, >Tom Shelton >.
|
Wed, 25 May 2005 04:26:05 GMT |
|
|
|