Skip to content

while (true) { }

notes from a developer, tutorials, how-to, ideas

Posted on 10 August 202022 July 2020 by Alberto Pasca

Swift / C – Lock macOS programmatically

PrivateFrameworks are always a joy.

Today, for a personal project I discovered how to use private Login.framework to lock programmatically via code (Swift or C) the screen of my mac book.

The method is called “SACLockScreenImmediate“.

You can find a more detailed list of Private Headers on this fork: https://github.com/elpsk/macOS_headers/tree/master/macOS/PrivateFrameworks


Swift code:

import Foundation

func lock() {
    let libHandle = dlopen("/System/Library/PrivateFrameworks/login.framework/Versions/Current/login", RTLD_LAZY)
    let sym = dlsym(libHandle, "SACLockScreenImmediate")
    typealias myFunction = @convention(c) () -> Void

    let SACLockScreenImmediate = unsafeBitCast(sym, to: myFunction.self)
    SACLockScreenImmediate()
}

lock()

Or if you prefer, this is the C version:

C code:

extern int SACLockScreenImmediate ( void );

int main ( ) {
    return SACLockScreenImmediate();
}

Remember to compile using the clang command, like this:

$ clang -F /System/Library/PrivateFrameworks -framework login -o lockscreen lockscreen.c 

Enjoy temporized lock screens!

  • Author
  • Recent Posts
Alberto Pasca
Alberto Pasca
Software engineer at Pirelli & C. S.p.A.
Software engineer with a strong passion for mobile 
development, security and connected things.
Alberto Pasca
Latest posts by Alberto Pasca (see all)
  • iOS – Fake GPS position: how to use and prevent - 15 February 2021
  • Candy Crush – Play without limitations - 9 February 2021
  • Unity3D – Snap object in editor grid - 3 January 2021

Share this:

  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on Telegram (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • More
  • Click to share on Reddit (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to email this to a friend (Opens in new window)

Like this:

Like Loading...

Related

Categoriesgeneric, macOS Tagsc++, dlsym, lock, Login.framework, macos, PrivateFrameworks, SACLockScreenImmediate, screen, swift

Post navigation

Previous PostPrevious Swift – Identify misspelled words and errors in phrases
Next PostNext macOS – App Icon Maker free utility

oʇɹ3qןɐFollow

oʇɹ3qןɐ
lupascaoʇɹ3qןɐ@lupasca·
18 Feb

#Pirelli's #Cyber #Tyre: when tyres talk https://bit.ly/3prBpUE

Reply on Twitter 1362339382536134660Retweet on Twitter 13623393825361346601Like on Twitter 13623393825361346601Twitter 1362339382536134660
lupascaoʇɹ3qןɐ@lupasca·
18 Feb

La gomma intelligente #Pirelli di serie sulla #McLaren #Artura https://video.repubblica.it/motori/la-gomma-intelligente-pirelli-di-serie-sulla-mclaren-artura/376506/377116 via @repubblica

Reply on Twitter 1362339020571873281Retweet on Twitter 1362339020571873281Like on Twitter 1362339020571873281Twitter 1362339020571873281
lupascaoʇɹ3qןɐ@lupasca·
15 Feb

#iOS - Fake #GPS position: how to use and hot to prevent https://www.albertopasca.it/whiletrue/ios-fake-gps-position-how-to-use-and-prevent/

Reply on Twitter 1361238931828838404Retweet on Twitter 1361238931828838404Like on Twitter 1361238931828838404Twitter 1361238931828838404
lupascaoʇɹ3qןɐ@lupasca·
15 Feb

#iOS - Fake #GPS position: how to use and how to prevent

Reply on Twitter 1361238921443762189Retweet on Twitter 1361238921443762189Like on Twitter 1361238921443762189Twitter 1361238921443762189
lupascaoʇɹ3qןɐ@lupasca·
9 Feb

#CandyCrush - Play without limitations https://www.albertopasca.it/whiletrue/candy-crush-play-without-limitations/

Reply on Twitter 1359096780089098246Retweet on Twitter 1359096780089098246Like on Twitter 1359096780089098246Twitter 1359096780089098246
Load More...

Recent Posts

  • iOS – Fake GPS position: how to use and prevent 15 February 2021
  • Candy Crush – Play without limitations 9 February 2021
  • Unity3D – Snap object in editor grid 3 January 2021
  • Swift – UIView gradient extension 7 December 2020
  • iOS – Data Leakage: App background cache 16 November 2020
  • Python – Draw “complex” shapes in Photoshop 12 November 2020

Archives

Proudly powered by WordPress
loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.
%d bloggers like this: