Windows API

BlitzMax Forums/BlitzMax Beginners Area/Windows API

xlsior(Posted 2006) [#1]
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!


Yan(Posted 2006) [#2]
Strict

Extern "Win32"
	Function SetSystemPowerState(fSuspend, fForce)
End Extern

SetSystemPowerState(False, False)


??


xlsior(Posted 2006) [#3]
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.


Chris C(Posted 2006) [#4]
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


Dreamora(Posted 2006) [#5]
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.


Yan(Posted 2006) [#6]
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)


Grey Alien(Posted 2006) [#7]
I spent a whole day yesterday trying to get some mouse hook (via Windows API) and dll code working in Delphi, what a stinker.


Who was John Galt?(Posted 2006) [#8]
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?


Chris C(Posted 2006) [#9]
it'll just use the same dll ;)


xlsior(Posted 2006) [#10]
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...


EOF(Posted 2006) [#11]
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:



xlsior(Posted 2006) [#12]
Still no go... No errors, yet nothing happens.


Yan(Posted 2006) [#13]



xlsior(Posted 2006) [#14]
Fantastic! Looks like that did the trick.

Thank you so much.


EOF(Posted 2006) [#15]
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