Skip to content

Instantly share code, notes, and snippets.

@devahmedshendy
Forked from neonichu/DynamicFunctions.swift
Created February 28, 2024 00:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devahmedshendy/09aef6803753747d8190ea30f9628f92 to your computer and use it in GitHub Desktop.
Save devahmedshendy/09aef6803753747d8190ea30f9628f92 to your computer and use it in GitHub Desktop.
Using dlopen / dlsym to call C functions from Swift
import Darwin
let handle = dlopen("/usr/lib/libc.dylib", RTLD_NOW)
let sym = dlsym(handle, "random")
let functionPointer = UnsafeMutablePointer<() -> CLong>(sym)
let result = functionPointer.memory()
println(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment