LoginSignup
5
5

More than 5 years have passed since last update.

関数のInvokeによるCFunctionPointer使う

Posted at

InvokeFunction.h

#include <stdio.h>

int invoke_fun(int (*f)(void));

#endif /* defined(__LiveCyalume__InvokeFunction__) */

InvokeFunction.c

#include "InvokeFunction.h"


int invoke_fun(int (*f)(void)) {
    return f();
}

{ProjectName}-Bridging-Header.h

int invoke_fun(int (*f)(void));

ViewController.swift


let handle = dlopen("/usr/lib/libc.dylib", RTLD_NOW)
let sym : UnsafeMutablePointer<Void> = dlsym(handle, "random")
let rawPointer = UnsafeMutablePointer<() -> Int32>(sym)
let opaquePtr : COpaquePointer = COpaquePointer(rawPointer)
let cFunPointer  = CFunctionPointer<() -> Int32>(opaquePtr)
println(invoke_fun(cFunPointer))

ただし、RetrunタイプがObjectの場合には使えない。

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5