1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
using System;
using System.Runtime.InteropServices;
namespace Essai.Calcul
{
/// Appel d'un fonction de résolution d'un fonction en C
public class CallC
{
// fonction à appeller
public delegate void CallBack(Int m, Int meq, Int n, IntPtr x, IntPtr f, IntPtr g);
// Cette fonction appelle plusieurs fois la fonction delegate.
[DllImport("c:\\math.dll", EntryPoint = "resolution")]
public static extern IntPtr Sq(CallBack c, Int m, Int meq, Int n, IntPtr xlb, IntPtr xub);
// Corps de la fonction à appeler
// En c le prototype de la fonction la fonction est le suivante :
// void fcn(int m, int meq, int n, double [] x, double *f, double [] g)
// le tableau x est en entrée. Le tableau g est ne sortie.
// et f est aussi en sortie.
public void fcn(int m, int meq, int n, IntPtr x, IntPtr f, IntPtr g)
{
double [] w = new double[2];
double [] ctr = new double[2];
// Récupération des données pour traitement
Marshal.Copy(x, w,0,2);
double tmp1 = w[0] - 2e0;
double tmp2 = w[1] - 1e0;
double [] result = new double[1];
result[0] = tmp1 * tmp1 + tmp2 * tmp2;
ctr[0] = w[0] - 2e0*w[1] + 1e0;
ctr[1] = -(w[0] * w[0]) / 4e0 - w[1] * w[1] + 1e0;
// Préparation des résultats à fournir à la dll en C.
Marshal.Copy(ctr, 0, g, 2);
Marshal.Copy(result, 0,f, 1);
}
// Méthode de lancement
public unsafe void launch()
{
w =new double[2];
ctr = new double[2];
double[] xlb = new double[2];
double[] xub = new double[2];
xlb[0] = -1e-6;
xlb[1] = -1e-6;
xub[0] = 1e-6;
xub[1] = 1e-6;
CallBack myCallBack = new CallBack(fcn);
// Copie des tableaux de données
IntPtr lb = Marshal.AllocHGlobal(Marshal.SizeOf(xlb[0])*2);
IntPtr ub = Marshal.AllocHGlobal(Marshal.SizeOf(xub[0])*2);
Marshal.Copy(xlb, 0, lb, 2);
Marshal.Copy(xub, 0, ub, 2);
IntPtr k = Sq(fonc, 2, 1, 2, 0, lb, ub, 0);
double[] res = new double[2];
Marshal.Copy(k, res, 0, 2);
Console.Out.WriteLine(res[0]);
Console.Out.WriteLine(res[1]);
}
}
} |
Partager