代码之家  ›  专栏  ›  技术社区  ›  Chechy Levas

将数组从C返回到.Net:我做错了什么?

  •  0
  • Chechy Levas  · 技术社区  · 7 年前

    this question .

    我试图将一个浮点数数组从C返回到.Net。我将包括一些F#代码和C#代码,以便任何一种语言的人都可以回答。

    非托管C代码:

    extern "C"
    {
        __declspec(dllexport) void DisplayHelloFromDLL(c_float* P_x, c_float* 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);
    
            //return the value
            OSQPSolution* sol = work->solution;
            x = sol->x;
    
            // 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, [<In>][<Out>] float[] x)
    
    [<EntryPoint>]
    let main argv = 
        let P_x = [|4.; 1.; 1.; 2.|]
        let mutable xResult:float[] = [|0.;0.|]
        ExternalFunctions.DisplayHelloFromDLL(P_x, xResult);
    
        printfn "This is x:%A" xResult
        0 // return an integer exit code
    

    C#代码

    class Program
        {
            [DllImport("TestLibCpp.dll")]
            public static extern void DisplayHelloFromDLL(double[] i, [In, Out] double[] x);
            static void Main(string[] args)
            {
                Console.WriteLine("This is C# program");
                double[] P_x = new double[] {4.0, 1.0, 1.0, 2.0};
                double[] x = new double[] { 0.0, 0.0};
                DisplayHelloFromDLL(P_x, x);
    
                Console.WriteLine("Finished");
            }
        }
    

    我尝试过其他的变化,比如

    open System.Runtime.InteropServices
    
    module ExternalFunctions =
        [<DllImport("TestLibCpp.dll")>]
        extern void DisplayHelloFromDLL(float[] i, [<In>][<Out>] float[]& x)
    
    [<EntryPoint>]
    let main argv = 
        let P_x = [|4.; 1.; 1.; 2.|]
        let mutable xResult:float[] = [|0.0; 0.0|]
        ExternalFunctions.DisplayHelloFromDLL(P_x, &xResult);
    
        printfn "This is x:%A" xResult
    

    但我怀疑问题在于C代码,而不是.Net代码。我认为问题在于工作->解决方案->x是指针,不是数组。我猜我需要将它从指针转换为数组,但我不知道如何实现。再说一次,我甚至不能完全肯定这是一个问题。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Chechy Levas    7 年前

    感谢佩瑟拉尔为我指明了正确的方向。

            //return the value
            OSQPSolution* sol = work->solution;
            for (int i = 0; i != n; ++i)
                x[i] = sol->x[i];