c++-cli

Translate C++/CLI to C#

别来无恙 提交于 2019-12-05 20:34:44
I have a small to medium project that is in C++/CLI. I really hate the syntax extensions of C++/CLI and I would prefer to work in C#. Is there a tool that does a decent job of translating one to the other? EDIT: When I said Managed c++ before I apparently meant c++/CLI You can only translate Managed C++ code (and C++/CLI code) to C# if the C++ code is pure managed. If it is not -- i.e. if there is native code included in the sources -- tools like .NET Reflector won't be able to translate the code for you. If you do have native C++ code mixed in, then I'd recommend trying to move the native

void* to Object^ in C++/CLI

故事扮演 提交于 2019-12-05 19:35:48
I am working on wrapping a large number of .h and .lib files from native C++ to Managed C++ for eventual use as a referenced .dll in C#. Some of the native C++ functions have a return type of void*. I am not sure how to handle this when I pass back the value to my calling code. For instance: if a C# app calls my dll wrapper, what do I return from the native call: void* start(ThreadFunc,void *, unsigned *); I am currently attempting to box the return in a generic System::Object^ with no luck. This is the call in the wrapper: m_NativeThread->start(cb, GCHandle::ToIntPtr(GCHandle::Alloc(o))

Access violation when using pin_ptr?

送分小仙女□ 提交于 2019-12-05 19:16:54
When I use pin_ptr to pass an array in native c code, I get access violation. The code is as bellow: array<float>^ LogLikelihoodScore(array<array<unsigned char>^>^ modelsBuffer , array<float>^ featuresArray, int numberOfFrames) { int i, j, modelsNum = modelsBuffer->Length, len; float **models = (float**) malloc(modelsNum * sizeof(void*)); for(i = 0; i < modelsNum; i++) { pin_ptr<unsigned char> ptr = &modelsBuffer[i][0]; models[i] = (float*) ptr; } array<float>^ scores = gcnew array<float>(modelsNum); pin_ptr<float> scoresPtr = &scores[0]; pin_ptr<float> featuresPtr = &featuresArray[0]; char*

Prevent delegate from being garbage collected

老子叫甜甜 提交于 2019-12-05 18:44:14
I'm currently trying to marshal a C# delegate to a C++ function pointer and I looked at the example from Microsoft : // MarshalDelegate1.cpp // compile with: /clr #include <iostream> using namespace System; using namespace System::Runtime::InteropServices; #pragma unmanaged // Declare an unmanaged function type that takes two int arguments // Note the use of __stdcall for compatibility with managed code typedef int (__stdcall *ANSWERCB)(int, int); int TakesCallback(ANSWERCB fp, int n, int m) { printf_s("[unmanaged] got callback address, calling it...\n"); return fp(n, m); } #pragma managed

How to convert doc to docx using open xml sdk in c#

浪子不回头ぞ 提交于 2019-12-05 17:42:15
Please help me to convert .doc file to .docx using open xml sdk or any other method except word automation. Thanks in advance. The OpenXML SDK allows you to manipulate only .docx files, not .doc . Here's a blog post illustrating how to perform bulk conversions of .doc to .docx files using a bulk conversion utility. I am afraid that if you don't want to use Word Automation, you will have to write your own .doc parser which might be quite a work. Eric White I haven't used it, but take a look at http://b2xtranslator.sourceforge.net/ . In addition, if your scenario supports it, and if your

How to get the executable path from a Managed DLL

懵懂的女人 提交于 2019-12-05 17:24:16
I have a managed DLL (written in C++/CLI) that contains a class used by a C# executable. In the constructor of the class, I need to get access to the full path of the executable referencing the DLL. In the actual app I know I can use the Application object to do this, but how can I do it from a managed DLL? Assembly.GetCallingAssembly() or Assembly.GetExecutingAssembly() or Assembly.GetEntryAssembly() Depending on your need. Then use Location or CodeBase property (I never remember which one). Brian Stewart @leppie: Thanks - that was the pointer I needed. For future reference, in C++/CLI this

Pass a C++/CLI wrapper of a native type to another C++/CLI assembly

空扰寡人 提交于 2019-12-05 17:11:46
Suppose I have the following simple wrapper of a NativeClassInstance. public ref class Wrapper { private: NativeClass *_wrapped; public: Renderer() { _wrapped = new NativeClass(); } ~Renderer() { delete _wrapped; } operator NativeClass*() { return _wrapped; } } Now, I want to create an instance of Wrapper from C# with Wrapper wrapper = new Wrapper() and use it in another native functionalities wrapper that resides in another assembly with Helper.Foo(wrapper) (nothing strange having other functionalities not directly related to the wrapped classes in another assembly, IMO): // Utilities is in

Mixed management in C++

最后都变了- 提交于 2019-12-05 16:47:50
I have added a class to my program and tested it. I was really surprised that there was any real errors. Here is the code: #pragma once #include "Iingredient.h" #include <string> #include <vector> using namespace std; ref class Recipe{ private: string partsName; vector<Iingredient> ing; public: Recipe(){} }; And here are the errors: Error 23 error C4368: cannot define 'partsName' as a member of managed 'Recipe': mixed types are not supported c:\users\user\documents\visual studio 2010\projects\smestras2_l1\Recipe.h 10 1 file2_L1 Error 24 error C4368: cannot define 'ing' as a member of managed

Is there a C#'s unsafe equivalent in C++/CLI?

最后都变了- 提交于 2019-12-05 16:32:55
I am trying to port C++/CLI code into Verifiable Type-Safe C++/CLI code (Use clr:safe flag) so I can get a AnyCPU assembly. The main compilation problem I find is that, I get a lot of C4956 errors and I think, that might be solved by explicitly tell the compiler I expect this to be unsafe. Suggestions? This has been covered here Basically, this is what /clr:pure was supposed to provide, because it also generates a pure MSIL assembly. Unfortunately it still causes a dependency on a particular bitness, so isn't compatible with AnyCPU . You shouldn't have many problems porting unsafe code from C#

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

怎甘沉沦 提交于 2019-12-05 16:06:10
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? 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.InteropServices.SEHException. All non-translated exceptions will be turned into that exception. As long as you have