c - 如何将数组从 .Net 传递到 C?

标签 c arrays f# interop

我正在尝试使用 open source library, written in C ,通过包装它并将其公开给 .Net。无论如何,我都不是 C 专家。

到目前为止,我已经成功地从 F# 调用了用 C 编写的演示代码。我通过关注 this guide 做到了这一点然后填空。我已经调整了 C 代码以期望将 int 传递给它,因此我至少可以将标量值从 F# 获取到 C。但是没有返回值。

但是使用数组要复杂得多。

我的C代码是

extern "C"
{
    __declspec(dllexport) void DisplayHelloFromDLL(int i)
    {
        //printf("Hello from DLL !\n");
        cout << "You gave me ... an int: " << i << endl;

        // Load problem data
        c_float P_x[4] = { 4., 1., 1., 2., }; //covariance matrix
        c_int   P_nnz = 4; //number of non-zero elements in covar
        c_int   P_i[4] = { 0, 1, 0, 1, }; //row indices?
        c_int   P_p[3] = { 0, 2, 4}; //?
        c_float q[2] = { 1., 1., }; //linear terms
        c_float A_x[4] = { 1., 1., 1., 1., }; //constraint coefficients matrix
        c_int   A_nnz = 4; //number of non zero elements in constraints matrix
        c_int   A_i[4] = { 0, 1, 0, 2, }; //row indices?
        c_int   A_p[3] = { 0, 2, 4}; //?
        c_float l[3] = { 1., 0., 0., }; //lower bounds
        c_float u[3] = { 1., 0.7, 0.7, }; //upper bounds
        c_int n = 2; //number of variables (x)
        c_int m = 3; //number of constraints


        // Problem settings
        OSQPSettings *settings = (OSQPSettings *)c_malloc(sizeof(OSQPSettings));

        // Structures
        OSQPWorkspace *work; // Workspace
        OSQPData *data;      // OSQPData

        // Populate data
        data = (OSQPData *)c_malloc(sizeof(OSQPData));
        data->n = n;
        data->m = m;
        data->P = csc_matrix(data->n, data->n, P_nnz, P_x, P_i, P_p);
        data->q = q;
        data->A = csc_matrix(data->m, data->n, A_nnz, A_x, A_i, A_p);
        data->l = l;
        data->u = u;


        // Define Solver settings as default
        osqp_set_default_settings(settings);

        // Setup workspace
        work = osqp_setup(data, settings);

        // Solve Problem
        osqp_solve(work);

        // Clean workspace
        osqp_cleanup(work);
        c_free(data->A);
        c_free(data->P);
        c_free(data);
        c_free(settings);
    }
}

我的 C# 代码是

class Program
    {
        [DllImport("TestLibCpp.dll")]
        public static extern void DisplayHelloFromDLL(int i);
        static void Main(string[] args)
        {
            Console.WriteLine("This is F# program");
            DisplayHelloFromDLL(52);
        }
    }

为了缩小这个问题的焦点:如何使 P_x 成为从 F# 传入的参数?

最佳答案

在阅读了 AlexF 的链接并开始工作后,我想我应该在这里为任何有类似问题的人发布一个答案。 我可能会弄错一些概念,但代码有效。如果我有任何细节错误,请告诉我,我会进行编辑。

需要注意的概念: Blittable 类型:这些类型在 .Net(托管)和 C(非托管)中具有相同的内部表示。 Blittable 类型由编码器“固定”,这似乎意味着指针从 .Net 传递到 C,而不是被复制的值。 Blittable 类型的内存位置被锁定,直到非托管函数返回。不确定这对垃圾收集有何影响。

In/Out 属性:Blittable 数组作为 In 参数传递。如果您希望将它们用作返回值,则必须将它们显式标记为 Out。

无论如何,这是有效的代码。

非托管:

extern "C"
{
    __declspec(dllexport) void DisplayHelloFromDLL(c_float* P_x)
    {
        //printf("Hello from DLL !\n");
        //cout << "You gave me ... an int: " << i << endl;

        // Load problem data
        //c_float P_x[4] = { 4., 1., 1., 2., }; //covariance matrix
        c_int   P_nnz = 4; //number of non-zero elements in covar
        c_int   P_i[4] = { 0, 1, 0, 1, }; //row indices?
        c_int   P_p[3] = { 0, 2, 4}; //?
        c_float q[2] = { 1., 1., }; //linear terms
        c_float A_x[4] = { 1., 1., 1., 1., }; //constraint coefficients matrix
        c_int   A_nnz = 4; //number of non zero elements in constraints matrix
        c_int   A_i[4] = { 0, 1, 0, 2, }; //row indices?
        c_int   A_p[3] = { 0, 2, 4}; //?
        c_float l[3] = { 1., 0., 0., }; //lower bounds
        c_float u[3] = { 1., 0.7, 0.7, }; //upper bounds
        c_int n = 2; //number of variables (x)
        c_int m = 3; //number of constraints


        // Problem settings
        OSQPSettings *settings = (OSQPSettings *)c_malloc(sizeof(OSQPSettings));

        // Structures
        OSQPWorkspace *work; // Workspace
        OSQPData *data;      // OSQPData

        // Populate data
        data = (OSQPData *)c_malloc(sizeof(OSQPData));
        data->n = n;
        data->m = m;
        data->P = csc_matrix(data->n, data->n, P_nnz, P_x, P_i, P_p);
        data->q = q;
        data->A = csc_matrix(data->m, data->n, A_nnz, A_x, A_i, A_p);
        data->l = l;
        data->u = u;


        // Define Solver settings as default
        osqp_set_default_settings(settings);

        // Setup workspace
        work = osqp_setup(data, settings);

        // Solve Problem
        osqp_solve(work);

        // Clean workspace
        osqp_cleanup(work);
        c_free(data->A);
        c_free(data->P);
        c_free(data);
        c_free(settings);
    }
}

托管 (F#)

open System.Runtime.InteropServices

module ExternalFunctions =
    [<DllImport("TestLibCpp.dll")>]
    extern void DisplayHelloFromDLL(float[] i)

[<EntryPoint>]
let main argv = 
    let P_x = [|4.; 1.; 1.; 2.|]
    ExternalFunctions.DisplayHelloFromDLL(P_x);
    0 // return an integer exit code

关于c - 如何将数组从 .Net 传递到 C?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52354173/

相关文章:

c++ - 用于 MP3、AAC、WAV 的跨平台 (C/C++) 音频库

c - 如何写一个简单的 "page fault generator"?

ios - Swift 将数组数组映射到元组数组 - 'map' 生成 [T],而不是预期的

javascript - 按特定子级对 DOM 元素进行排序(无 JQuery)

F#:如何将一个序列分成一个序列

oop - 我应该编写利用 Intellisense 的代码吗?

C: 为什么我在 free() 上会出错

c - 如何删除随机数序列

c++ - 为什么输出显示 0 而不是 0.0?

f# - F# 中与浮点( double 类型)的模式匹配