c++-cli

Best practice for translating exceptions in C++/CLI wrapper class

久未见 提交于 2019-12-10 08:13:42
问题 I am writing a .NET wrapper class for an existing native class which throws exceptions. What are the best practices for translating between native C++ exceptions and Managed exceptions? Catch and re-throw on a one-to-one basis (e.g. std::invalid_argument -> System.System.ArgumentException)? Is there a mapping already drawn up somewhere? 回答1: There is no standard mapping that I know of. What I've done in the past is translate the ones I know about, and a catch block for System.Runtime

Use of ^ operator in visual c++

…衆ロ難τιáo~ 提交于 2019-12-10 03:46:58
问题 I am doing Visual c++ programming, I have created a CLR console application. I have noticed that String arrays should be declared like String ^, not String[]. What is the use of ^? And why is it being used instead of []? And is this substitution limited only to CLR applications? Here is a line of the code and the error: array<String[]>[] abc; the errors generated were error C2143: syntax error : missing ';' before '[' error C2146: syntax error : missing ';' before identifier 'abc' eror C2065:

Array of pointers in C++/CLI MSIL assembly

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-10 03:34:28
问题 I'm trying to wrap some legacy C code for use with C# running on .NET Core. I'm using the approach given here to create a C++ wrapper that compiles to pure MSIL. It's working well for simple functions, but I've found that if my code ever uses pointers-to-pointers or arrays of pointers it will crash with a memory violation. Often it crashes Visual Studio and I have to restart everything, which is tedious. For example, the following code will cause the crashes: public ref class example { public

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

送分小仙女□ 提交于 2019-12-10 02:27:00
问题 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

C++/CLI->C# error C2526: C linkage function cannot return C++ class

百般思念 提交于 2019-12-10 02:26:40
问题 I have a simple .NET dll built with VS2010 C# that exposes 2 static members of a class public class Polygon { public static void Test(int test) {} public static void Test(List<int> test) {} } I then created a Console app from VS2010 C++ and added this function above _tmain extern "C" void TestMe() { Polygon::Test(3); } Adding the reference and compiling gives me this error 1>WierdError.cpp(9): error C2526: 'System::Collections::Generic::List<T>::GetEnumerator' : C linkage function cannot

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

自古美人都是妖i 提交于 2019-12-10 01:55:00
问题 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? 回答1: I found the bug. It is a mix of

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

偶尔善良 提交于 2019-12-10 01:25:51
问题 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

Unreachable breakpoint at execut(able/ing) code

久未见 提交于 2019-12-09 19:40:09
问题 I've got two DLLs, one in written in native C++ and the other in C++/CLI. The former is injected into a process, and at a later point in time, loads the latter. While debugging, I noticed that the native DLL's breakpoints were functioning correctly while the other's weren't, even though its code was being executed. The breakpoints showed this message: This breakpoint will not be hit. No executable code associated with this line. Possible causes include: preprocessor directives or compiler

Why could i get an Unhandled exception Access violation writing in c++/CLI?

烂漫一生 提交于 2019-12-09 19:30:47
问题 I have been struggeling writing a solution excisting out of an c++ win32console and a c++ dll. i finally managed to get them talking without linker errors (so i am assuming both are fully managed c++/CLI projects) but when i run the console i get the following error. Unhandled exception at 0x03f71849 in Company.Pins.Bank.Win32Console.exe: 0xC0000005: Access violation writing location 0x00000001. The console also shows the following Unhandled Exception: System.NullReferenceException: Object

In Visual Studio 2012 what is the difference between Librarian and Linker?

蓝咒 提交于 2019-12-09 16:38:00
问题 I am configuring my Projects to build with x86 and x64 bits, in order to do that I had to change the Target Machine for the different Configurations. I was trying to find where to set the Target Machine for my Native C++ Libraries and I found this post. However I don't have a Linker in the Native C++ Library projects, I have the "Configuration Properties -> Linker -> Advanced -> Target Machine" option in the C++/CLI projects only. In the Native C++ Library projects I have a " Librarian "