1

I'm trying to use a class from a C++ unmanaged DLL that I coded in Borland C++ Builder. Then I want to use this code in a C#.

I use P/Invoke to import functions from the dll then I'm trying to wrap it in a C# managed DLL. I have some overload errors and invalid arguments. This probably comes from my wrong way to use "ref" instead of "pointer" but I don't know how to adjust it. I know that I'm probably doing it wrong but I don't know what to do know, plz try to direct me.

C++ unmanaged DLL:

Test.cpp

#include <basepch.h>

#pragma hdrstop

#include "Test.h"

//-------------------

#pragma package(smart_init)

__fastcall TTest::TTest()
{
        //rien
}

__fastcall TTest::~TTest()
{
        //rien
}

void __fastcall TTest::setNombre(int nbr)
{
        nombre = nbr;
}

int __fastcall TTest::getNombre()
{
        return nombre;
}

Test.h

    //---------------------------------------------------------------------------

#ifndef TestH
#define TestH

#include <SysUtils.hpp>
#include <Classes.hpp>

#include <string.h>
#include <stdio.h>
#include <StrUtils.hpp>
#include <time.h>

//---------------------------------------------------------------------------

class PACKAGE TTest
{

private:

       int nombre;

protected:
public:

        __fastcall TTest();
        __fastcall ~TTest();
        void __fastcall setNombre(int nbr);
        int __fastcall getNombre();

};

extern PACKAGE TTest *Test;

//---------------------------------------------------------------------------
#endif

C# managed DLL:

ManagedClasseTest.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ManagedClasseTest
{
    struct TUnmanagedClasseTest
    {
        private ref int nombre;
    [DllImport("ClasseTest.dll",
               EntryPoint="@TTest@$bctr$qqrv",
               CallingConvention=CallingConvention.ThisCall)]
    public static extern void ctor(ref TUnmanagedClasseTest c);

    [DllImport("ClasseTest.dll",
               EntryPoint="@TTest@$bdtr$qqrv",
               CallingConvention=CallingConvention.ThisCall)]
    public static extern void dtor(ref TUnmanagedClasseTest c);

    [DllImport("ClasseTest.dll",
               EntryPoint="@TTest@setNombre$qqri",
               CallingConvention=CallingConvention.ThisCall)]
    public static extern void setNombre(ref TUnmanagedClasseTest c, int nbr);

    [DllImport("ClasseTest.dll",
               EntryPoint="@TTest@getNombre$qqrv",
               CallingConvention=CallingConvention.ThisCall)]
    public static extern int getNombre(ref TUnmanagedClasseTest c);

    public static void Uctor(ref TUnmanagedClasseTest c) {
        ctor(c);
    }
    public static void Udtor(ref TUnmanagedClasseTest c) {
        dtor(c);
    }
    public static void UsetNombre(ref TUnmanagedClasseTest c, int i) {
        nombre = setNombre(c, i);
    }
    public static int UgetNombre(ref TUnmanagedClasseTest c) {
        return getNombre(c);
    }
}


public class ManagedClasseTest
{
    public void ManagedClasseTest()
    {
        Umct = new TUnmanagedClasseTest();
        TUnmanagedClasseTest.Uctor(Umct);
    }

    public void ~ManagedClasseTest()
    {
        TUnmanagedClasseTest.Udtor(Umct);
    }

    public void MsetNombre(int nombre)
    {
        TUnmanagedClasseTest.UsetNombre(Umct, nombre);
    }

    public int MgetNombre()
    {
        return TUnmanagedClasseTest.UgetNombre(Umct);
    }

    private ref TUnmanagedClasseTest Umct;
}
}
EdelMartin
  • 11
  • 1
  • 2
  • 1
    That's not possible. Covered in [this post](http://stackoverflow.com/a/15664100/17034) – Hans Passant Mar 03 '15 at 15:43
  • (1) Try to use IntPtr instead of `ref TUnmanagedClasseTest` (2) Ensure your C++ class has no virtual functions. (3) Learn ComImport: this is a more straightforward solution. – Nikerboker Mar 03 '15 at 19:58

0 Answers0