I know how to instatiate the C++ classes that are in that DLL, here is a sample code:
Expand|Select|Wrap|Line Numbers
- //Sample.h
- public class __declspec(dllexport) Sample
- {
- public:
- Sample(int Value);
- virtual ~Sample();
- int GetValue();
- CString & AsString();
- private:
- int m_iValue;
- CString m_sValue;
- };
- //---------------------------------------------------------------
- //Sample.cpp
- Sample::Sample(int Value)
- {
- m_iValue = Value;
- m_sValue.Format("AsString: %i", m_iValue);
- }
- Sample::~Sample()
- {
- }
- int Sample::GetValue()
- {
- return m_iValue;
- }
- CString & Sample::AsString()
- {
- return m_sValue;
- }
- //-----------------------------------------------------------------
- //Form1.cs
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- LoadSample();
- }
- public void LoadOldMFC()
- {
- Sample s = new Sample(450);
- textBox1.AppendText(s.GetValue().ToString() + "\r\n");
- textBox1.AppendText(s.AsString() + "\r\n");
- }
- }
- [StructLayout(LayoutKind.Sequential, Pack = 4)]
- public unsafe struct __Sample
- {
- public IntPtr* _vtable;
- public int m_Value;
- }
- public unsafe class Sample : IDisposable
- {
- private __Sample* _cpp;
- [DllImport("OldMFC.dll", EntryPoint = "??0Sample@OldMFC@@QAE@H@Z", CallingConvention = CallingConvention.ThisCall)]
- private static extern int _Sample_Constructor(__Sample* ths, int Value);
- [DllImport("OldMFC.dll", EntryPoint = "??1Sample@OldMFC@@UAE@XZ", CallingConvention = CallingConvention.ThisCall)]
- private static extern void _Sample_Destructor(__Sample* ths);
- [DllImport("OldMFC.dll", EntryPoint = "?GetValue@Sample@OldMFC@@QAEHXZ", CallingConvention = CallingConvention.ThisCall)]
- private static extern int _Sample_GetValue(__Sample* ths);
- [DllImport("OldMFC.dll", EntryPoint = "?AsString@Sample@OldMFC@@QAEAAV?$CStringT@DV?$StrTraitMFC_DLL@DV?$ChTraitsCRT@D@ATL@@@@@ATL@@XZ", CallingConvention = CallingConvention.ThisCall)]
- private static extern string _Sample_AsString(__Sample* ths);
- public Sample(int Value)
- {
- _cpp = (__Sample*)Memory.Alloc(sizeof__Sample));
- _Sample_Constructor(_cpp, Value);
- }
- public void Dispose()
- {
- _Sample_Destructor(_cpp);
- Memory.Free(_cpp);
- }
- public int GetValue()
- {
- return _Sample_GetValue(_cpp);
- }
- public string AsString()
- {
- return _Sample_AsString(_cpp);
- }
- }
I have no problem on instantiating the class or calling function GetValue, but when I call function AsString, I only get garbage.
Someone knows how can I have this working?Alex G.
Is there something I'm missing?
Is there some way to "cast" MFC class CString to System.String?