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?

2
  • If the compiler told you "hey, got a few errors, please fix" where would you look first? :) Please tell us what the errors are. Commented Jun 20, 2012 at 13:32
  • Fixed. I've add VS output. How to right call this function from native C++ or C? Commented Jun 20, 2012 at 13:39

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

My WrapperLib project contain WrapperLib.h with code and WrapperLib.cpp only with: #include "stdafx.h" #include "WrapperLib.h" Is it right?
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.
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).

8 Comments

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: You also need #include "WrapperLib.h"
But in this test console aplication I don't have #include "WrapperLib.h"
@Superjet100: Oh, it's a separate project? Then add a reference to the project where WrapperLib::WrapperClass is defined.
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#).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.