Well for anyone who is interested I've worked one solution out (not to say it's the best solution though)
MyDll.h:
Code:
extern "C"{
class __declspec(dllexport) MyClass{
public:
MyClass( long a, long b );
long getNumA();
long getNumB();
long getAvg();
private:
long numA, numB;
};
__declspec(dllexport) class MyClass* getMyClassRef( long a, long b );
__declspec(dllexport) long getNumberA( class MyClass* refToMyClass );
__declspec(dllexport) long getNumberB( class MyClass* refToMyClass );
__declspec(dllexport) long getAverage( class MyClass* refToMyClass );
}
MyDll.cpp
Code:
#include "stdafx.h"
#include "MyDll.h"
extern "C"{
MyClass::MyClass( long a, long b )
: numA(a_, numB(b){}
long MyClass::getNumA(){
return numA;
}
long MyClass::getNumB(){
return numB;
}
long MyClass::getAvg(){
return (numA+numB)/2;
}
__declspec(dllexport) class MyClass* getMyClassRef( long a, long b ){
MyClass* temp = new MyClass( a, b );
return temp;
}
__declspec(dllexport) long getNumberA( class MyClass* refToMyClass ){
return refToMyClass->getNumA();
}
__declspec(dllexport) long getNumberB( class MyClass* refToMyClass ){
return refToMyClass->getNumB();
}
__declspec(dllexport) long getAverage( class MyClass* refToMyClass ){
return refToMyClass->getAvg();
}
}
Default.aspx.cs:
Code:
using ...
using System.Runtime.InteropServices;
namespace SampleWebApp{
public partial class _Default : System.Web.UI.Page{
[StructLayout(LayoutKind.Sequential)]
class MyWrapperClass{
Int32 a, b;
[DllImport("MyDll.dll")]
public static extern MyWrapperClass getMyClassRef( Int32 x, Int32 y );
[DllImport("MyDll.dll")]
public static extern Int32 getNumberA( MyWrapperClass mwc );
[DllImport("MyDll.dll")]
public static extern Int32 getNumberB( MyWrapperClass mwc );
[DllImport("MyDll.dll")]
public static extern Int32 getAverage( MyWrapperClass mwc );
};
protected void Page_Load(object sender, EventArgs e){
MyWrapperClass m = MyWrapperClass.getMyClassRef( 50, 100 );
textArea1.InnerHtml = MyWrapperClass.getNumberA( m ).ToString();
textArea2.InnerHtml = MyWrapperClass.getNumberB( m ).ToString();
textArea3.InnerHtml = MyWrapperClass.getAverage( m ).ToString();
}
}
}
As far as I can tell, there is no way to instantiate an object declared within the DLL ( I assume because of CLR typing - but if you know differently, I'd really really like to know how you do it!)
So I basically had to write wrappers within the DLL to achieve this effect. It certainly doesn't seem perfect to me, so please somebody show me more or give me some pointers, I really need to know everything I can about marshalling data using P/Invoke.
Also, I read through this little gem:
Interop with Native Libraries - Mono
^an excellent site with A LOT of info.
Thank You