Reputation: 18451
I´m trying to build a C++/CLR wrapper to call my C++ code from inside a C# .Net application.
Here are the steps followed:
C++ Project:
cppproject.h
#ifndef _CPPPROJECT_H
#define _CPPPROJECT_H
typedef enum {
SUCCESS,
ERROR
} StatusEnum;
namespace cppproject
{
class MyClass {
public:
MyClass();
virtual ~MyClass();
StatusEnum Test();
};
}
#endif
cppproject.cpp
#include "cppproject.h"
namespace cppproject {
MyClass::MyClass() {};
MyClass::~MyClass() {};
StatusEnum MyClass::Test()
{
return SUCCESS;
}
}
Now the wrapper project (C++/CLR type) to tie together C# and C++:
wrapper.h
// wrapper.h
#pragma once
#include "cppproject.h"
using namespace System;
namespace wrapper {
public ref class Wrapper
{
public:
/*
* The wrapper class
*/
cppproject::MyClass* wrapper;
Wrapper();
~Wrapper();
StatusEnum Test();
};
}
wrapper.cpp
// This is the main DLL file.
#include "stdafx.h"
#include "wrapper.h"
namespace wrapper {
Wrapper::Wrapper()
{
wrapper = new cppproject::MyClass();
}
Wrapper::~Wrapper()
{
delete wrapper;
}
StatusEnum Wrapper::Test()
{
return wrapper->Test();
};
}
And finally the C# code, where I´m getting the error:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using wrapper;
namespace netproject
{
/*
* Enums
*/
public enum StatusEnum {
SUCCESS,
ERROR
};
public partial class netproject
{
public const int MAX_REPORT_DATA_SIZE = 1024;
public wrapper.Wrapper wrapper;
public netproject() { wrapper = new wrapper.Wrapper(); }
~netproject() { wrapper = null; }
public StatusEnum Test()
{
var sts = wrapper.Test(); <<- ERROR
return (netproject.StatusEnum) sts;<<- ERROR
}
}
}
The compiler error at the C# project:
error CS0122: 'wrapper.Wrapper.Test()' is inaccessible due to its protection level
error CS0426: The type name 'StatusEnum' does not exist in the type 'netproject.netproject'
I can´t understand that. Test
is defined public in both the wrapper project and the C++ project. And the StatusEnum
is also public in the C# project above the error line.
Help appreaciated to find out what´s going on here....
Upvotes: 1
Views: 1040
Reputation: 27864
typedef enum {
SUCCESS,
ERROR
} StatusEnum;
This is not something that is accessible in C#. As I see it, you have two options:
1) You can make the enum a managed enum.
public enum class StatusEnum {
SUCCESS,
ERROR
};
2) I'm generally not a fan of enums that only have two values. In many cases, a Boolean will work just as well.
public ref class Wrapper
{
// returns true on success.
bool Test();
};
Upvotes: 1