> ## Documentation Index
> Fetch the complete documentation index at: https://docs.arkffi.hmbill.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# 函數指針

> 通過 CFunction 使用原始 C 函數指針

部分 C API 期望函數指針作爲參數。arkffi 提供 `CFunction` 來包裝它們。

## 獲取指針

```typescript theme={null}
const handle = ffi.load('libffi_target.so');
const ptr = ffi.getSymbolPtr(handle, 'add');
// ptr 是 C 函數的內存地址
```

## 創建 CFunction 包裝

```typescript theme={null}
import { CFunction, FFIType } from 'arkffi';

const add = CFunction({
  args: [FFIType.double, FFIType.double],
  returns: FFIType.double,
  ptr: ptr,
});

add(2.0, 3.0); // → 5.0
add.close();
```

## 生命週期

完成後始終關閉包裝器：

```typescript theme={null}
const fn = CFunction({ ... });
fn();
fn.close();
```

## 傳遞給 C API

```typescript theme={null}
import { CFunction, FFIType } from 'arkffi';

// C: void sort(int* arr, int (*cmp)(int, int));
const cmp = CFunction({ args: [FFIType.int32, FFIType.int32], returns: FFIType.int32, ptr: cmpPtr });
ffi.callMixed(handle, 'sort', 'ip', 'v', [arrPtr, cmp.ptr], []);
```

## 使用 JSCallback 代替

對於 ArkTS 到 C 的回調，使用 `JSCallback`：

```typescript theme={null}
const cmp = new JSCallback(
  (a: number, b: number): number => a - b,
  { args: [FFIType.int32, FFIType.int32], returns: FFIType.int32 },
);

ffi.callMixed(handle, 'sort', 'ip', 'v', [arrPtr, cmp.ptr], []);
cmp.close();
```
