ffi使用

2023-05-16

编译程序时,发现测试用例过不去,抽象出其中的测试用例。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <ffi.h>
#include <glib.h>
#include <glib-object.h>
#include <glib/gprintf.h>
#include <girepository.h>
#include <gmodule.h>

typedef enum _cairo_status{
 CAIRO_STATUS_SUCCESS = 0,

 CAIRO_STATUS_NO_MEMORY,
 CAIRO_STATUS_INVALID_RESTORE,
 CAIRO_STATUS_INVALID_POP_GROUP
} cairo_status_t;

cairo_status_t test_func(int m, int n)
{
    return CAIRO_STATUS_SUCCESS;
}

void test_ffi_call (void) {

    GIArgument retval;
    void* func_addr = &test_func;
    int nargs = 2;

    ffi_type **ffi_arg_types = alloca(sizeof(ffi_type *) *nargs);
    ffi_arg_types[0] = &ffi_type_sint;
    ffi_arg_types[1] = &ffi_type_sint;

    void **ffi_args = alloca(sizeof(void *) *nargs);
    void *ffi_arg_ptr = alloca(ffi_arg_types[0]->size);
    int *arg1 = ffi_arg_ptr;
    *arg1 = 4;
    ffi_args[0] = ffi_arg_ptr;

    void *ffi_arg_ptr2 = alloca(ffi_arg_types[1]->size);
    int *arg2 = ffi_arg_ptr2;
    *arg2 = 5;
    ffi_args[1] = ffi_arg_ptr2;
    
    ffi_cif cif;
    ffi_type *ffi_ret_type = &ffi_type_sint32;
    //ffi_prep_cif(ffi_cif *cif, ffi_abi abi, unsigned int nargs, ffi_type *rtype, ffi_type **atypes);
    ffi_status status = ffi_prep_cif(&cif, FFI_DEFAULT_ABI, (unsigned int)nargs, ffi_ret_type, ffi_arg_types);

    if (status == FFI_OK) {
        union { GIArgument arg; ffi_sarg i; } *uu = (gpointer)&retval;
        printf("0--->%lx, %p.\n", uu->i, &retval);

        uu->i = 0x12cf702d0;
        uu = (gpointer)&retval;
        printf("1--->%lx, %p.\n", uu->i, &retval);
        //ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue);
        ffi_call(&cif, func_addr, &retval, ffi_args);
        uu = (gpointer)&retval;
        printf("2--->%lx, %p.\n", uu->i, &retval);
    }
}

int main (void)
{
        test_ffi_call();
        return 0;
}
                                             

编译:

~$ gcc test_ffi.c -o test -fpermissive -w -lffi -I/usr/include/gobject-introspection-1.0/ -I/usr/include/glib-2.0/ -I/usr/lib/loongarch64-linux-gnu/glib-2.0/include/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ffi使用 的相关文章

随机推荐

  • 十一、 Debian忘记密码重置

    其方式是在GRUB引导菜单下按 e 进入编辑模式直接修改用户密码 重启VPS xff0c 可以在面板重启也可以在VNC上面使用发送 CTRL 43 ALT 43 DEL 按钮直接重启 xff0c 在图示处按 e 键 xff08 若出现BIO
  • 加入共享宽带,让你的闲置宽带循环利用再变现

    共享经济是近些年来发展的一个热点名词 xff0c 因此大家也会看到一些非常多的共享产品出现在市面上 比如说大家熟悉的共享单车 xff0c 共享汽车共享充电宝等等 xff0c 但是不知道大家有没有听说过共享宽带呢 xff1f 宽带几乎是家家户
  • 一招让NAS自给自足

    网络带来了许多便利 xff0c 但又给生活带来了很多烦恼 xff0c 比如微信文档总是过期 xff0c 关键内容经常找不到 xff0c 照片备份太散乱 最近听朋友说听说前任离婚了 xff0c 我突然想重温下与她昨日的温情 xff0c 可是翻
  • 百度网盘撸用户羊毛是怎么一回事

    最近百度网盘事件闹得沸沸扬扬 xff0c 很多吃瓜小伙伴对这次事件的来龙去买不太清楚 xff0c 今天就给大家八一八百度网盘如何反撸用户引发众怒 百度对于该计划的说明 xff1a 用户参加该计划可贡献闲置网络带宽和电脑存储空间给百度 xff
  • 业务流程节点信息提示

    xfeff xfeff 该模块中主要是为了明确用户操作 让用户具体的知道该进行哪一步操作 xff0c 在登陆系统后 xff0c 系统首页中会有下面类似的流程图 xff1a 当用户完成一项操作后 xff0c 要根据流程提示其他用户进行下一步操
  • UbuntuWSL操作PA的BUG记录——AM_HOME环境变量的设定

    2021年5月更 xff0c 发现WSL2是真的香 xff0c 下次还用 x1f604 2021年4月 血亏 xff0c 建议老实用虚拟机做 xff0c WSL还是有很多未完善的地方 xff0c 不适合新手瞎折腾 问题描述 xff1a 当使
  • windows11编译OpenCV4.5.0 with CUDA(附注意事项)

    windows11编译OpenCV4 5 0 with CUDA 从OpenCV4 2 0 版本开始允许使用 Nvidia GPU 来加速推理 本文介绍最近使用windows11系统编译带CUDA的OpenCV4 5 0的过程 文中使用 特
  • OpenCV—矩阵数据类型转换cv::convertTo

    OpenCV 矩阵数据类型转换cv convertTo 函数 void convertTo OutputArray m int rtype double alpha 61 1 double beta 61 0 const 参数 m 目标矩阵
  • Mysql LIMIT使用

    原文出处 xff1a http www jb51 net article 62851 htm Mysql中limit的用法 xff1a 在我们使用查询语句的时候 xff0c 经常要返回前几条或者中间某几行数据 xff0c 这个时候怎么办呢
  • cookies的理解与chrome查看cookie

    Cookies是一种能够让网站服务器把少量数据储存到客户端的硬盘或内存 xff0c 或是从客户端的硬盘读取数据的一种技术 Cookies是当你浏览某网站时 xff0c 由Web服务器置于你硬盘上的一个非常小的文本文件 xff0c 它可以记录
  • 【Qt】Qt多线程开发—实现多线程设计的四种方法

    Qt 使用Qt实现多线程设计的四种方法 文章目录 Qt 使用Qt实现多线程设计的四种方法一 写在前面二 方法一 QThread xff1a 带有可选事件循环的底层API三 方法二 QThreadPool和QRunnable xff1a 重用
  • OpenStack Designate系统架构分析

    前言 OpenStack提供了云计算数据中心所必不可少的用户认证和授权 计算 存储 网络等功能 xff0c 网上已经有不少的文章介绍这些功能的配置 架构分析以及代码详解 但是 针对OpenStack Designate所提供的DNSaaS服
  • 【Qt】Qt线程同步之QWaitCondition

    Qt 线程同步之QWaitCondition 文章目录 Qt 线程同步之QWaitCondition一 简介二 成员函数API xff08 2 1 xff09 等待 wait xff08 2 2 xff09 唤醒一个线程 xff08 2 3
  • Linux下如何用命令连接有线网络

    因为进入tty命令界面 xff0c 想要下东西 xff0c 需要用命令连接有线网络直接输入命令 sudo dhclient eth0
  • Debian下制作deb包

    1 安装相应的编译工具 apt get install dh make dpkg dev debhelper fakeroot build essential Docker中执行dh make出现如下错误 xff1a Cannot get
  • electron在龙芯平台上本地安装使用和打包(二)

    已经打包好的适合在mips平台运行的electron quick start xff0c 点击下载 1 安装electron前的准备 从http www loongnix org index php Electron下载所需软件包 xff0
  • mips版本electron在龙芯平台上的安装

    本文主要讲述如何安装mips版本的electron xff0c 和上两篇不同的是 xff0c 此安装方法只需要用户修改一行代码即可完成 mips架构用户只需要关注本文章的3 2章节 目前用户可以通过此方法安装mips版本的4 1 3 xff
  • 制作PyPI包

    1 环境 本文介绍使用setup py生成pip可以安装的python包以及使用git信息自动生成软件包版本 1 1 python pip版本 python3 version Python 3 7 3 pip 18 1 from usr l
  • python3 对字典去重

    对于一个列表中的多个字典进行去重 1 对key去重 将相同的key合并到一个字典中 2 对元素去重 将一个字典的重复元素去重 代码如下 xff1a span class token comment initializing list spa
  • ffi使用

    编译程序时 xff0c 发现测试用例过不去 xff0c 抽象出其中的测试用例 span class token macro property span class token directive hash span span class t