Reference C++ managed class library from C# class library - c#-4.0

How to reference class library from C++ managed? I can reference C++ managed library from another C++ managed library and use classes from it. But when i reference it from C# library it does not provide classes and namespaces from C++ (object browser show classes as well).
C++ Managed Class library1
namespace ns1{
public ref class Class1{};
}
C++ Managed Class library2
using namespace ns1;
namespace ns2{
public ref class class2{
ns1::Class1 _cl;
};
}
And i can not do the same from C# class library.
VS 2010 can not reference C++ managed library from C# as 'Project reference'.
It is necessary to reference .dll file.

Did not you forget public keyword?
Class definition should look like this:
public ref class MyClass
{
}
to be accessible from another assembly.

Related

C# How to call the delegate method in Consumerbuilder class from Confluent.Kafka library?

C# How to class the delegate method in Consumerbuilder class from Confluent.Kafka library?

passing object from c# to c++

I've been working on a prototype code application that runs in C# and uses classes and functions from older C++ code (in the form of an imported DLL). The code requirement is to pass in a class object to the unmanaged C++ DLL (from C#) and have it be stored/modified for retrieval later by the C# application. Here's the code I have so far...
Simple C++ DLL Class:
EXPORT_DLL int init(MyInitParams *initparams);
C++ DLL Functions:
struct MyInitParams {
public:
int _np;
int _nm;
int type;
double *CV_Weight;}
in c# DLL
[DllImport("NEWUSEMPC", CallingConvention = CallingConvention.Cdecl, EntryPoint = "init")]
public static extern int init(InitParams parameters);
in c# class
class InitParams
{
public int _np;
public int _nm;
public int type;
public double[] CV_Weight;}
If you own the code of the c++ dll it would be a lot more convenient for you to include it in your solution, and create an interop between c# and c++ using managed c++ as a translation layer. Be aware, that the managed c++ layer should only do the translation of data and invoke the native method, and literally nothing else, because managed c++ is designed only as a bridge between the native and managed world.
You can also use mixed debugger to check out what is happening in both managed and unmanaged code in debug to take a look on the variables, so that you can see what's missing.
I personally would discourage the use of platform invoke instead of an interop class, because the latter is a lot cleaner and is easier to maintain later on.

"Activating a single-threaded class from MTA is not supported" error in from C++ WinRT DLL used in C# application

I have a C# program that uses a C++ WinRT DLL. The C# program creates an instance of a public WinRT class Foo which internally tries to instantiate an object of a second WinRT class Bar that isn't declared public. When calling "ref new" on the Bar class, it throws an exception saying "Activating a single-threaded class from MTA is not supported".
How do I configure the Bar class to that it works in a MTA-style threaded application? Is it a per-class or DLL-wide setting?
It's per-class behavior, controlled by attributes ThreadingModel and MarshallingBehavior. See MSDN for details - Threading and Marshaling.
Usage is like this:
using namespace Windows::Foundation::Metadata;
using namespace Platform;
[Threading(ThreadingModel=ThreadingModel::STA]
[MarshalingBehavior(MarshalingType=MarshalingType::None)]
public ref class MySTAClass
{
};

How to call pure virtual functions in dll from exe application?

I have a dll which has an abstract class with all of its member functions are pure virtual functions. I am trying to write an application to call these functions. What are the steps I need to take to call these pure virtual functions?
This is a just a prototype
Header file with abstract class : interface.h [These are the exported functions]
class MathFuncExport {
public:
virtual int Add(int a, int b)=0;
MathFuncExport(){};
virtual ~MathFuncExport(){};
};
Header file in dll : MathFuncDll.h
#include "intf.h"
class MyMathFuncs : public MathFuncExport
{
public:
MyMathFuncs(){};
virtual ~MyMathFuncs(){};
virtual int Add(int a, int b);
};
Implementation : MyMathFunsDll.Cpp file
#include "MathFuncDll.h"
int MyMathFuncs::Add(int a, int b)
{
return a + b;
}
This created a dll but I am not able to call the functions in abstract class or I am missing some link here. Please help me in resolving this issue.
Thanks
You cannot call pure virtual. They are implemented to force function(s) implementation.
Since your base class is in the DLL you have to export class in order to use it for deriving other classes.
The easiest way to export class is to use implicit linking; this way you need a header for the class declaration and import library. For overllok of the different linkages check this link.
For a little demo create Win32 dll. Check MFC support if you need it and check the Export Symbols box.
This will create a dll with sample class and global variable export. Look at the header file where special macro is created, having different meaning for DLL (export) and executable linking with this dll (import).
Once you understand how to use implicit linking, you will be able to derive class from the base class in the dll as if you were using code in the executable module.

Conflict in namespace in C#

If my namespace is
Cytel.GlobalSettings.ChartSetting
and
I have a static class ChartSetting
packed in dll.
then when I use ChartSetting class in other project it shows red color with Chartsetting class's method and public members are not shown
It is interpreting your reference to ChartSetting as a reference to the namespace. You should use the fully qualified name of the ChartSetting static class. If it has an empty namespace, then you will need to use the global qualifier thus:
global::ChartSetting
You can also specify an alias to this class:
using MyAlias = global::ChartSetting;

Resources