Hi,
I've successfully been able to call C++ function into my C# ASP.NET application using P/Invoke. However, calling and using c++ classes from my DLL has been quite troublesome. Here is a small example I'm working with:
Default.aspx.cs:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.InteropServices;
namespace SampleWebApp{
public partial class _Default : System.Web.UI.Page{
[StructLayout(LayoutKind.Sequential)]
class ClassWrapper{
[DllImport("MyDll.dll")]
public static extern Int32 getANumber();
public Int32 getNum(){
return ClassWrapper.getANumber();
}
[DllImport("MyDll.dll")]
public static extern ClassWrapper returnByRef();
};
protected void Page_Load(object sender, EventArgs e){
ClassWrapper c = ClassWrapper.returnByRef(); // seems to work fine
textArea.InnerHtml = c.getNum().ToString(); // Compiler complains no entry point could be found in dll for getANumber()
}
}
}
MyDll.h:
Code:
extern "C"{
class __declspec(dllexport) myClass{
public:
myClass(void);
long getANumber(void);
};
__declspec(dllexport) class myClass* returnByRef();
}
and MyDll.cpp:
Code:
#include "stdafx.h"
#include "MyDll.h"
extern "C"{
__declspec(dllexport) myClass::myClass(){
return;
}
__declspec(dllexport) long myClass::getANumber(){
return 5;
}
__declspec(dllexport) class myClass* returnByRef(){
myClass* temp = new myClass();
return temp;
}
}
Like I said, functions I've had no problems with, but I really can't figure out what to do about classes( how to intantiate and call member functions, etc.)
Any help/guidance you can offer me is most appreciated.
Thank You