c++-cli

Callbacks from C++ back to C#

大兔子大兔子 提交于 2019-12-21 05:27:06
问题 say I have a C++ library function for computing PI: // pi.h: #ifdef BUILDING_DLL #define DLL_MACRO __declspec(dllexport) #else #define DLL_MACRO __declspec(dllimport) #endif namespace Cpp { class PI { public: static double DLL_MACRO compute(); }; }; // pi.cpp: #include "pi.h" #include <cmath> double Cpp::PI::compute() { // Leibnitz summation formulae: double sum = 0.0; for(long n = 0; n < 100*1000*1000; n++) sum += 4.0*pow(-1.0, n)/(2*n + 1.0); return sum; } I need to call this function from

Create NuGet package for C++/CLI (mixed) assembly

ε祈祈猫儿з 提交于 2019-12-21 04:21:41
问题 I have created a C++/CLI (mixed) assembly which has a managed wrapper class around some unmanaged C++ code. The managed part targets .NET 4.6.1, I got a file entry.cpp with just this line to do that: [assembly:System::Runtime::Versioning::TargetFrameworkAttribute(L".NETFramework,Version=v4.6.1", FrameworkDisplayName = L".NET Framework 4.6.1")]; When I now manually include the compiled assembly in a .NET 4.6.1 project I can use the managed class as expected. This project can be build four ways

Limitations of using C++/CLI with NUnit

跟風遠走 提交于 2019-12-21 04:14:53
问题 This answer to a question about C++ unit test frameworks suggests a possibility that had not occurred to me before: using C++/CLI and NUnit to create unit tests for native C++ code. We use NUnit for our C# tests, so the possibility of using it for C++ as well seems enticing. I've never used managed C++, so my concern is are there any practical limitations to this approach? Are many of you doing this? If so, what was your experience like? 回答1: We do this all of the time. We have many

LoadLibrary 193

跟風遠走 提交于 2019-12-21 02:13:08
问题 I am creating a C++/CLI dll that will be loaded into a legacy c++ application. The legacy application does this with a traditional call to LoadLibrary. Both the application and the C++/CLI dll are compiled in 64 bit mode. When the LoadLibrary call happens, it fails with error 193. This usually means that some non-64bit component is trying to load. When I look at the dll load output in visual studio 2010, I see the the failure is occurring when mscoree.dll is being loaded (to be exact, I see

Convert a 'System::String ^' to 'const char *' in vc++

北城余情 提交于 2019-12-21 02:00:29
问题 How can I convert a 'System::String ^' to 'const char *' in vc++? My code: String ^Result1= "C:/Users/Dev/Desktop/imag.jpg"; IplImage *img1 = cvLoadImage(Result1, 1); if I do like above it will generate following error. error C2664: 'cvLoadImage' : cannot convert parameter 1 from 'System::String ^' to 'const char *' Please help me. 回答1: It's like this: How to convert from System::String* to Char* in Visual C++ System::String ^ str = "Hello world\n"; //method 1 pin_ptr<const wchar_t> str1 =

How to catch unmanaged C++ exception in managed C++

二次信任 提交于 2019-12-20 18:52:19
问题 I am developing a thin managed C++ wrapper over a large unmanaged C++ library, and a large C# library. I need to catch errors originating in that large unmanaged C++ library, and rethrow them as Clr exceptions. The unmanaged library throws instances of the following class: Error::Error(const std::string& file, long line, const std::string& function, const std::string& message) { message_ = boost::shared_ptr<std::string>(new std::string( format(file, line, function, message))); } const char*

Visual Studio 2010 C++/CLI in Static Library Mode: could not find assembly 'mscorlib.dll'

前提是你 提交于 2019-12-20 12:03:54
问题 I am working on a C++/CLI project with VS 2012 in Dynamic Library (.dll) and x64 mode. If I switch the mode to Static Library, I get the error below. Error 1 error C1107: could not find assembly 'mscorlib.dll': please specify the assembly search path using /AI or by setting the LIBPATH environment variable C:\Depot\Main\Current\Sln\ALibraryProject\Stdafx.cpp 1 1 ALibraryProject I tried removing the reference to the mscorlib.dll then adding it again from: Project > Properties > General >

Type visibilty for a header Share a header file shared between native and managed clients

蹲街弑〆低调 提交于 2019-12-20 07:22:22
问题 I have a header file that is included by both a native cpp file and a managed cpp file(compiled with /clr). It includes only native types, but I want to specify that the native types are visible outside the assembly (see http://msdn.microsoft.com/en-us/library/4dffacbw(VS.80).aspx). Essentially, I want: public class NativeClass // The public makes this visible outside the assembly. { }; If I include this code from a native cpp, I get the following error: error C3381: 'NativeClass' : assembly

Keep the delegate argument names when compiling C++/CLI for .Net

我的未来我决定 提交于 2019-12-20 05:24:16
问题 In C# I can get Visual Studio to keep the delegate's argument names. For example if I have: public delegate void Blah(object myArg); public event Blah Foo; Then when I add a method to the event, Visual Studio UI automatically keeps the names and creates the method: void Form1_Foo(object myArg); But, if I declare a delegate in C++/CLI: public: delegate void Blah(Object^ myArg); event Blah^ Foo; it doesn't keep the names and creates a method with nonsense names: void Form1_Foo(object A_0) How

Difference between array<T> and array<T^> where T is ValueType

与世无争的帅哥 提交于 2019-12-20 05:16:07
问题 I'm writing a C++/CLI wrapper over native lib for my C# project. I'm trying to convert std::vector<unsigned char> in native c++ to System.Byte[] in C#. In C++/CLI both variants are valid auto arr = gcnew array<System::Byte>(10); auto arr = gcnew array<System::Byte^>(10); But in first case in C# code we got System::Byte[] type whereas in second case we got System::ValueType[]. So my question is why we got such strange behavior? 回答1: The ^ hat should only be used on reference types. Byte is a