Re: Create a dll from MinGW and call it from, .NET/csharp

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: Create a dll from MinGW and call it from, .NET/csharp

Thomas Stover
I'm trying to following along, and get the error:

Unhandled Exception: System.BadImageFormatException: An attempt was made
to load  a program with an incorrect format. (Exception from HRESULT:
0x8007000B) at TestSharp.MainClass.average(Func f, Parameters& p, Double
a, Double b, Int 32 n) at TestSharp.MainClass.Main(String[] args)


What's the command line are you using to build testgeneric.cs?

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: Create a dll from MinGW and call it from, .NET/csharp

Thomas Stover
Well hold up. It now runs with no error, but it doesn't return the same
results as the C program.

c:\Users\...\interop_test>csc /nologo /platform:x86 testgeneric.cs

c:\Users\...\interop_test>testgeneric.exe
Average in [0,2] (10 points): -1.93086419753086

And for some better discussion, the mingw-win64 built dll works like this:

c:\Users\...\interop_test>csc /nologo testgeneric.cs

c:\Users\...\interop_test>testgeneric.exe
Average in [0,2] (10 points): -3.59259259259259

Which is the SAME result I get with the C program. I suspect a calling
convention issue.

c:\Users\...\interop_test>testgeneric-native.exe
Average in [0,2] (10 points): -3.592593

(Sorry if these attachments end up being a mess)



#include <stdio.h>
#include "generic.h"
 
struct Par3
{
 double a, b, c;
};
 
double quadratic (double x, void *par)
{
 double *p;
 p = par;
 return (*p * x + *(p+1)) * x + *(p+2);
}

int main (void)
{
 struct Par3 params = {1.0, 0.0, -5.0};

 printf("Average in [0,2] (10 points): %f\n",
        average(quadratic, &params, 0.0, 2.0, 10));

 return 0;
}


using System;
using System.Runtime.InteropServices;
 
namespace TestSharp
{
 class MainClass
 {
  [StructLayout (LayoutKind.Sequential)]
   struct Parameters { public double a, b, c; }
 
   delegate double Func (double x, ref Parameters p);
 
   [DllImport ("generic.dll")] static extern
   double average (Func f, ref Parameters p, double a, double b, int n);
 
   static double Quadratic (double x, ref Parameters p)
   { return (p.a*x + p.b)*x + p.c;}
 
   public static void Main(string[] args)
   {
     Parameters par;
     Func func;
     double avg;
 
     par = new Parameters();
     { par.a = 1;  par.b = 0;  par.c=-5;}
     func = new Func (Quadratic);
     avg = average (func, ref par, 0.0, 2.0, 10);
     Console.WriteLine("Average in [0,2] (10 points): " + avg.ToString() +"\r\n");
     Console.WriteLine("RETURN to terminate ...");
     Console.ReadLine();
   }
 }
}

#include "generic.h"

double average (double (*f) (double x, void *params),
                void *par, double a, double b, int n)
{
 int i;
 double sum, x, dx;
 
 sum = 0;   x = a;   dx = (b-a) / (n-1);
 for (i=0; i<n; i++)
 {
  sum += f(x,par);
  x += dx;
 }

 return sum / n;
}

#ifndef _generic_h_
#define _generic_h_

#ifdef BUILD_DLL
// the dll exports
#define EXPORT __declspec(dllexport)
#else
// the exe imports
#define EXPORT __declspec(dllimport)
#endif


EXPORT
double average (double (*f) (double x, void *params),
                void *par, double a, double b, int n);

#endif

testgeneric.exe: testgeneric.o generic.dll
        $(CC) -o testgeneric.exe testgeneric.o -L./ -lgeneric $(DEBUG)
 
testgeneric.o: testgeneric.c
        $(CC) -c testgeneric.c $(DEBUG)
 
generic.dll: generic.o
        $(CC) -shared -o generic.dll -Wl,--out-implib,libgeneric.a generic.o
 
generic.o: generic.h generic.c
        $(CC) -c generic.c -DBUILD_DLL -o generic.o $(DEBUG)



-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
MinGW-users mailing list
[hidden email]

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users