c++-cli

Call C++ functions from C#/.NET

爱⌒轻易说出口 提交于 2019-12-05 02:33:39
问题 I have a solution which has a C++ project and a C# project. The C++ project defines a class, which I want to instantiate in C# and call its member functions. So far I managed to instantiate the class: CFoo Bar = new CFoo(); But when I try to call a function on it, the compiler says, it is not available. Also, when I inspect the object in the debugger, no members are shown. What am I missing here? 回答1: You need to declare the class in C++/CLI as a ref class . (Note that we're talking about C++

auto_ptr or shared_ptr equivalent in managed C++/CLI classes

孤街醉人 提交于 2019-12-05 02:06:17
In C++/CLI , you can use native types in a managed class by it is not allowed to hold a member of a native class in a managed class : you need to use pointers in that case. Here is an example : class NativeClass { .... }; public ref class ManagedClass { private: NativeClass mNativeClass; // Not allowed ! NativeClass * mNativeClass; // OK auto_ptr<NativeClass> mNativeClass; //Not allowed ! boost::shared_ptr<NativeClass> mNativeClass; //Not allowed ! }; Does anyone know of an equivalent of shared_ptr in the C++/CLI world? Edit: Thanks for your suggestion, "1800-Information". Following your

LNK2022 (Duplicate managed types have different visibilities) on MSVS 2012

喜欢而已 提交于 2019-12-05 01:37:03
I'm porting a solution from MSVS2005 to MSVS2012. The projects are in C++ .NET but use homemade native C++ libraires too. We had no problem building the projects with 2005 but now, I'm unable to build a project using 2012. I get the following error message: MyFile.obj : error LNK2022: metadata operation failed (801311E4) : Duplicate managed types have different visibilities. What does this mean? What info do you need to help me? Thanks for your help? I found the bug. It is a mix of everything that has been suggested here. Somewhere in the project, a native C++ header file is included. A class

How to check an object's type in C++/CLI?

僤鯓⒐⒋嵵緔 提交于 2019-12-05 00:13:00
Is there a simple way to check the type of an object? I need something along the following lines: MyObject^ mo = gcnew MyObject(); Object^ o = mo; if( o->GetType() == MyObject ) { // Do somethine with the object } else { // Try something else } At the moment I'm using nested try-catch blocks looking for System::InvalidCastException s which feels ugly but works. I was going to try and profile something like the code above to see if it's any faster/slower/readable but can't work out the syntax to even try. In case anyone's wondering, this comes from having a single queue entering a thread which

C++/CLI enum not showing up in C# with reference to C++/CLI project

。_饼干妹妹 提交于 2019-12-05 00:12:40
I can't get the contents of an C++/CLI enum to show up in a C# project. I can see inside a class I wrote, and even see the enum, but I can't see the enum values. So I can't use the thing on my C# side. Any reason why contents of a C++/CLI enum wouldn't show up. My C++/CLI project is compiled as mixed to reuse a C++ library. And I'm using Visual Studio 2005. Make sure you used enum class to create a .NET-compatible enumeration, and not a native C++ enum type. Later versions of Visual C++ require public enum class , to disambiguate from the enum class contextual keyword which is part of C++11

C++/CLI class wrapper for c library - callbacks

匆匆过客 提交于 2019-12-04 22:02:09
问题 I am wrapping a C library using C++/CLI. The C library was designed to be used from an unmanaged C++ class. This means that the library functions accept a C++ object pointer and then provide that pointer back in callbacks. This enables the callback code to redirect requests to an appropriate event function in the calling C++ object. The actual functions are quite involved, so I have simplified the problem space to just a few basic items: // C library function signature void CLibFunc

Connecting c++ and c# code with a c++/cli bridge

半腔热情 提交于 2019-12-04 21:59:44
I have a client application in native c++ code which is using native c++ dlls. I am investigating the possibility of connecting this code with c# dlls as they would be much easier to write. I decided to write a c++/cli bridge dll which can be loaded with LoadLibrary and which would pass the calls to c# dll. The communication between the client and the dll is such that the client passes a pointer to an interface object through which the dll then communicates with the client. I wrapped this object in the c++/cli bridge code for the c# code to use it. The bridge should also expose several

Why does C++ need language modifications to be “managed”?

此生再无相见时 提交于 2019-12-04 20:26:10
问题 Why can't a compiler be written that manages what needs to be managed in C++ code (i.e. to make it "CLR compatible")? Maybe with some compromise, like prohibiting void pointers in some situations etc. But all these extra keywords etc. What's the problem that has to be solved by these additions? I have my thoughts about some aspects and what might be hard to solve, but a good solid explanation would be highly appreciated! 回答1: I'd have to disagree with the answers so far. The main problem to

C# to C++ process with WM_COPYDATA passing struct with strings

倖福魔咒の 提交于 2019-12-04 18:39:30
问题 From a c# program I want to use WM_COPYDATA with SendMessage to communicate with a legacy c++/cli MFC application. I want to pass a managed struct containing string objects. I can find the handle to the c++ application for use with SendMessage fine. The bit I don't know about is how the struct and it's strings can be marshalled and read at the other end. Especially as it contains non-blittables. Do people think this is feasible? I'll continue to work on it, but would apprecite someone who's

Visual Studio 2012 winform designer is VERY slow

主宰稳场 提交于 2019-12-04 18:23:58
问题 We've recently migrated one of our Winforms projects to Visual Studio 2012 from Visual Studio 2008. The transition has went remarkably smoothly and everything builds just fine, however we're now struggling with the winforms designer, which is running incredibly slow. To give an example, if we open a small form (the form contains two text boxes, a numeric updown and two buttons - all standard built-in controls, nothing 3rd party), it will take approximately 40-45 seconds in 2012, however on