c++-cli

c++/cli pass (managed) delegate to unmanaged code

风流意气都作罢 提交于 2019-11-26 12:19:23
How do I pass a function pointer from managed C++ (C++/CLI) to an unmanaged method? I read a few articles, like this one from MSDN , but it describes two different assemblies, while I want only one. Here is my code: 1) Header (MyInterop.ManagedCppLib.h): #pragma once using namespace System; namespace MyInterop { namespace ManagedCppLib { public ref class MyManagedClass { public: void DoSomething(); }; }} 2) CPP Code (MyInterop.ManagedCppLib.cpp) #include "stdafx.h" #include "MyInterop.ManagedCppLib.h" #pragma unmanaged void UnmanagedMethod(int a, int b, void (*sum)(const int)) { int result = a

Using C++ Class DLL in C# Application

倾然丶 夕夏残阳落幕 提交于 2019-11-26 11:41:38
I have an unmanaged C++ DLL which merely exports a single class (not COM...it's just a simple C++ class) as its interface. I want to use this class in C# but am told that it cannot merely be imported into C#. What is the right way to use this class in my C# application? ShuggyCoUk Simple way assuming class Foo: Create a C++/CLI project, call this FooWrapper. Make FooWrapper depend on the unmanaged dll (however you normally would). Create a managed class ManagedFoo which contains a single private instance field of type Foo*. provide public wrapping functions in ManagedFoo which forward on to

In C++/CLI, how do I declare and call a function with an 'out' parameter?

混江龙づ霸主 提交于 2019-11-26 10:57:16
问题 I have a function which parses one string into two strings. In C# I would declare it like this: void ParseQuery(string toParse, out string search, out string sort) { ... } and I\'d call it like this: string searchOutput, sortOutput; ParseQuery(userInput, out searchOutput, out sortOutput); The current project has to be done in C++/CLI. I\'ve tried using System::Runtime::InteropServices; ... void ParseQuery(String ^ toParse, [Out] String^ search, [Out] String^ sort) { ... } but if I call it

How to use LINQ in C++/CLI - in VS 2010/.Net 4.0

十年热恋 提交于 2019-11-26 09:47:02
问题 Just wondering if there is a way to use LINQ in C++/CLI. I found one post that was focused on VS 2008 and required a bunch of workarounds for the System::String class. I have seen some framework replacements on CodeProject, but I was wondering if there is a way to use it directly in C++/CLI. If you can, anyone have a good example? 回答1: You can use the Linq methods that are defined in the System::Linq namespace, but you'll have to jump through a couple extra hoops. First, C++/CLI doesn't

What is the best way to convert between char* and System::String in C++/CLI

怎甘沉沦 提交于 2019-11-26 09:26:25
问题 What is the approved way to convert from char* to System::string and back in C++/CLI? I found a few references to marshal_to<> templated functions on Google, but it appears that this feature never made the cut for Visual Studio 2005 (and isn\'t in Visual Studio 2008 either, AFAIK). I have also seen some code on Stan Lippman\'s blog, but it\'s from 2004. I have also seen Marshal::StringToHGlobalAnsi(). Is there a method that is considered \"best practice\"? 回答1: There's a good overview here

Change C++/CLI project to another framework than 4.0 with vs2010

给你一囗甜甜゛ 提交于 2019-11-26 08:55:21
问题 Since I upgraded my project to visual studio 2010 project format, my C++/CLI project is targeted to .net framework 4.0. It is easy to switch the framework version to another version from a C# project, but I have no clue how to do this in a C++/CLI project, I see no setting for this in the project property pages. 回答1: This shows up when you press F1 in the Framework and References dialog: By default for new projects, the targeted framework is set to .NET Framework 4. The IDE does not support

how to convert System::String to const char*?

匆匆过客 提交于 2019-11-26 08:38:33
问题 how to convert \'String^\' to \'const char*\'? String^ cr = (\"netsh wlan set hostednetwork mode=allow ssid=\" + this->txtSSID->Text + \" key=\" + this->txtPASS->Text); system(cr); Error : 1 IntelliSense: argument of type \"System::String ^\" is incompatible with parameter of type \"const char *\" 回答1: You can do this using the msclr::interop::marshal_context class: #include <msclr/marshal.h> Then: String^ something = "something"; msclr::interop::marshal_context ctx; const char* converted =

What does System.Double[*] mean

无人久伴 提交于 2019-11-26 08:37:48
问题 This gem was created in some interop code which we decompiled. We can\'t figure out how to create an instance of this array, nor what type of array it is. Looking at Type.GetElementType gives me that it is an array of type Double , but we can\'t figure out how it is different from System.Double[] . 回答1: This is a typical interop problem, the array was created in a COM Automation server. Which exposes the array as a SafeArray, the CLR automatically marshals them to a .NET array object and

C++/CLI wrapper for native C++ to use as reference in C#

怎甘沉沦 提交于 2019-11-26 07:55:20
问题 Title explains. I have native C++ dlls that I\'m writing C++/CLI wrappers for, which will in turn will be imported in C# as reference. The problem is that in C# I don\'t see the classes I have in wrapper (imported from DLL). What keywords should I use and HOW to re-declare my native C++ objects to become visible in C#? 回答1: Ok, tutorial. You have a C++ class NativeClass that you want to expose to C#. class NativeClass { public: void Method(); }; 1) Create a C++/CLI project. Link to your C++

Why C# is not allowing non-member functions like C++

橙三吉。 提交于 2019-11-26 06:06:33
问题 C# will not allow to write non-member functions and every method should be part of a class. I was thinking this as a restriction in all CLI languages. But I was wrong and I found that C++/CLI supports non-member functions. When it is compiled, compiler will make the method as member of some unnamed class. Here is what C++/CLI standard says, [Note: Non-member functions are treated by the CLI as members of some unnamed class; however, in C++/CLI source code, such functions cannot be qualified