c++-cli

How to get the executable path from a Managed DLL

倾然丶 夕夏残阳落幕 提交于 2019-12-07 10:58:46
问题 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? 回答1: Assembly.GetCallingAssembly() or Assembly.GetExecutingAssembly() or Assembly.GetEntryAssembly() Depending on your need. Then use Location or CodeBase property (I never remember which

Can a managed ref-class directly implement a COM interface?

匆匆过客 提交于 2019-12-07 09:49:26
Is there a built-in way to allow a managed ref-class to implement and expose a COM inerface that is safely callable from native code? Looking at the C# side, this is easily done by decorating the target interface with the proper COM-interop attributes, for example: Native Interface interface ISampleGrabberCB: public IUnknown { virtual STDMETHODIMP SampleCB( double SampleTime, IMediaSample *pSample ) = 0; virtual STDMETHODIMP BufferCB( double SampleTime, BYTE *pBuffer, long BufferLen ) = 0; }; static const IID IID_ISampleGrabberCB = { 0x0579154A, 0x2B53, 0x4994, { 0xB0, 0xD0, 0xE7, 0x73, 0x14,

How do I use ExpectedException in C++/CLI NUnit tests?

守給你的承諾、 提交于 2019-12-07 09:33:49
问题 How do you do the equivalent of: [Test, ExpectedException( typeof(ArgumentOutOfRangeException) )] void Test_Something_That_Throws_Exception() { throw gcnew ArgumentOutOfRangeException("Some more detail"); } ...in C++ (the example there is C#)? As far as I can see, there's no typeof() function for the C++ implementation of NUnit. 回答1: To avoid anyone else hunting around for ages trying to find it, here's the solution: [Test, ExpectedException( ArgumentOutOfRangeException::typeid )] void Test

How can we share the data using shared memory segment with “Object” between two managed processes?

こ雲淡風輕ζ 提交于 2019-12-07 09:08:28
How can I share the data between two managed processes using shared memory segments? I am using "object" inside C++/CLI code to share the data with some other part of memory in the other process. I am using following code segment. #define BUFFER_SIZE 32768 #pragma data_seg (".SHAREDMEMORY") bool _Locked = false; bool _Initialized = false; unsigned char[10000] data = NULL; #pragma data_seg() #pragma comment(linker,"/SECTION:.SHAREDMEMORY,RWS") but I need it to be: #pragma data_seg (".SHAREDMEMORY") bool _Locked = false; bool _Initialized = false; object^ _object = nullptr; #pragma data_seg()

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

夙愿已清 提交于 2019-12-07 08:44:19
问题 Please help me to convert .doc file to .docx using open xml sdk or any other method except word automation. Thanks in advance. 回答1: 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. 回答2: I haven't used it, but take a look at

Getting sensible info from CLR-to-SEH exceptions in a mixed mode C++ project

戏子无情 提交于 2019-12-07 08:42:44
问题 Mixed mode C++ project. Native code is calling managed code. Managed code might throw an exception. I can catch said exception in native mode using a vectored exception handler; I can see its PEXCEPTION_POINTERS . The telling code 0xE0434F4D, meaning it's a CLR exception, is there. Question: is there any way to get any sensible information (exception class, message, stack trace etc.) from the attendant data? There's one parameter in the ExceptionInformation , and it looks like a pointer to

Prevent delegate from being garbage collected

点点圈 提交于 2019-12-07 08:42:35
问题 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) {

Error <mutex> is not supported when compiling with /clr or /clr:pure

让人想犯罪 __ 提交于 2019-12-07 07:25:39
问题 I have a C++ dll which I will call Dll A where I have used: #include <mutex> Dll As properties are set to "No Common Language Runtime Support" and it builds successfully. I have another Dll B which includes DLL A in its references. Dll Bs properties are set to: "Common Language Runtime Support (/clr)" as it includes C++/CLI code. When I build DLL B I get the error: C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\mutex(8): fatal error C1189: #error : <mutex> is not supported

How to check a generic type in C++/CLI?

*爱你&永不变心* 提交于 2019-12-07 07:20:48
问题 In C++/CLI code I need to check if the type is a specific generic type. In C# it would be: public static class type_helper { public static bool is_dict( Type t ) { return t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IDictionary<,>); } } but in cpp++\cli it does not work the same way, compiler shows the syntax error: class type_helper { public: static bool is_dict( Type^ t ) { return t->IsGenericType && t->GetGenericTypeDefinition() == System::Collections::Generic::IDictionary<,>:

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

两盒软妹~` 提交于 2019-12-07 07:10:07
问题 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