2

I'm try to follow this steps: Calling C# from C++, Reverse P/Invoke, Mixed Mode DLLs and C++/CLI 1. I make C# Dll named TestLib:

namespace TestLib
{
    public class TestClass
    {
        public float Add(float a, float b)
        {
            return a + b;
        }
    }
}

2. Then I create C++/CLI Dll named WrapperLib and add reference to C# TestLib.

// WrapperLib.h

#pragma once

using namespace System;
using namespace TestLib;

namespace WrapperLib {

    public class WrapperClass
    {
    float Add(float a, float b)
        {
        TestClass^ pInstance = gcnew TestClass();
        //pInstance
        // TODO: Add your methods for this class here.
        return pInstance->Add(a, b);
        }
    };
}

C+ 3. For check tis example I've create C++/CLI console application and try to call this code:

// ConsoleTest.cpp : main project file.

#include "stdafx.h"

using namespace System;
using namespace WrapperLib;

int main(array<System::String ^> ^args)
{
    Console::WriteLine(L"Hello World");
    WrapperClass cl1 = new WrapperClass();

    return 0;
}

But I get a few errors:

error C2065: 'WrapperClass' : undeclared identifier C:\Projects\TestSolution\ConsoleTest\ConsoleTest.cpp    11  1   ConsoleTest
error C2146: syntax error : missing ';' before identifier 'cl1' C:\Projects\TestSolution\ConsoleTest\ConsoleTest.cpp    11  1   ConsoleTest
error C2065: 'cl1' : undeclared identifier  C:\Projects\TestSolution\ConsoleTest\ConsoleTest.cpp    11  1   ConsoleTest
error C2061: syntax error : identifier 'WrapperClass'   C:\Projects\TestSolution\ConsoleTest\ConsoleTest.cpp    11  1   ConsoleTest

Well I know somewhere I missed, but where?

Community
  • 1
  • 1
Superjet100
  • 129
  • 1
  • 11

2 Answers2

2

According to @Ben Voigt suggestion I believe that your code should look somewhat like this:

// ConsoleTest.cpp : main project file.

#include "stdafx.h"
#include "WrapperLib.h"

using namespace System;
using namespace WrapperLib;

int main(array<System::String ^> ^args)
{
    float result;
    Console::WriteLine(L"Hello World");
    WrapperClass cl1;

    result = cl1.Add(1, 1);

    return 0;
}

If you don't include the header file of your wrapper library, the C++ compiler will never find its functions and you will keep getting the errors that you displayed earlier.

Felipe
  • 6,312
  • 11
  • 52
  • 70
  • My WrapperLib project contain WrapperLib.h with code and WrapperLib.cpp only with: #include "stdafx.h" #include "WrapperLib.h" Is it right? – Superjet100 Jun 20 '12 at 14:40
  • Yes I believe that is correct, however you need to #include your WrapperLib header file into your main project (the one you are using to test it). As @Mark Simith said above, in native C++ it doesn't matter if you did or did not reference another project, you still need to include the header files. – Felipe Jun 20 '12 at 14:50
1

That's not good C++, looks like Java or C#.

The correct syntax to create a new object in C++/CLI is

WrapperClass cl1;

or

WrapperClass^ cl1 = gcnew WrapperClass();

C++ has stack semantics, you have to tell the compiler whether you want a local object that is automatically disposed at the end of the function (first option), or a handle that can live longer (second option, using ^ and gcnew).

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • I try with WrapperClass^ cl1 = gcnew WrapperClass(); but I get errors: Error 1 error C2065: 'WrapperClass' : undeclared identifier error C2065: 'cl1' : undeclared identifier error C2061: syntax error : identifier 'WrapperClass' – Superjet100 Jun 20 '12 at 13:44
  • @Superjet100: You also need `#include "WrapperLib.h"` – Ben Voigt Jun 20 '12 at 13:46
  • But in this test console aplication I don't have #include "WrapperLib.h" – Superjet100 Jun 20 '12 at 13:57
  • 1
    @Superjet100: Oh, it's a separate project? Then add a reference to the project where `WrapperLib::WrapperClass` is defined. – Ben Voigt Jun 20 '12 at 14:20
  • Yes, I've added reference in console C++/CLI application to C++/CLI wrapper library. – Superjet100 Jun 20 '12 at 14:32
  • 1
    For native classes from another dll (like WrapperClass), to make them visible in your translation unit you need to #include their header file (just like standard C++ code). For managed classes from another dll, you only need to add a reference to the project (just like in C#). – Matt Smith Jun 20 '12 at 14:40
  • @Superjet: Is `WrapperLib.h` actually getting compiled in the library project? Introduce a syntax error, see if the compiler notices. – Ben Voigt Jun 20 '12 at 14:42
  • Where I must put code realization? In CPP file? And what must contain H file? – Superjet100 Jun 20 '12 at 14:59