Who to debug this code?

Hello,
I’m trying to disable Mac sleep with this code which uses the IOKit library.
Can anyone help me debug the code because I have two error messages?

Declare Function IOPMAssertionCreateWithName Lib "IOKit" (assertionType As UInt32, assertionLevel As UInt32, reason As CFStringRef, ByRef assertionID As UInt32) As UInt32
Declare Function IOPMAssertionRelease Lib "IOKit" (assertionID As UInt32) As UInt32
Declare Function CFStringCreateWithCString Lib "CoreFoundation" (allocator As Ptr, cString As CString, encoding As UInt32) As Ptr
Declare Sub CFRelease Lib "CoreFoundation" (ref As Ptr)

Const kIOPMAssertionTypeNoIdleSleep = &h40000000
Const kIOPMAssertionLevelOn = &h000000FF

Dim reasonString As String = "Your reason here"
Dim assertionID As UInt32

Dim reasonPtr As Ptr = CFStringCreateWithCString(Nil, reasonString, 0)
IOPMAssertionCreateWithName(kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, reasonPtr, assertionID)
CFRelease(reasonPtr)

// Your code here (computer will not sleep during this time)

//To enable standby again
IOPMAssertionRelease(assertionID)

Window1.Button1.Pressed, line 13
Parameter “reason” expects type CFStringRef, but this is type Ptr.
IOPMAssertionCreateWithName(kIOPMAssertionTypeNoIdleSleep, kIOPMAssertionLevelOn, reasonPtr, assertionID)

and

Window1.Button1.Pressed, line 19
You must use the value returned by this function
IOPMAssertionRelease(assertionID)

Thanks

  1. No reason for your complex CFStringRef handling. A Xojo string will be converted to CFStringRef automatically, so just call AssertionCreate… with reasonString instead of reasonPtr.

  2. If the Release declare is really a function returning a uint32 (I did not check in Apple docs), either use it in such a property or call the function with „Call“ to ignore the return value.

1 Like