簡體   English   中英

從C返回數組到.Net:我在做什么錯?

[英]Returning an array from C to .Net: What am I doing wrong?

這是該問題的后續內容。

我試圖將浮點數數組從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);
    }
}

因此,我所做的一切都被聲明為參數“ x”,並在計算結果后對其進行設置。

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");
        }
    }

在F#和C#情況下,x的值均不變。 我嘗試了其他變體,例如

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代碼。 我認為問題是work-> solution-> x是指針,而不是數組。 我猜想我需要將其從指針轉換為數組,但無法解決該問題。 再說一次,也不完全確定這甚至是問題的開始。

感謝PetSerAl向我指出(正確的雙關語)正確的方向。

我要做的就是將C代碼的一部分更改為

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM