Windows API
BlitzMax Forums/BlitzMax Beginners Area/Windows API
| ||
I'm trying to get BlitzMax to call a Windows API function, without much success... Anyone can shed some light on what I need to do here? It compiles without error, but attempting to run it returns the error: Attempt to call uninitialized function pointer Any help would be appreciated! |
| ||
Strict Extern "Win32" Function SetSystemPowerState(fSuspend, fForce) End Extern SetSystemPowerState(False, False) ?? |
| ||
Thanks! <testing> Well... It doesn't give any errors, but it also doesn't actually seem to *do* anything... even with the 'force' parameter in place, which is supposed to forcefully terminate any program holding up sleep/hibernation Hm. |
| ||
Global SetSystemPowerState:Int(fSuspend, fForce) "win32" Global GetLastError:Int() "win32" lib = LoadLibraryA ("kernel32.dll") If lib SetSystemPowerState = GetProcAddress(lib, "SetSystemPowerState") GetLastError=GetProcAddress(lib, "GetLastError") Else Print "Dead"; End EndIf SetSystemPowerState(False,False) e=GetLastError() Print e ERROR_PRIVILEGE_NOT_HELD=1314! do enjoy the windows api it is *such* a joy to use NOT!!! lol |
| ||
You aren't trying to do such stuff with a regular user account are you? System control calls only work with administration rights on real windows (XP SP2 and similar) as they are considered to be the same security level as system settings. |
| ||
From MSDN...*SHUDDER*... The calling process must have the SE_SHUTDOWN_NAME privilege. To enable the SE_SHUTDOWN_NAME privilege, use the AdjustTokenPrivileges function. For more information, see Changing Privileges in a Token. ...I haven't a clue what that means exactly, but have fun. ;o) |
| ||
I spent a whole day yesterday trying to get some mouse hook (via Windows API) and dll code working in Delphi, what a stinker. |
| ||
First off, thanks ChrisC - you answered a question I was about to ask. But another question... Suppose you have more than one module that 'LoadLibraryA's the same dll.... is this wasteful or is the routine smart enough to use one copy? |
| ||
it'll just use the same dll ;) |
| ||
You aren't trying to do such stuff with a regular user account are you? I also get the 1314 error when running as Administrator, so it looks like there is something weird going on indeed... I verified that the process in the taks manager's process list is running as 'administrator' indeed... some more googling found others with similar issues, and supposedly "the caller requires the SE_SHUTDOWN_NAME privilege", whatever that means... Supposedly there's a call for AdjustTokenPrivileges that needs to be invoked, but I have absolutely no idea on how to translate that to blitzmax... I found some visual basic code that does it here: http://www.vbaccelerator.com/home/VB/Tips/How_to_Shutdown_the_System_in_Windows_9x_and_NT/article.asp but the syntax is very different from BlitzMax's API stuff and I haven't been able to get anything workable out of it. Anyone else has any idea on how to convert this? All I'm trying to do is to programatically put my computer in Hibernation mode, but not having a whole lot of luck so far... |
| ||
Had a go here but I'm getting a failure at OpenProcessToken() call in the EnableShutDown() function. I'm not sure if GetCurrentProcess() is causing the problem here. I always get -1 as the return value. See if this works for you: |
| ||
Still no go... No errors, yet nothing happens. |
| ||
|
| ||
Fantastic! Looks like that did the trick. Thank you so much. |
| ||
Good work Yan. Works on my system. I've created a set of functions and a GUI test window to try them out. Functions: Standby [flag] Hibernate [flag] Shutdown [flag] Restart [flag] LogOff [flag] If optional flag is TRUE all programs are forced to close GUI Test Window Shutdown Functions - Shutdown.bmx |