unmanaged

How to import and use a unmanaged C++ class from C#?

会有一股神秘感。 提交于 2019-12-22 13:39:08
问题 I have an native C++ dll, some header files and the import library. Is there a way how to instantiate an object within C# that is defined in the dll? The two ways I'm aware of are: to wrap the C++ code into COM to use DLLImport and external C functions 回答1: C++/CLI is your friend for this. You'll run into one problem though: it is not possible to store standard C++ objects inside C++/CLI ref or value classes (the ones for .NET). So you'll have to resort to the following class (that you can

Is a WPF application managed code only?

落爺英雄遲暮 提交于 2019-12-22 10:28:09
问题 I want to use WPF in an app. I want to write it in C++. Does the application have to be managed? I know that I can mix managed with unmanaged. I'm wondering if I can have the whole application be unmanaged. 回答1: You can easily develop 99% of your WPF application in unmanaged code, but making it 100% unmanaged is quite difficult. WPF classes don't have a Guid attribute, so they won't work with COM. So constructing WPF objects such as Button and Window with 100% unmanaged code requires one of

How do I find the source of a “A procedure imported by 'xxx.dll' could not be loaded.” exception?

只谈情不闲聊 提交于 2019-12-22 07:27:09
问题 I have been chasing this exception for the past week. Situation is: I have an application that is written in C# and built in Visual Studio 2010. The application includes a DLL that is a wrapper of an unmanaged code library. The unmanaged code is written in C++ and built in Visual Studio 2008. This is required because the code references additional libraries (Qt) and that code targets WinCE version 5 (necessary due to devices supported in the field). I have tried many of the suggestions I have

Is it possible to call unmanaged code using C# reflection from managed code?

廉价感情. 提交于 2019-12-22 07:05:43
问题 Is it possible using reflection and C# .NET to call dynamicly different function (with arguments) written in C or C++ before .NET came(unmanaged code) ? And smole C# example if possible would be appreciated! Thanks! Br, Milan. 回答1: Yes, dynamic P/Invoke is possible in .NET using Marshal.GetDelegateForFunctionPointer . See the following sample taken from the section Delegates and unmanaged function pointers from the article Writing C# 2.0 Unsafe Code by Patrick Smacchia: using System; using

Unmanaged Memory leak

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-22 05:13:35
问题 I have am using a WPF application which uses BitmapSource but I need to do some manipulation but I need to do some manipulation of System.Drawing.Bitmaps. The memory use of the application increases while it runs. I have narrowed down the memory leak to this code: private BitmapSource BitmaptoBitmapsource(System.Drawing.Bitmap bitmap) { BitmapSource bms; IntPtr hBitmap = bitmap.GetHbitmap(); BitmapSizeOptions sizeOptions = BitmapSizeOptions.FromEmptyOptions(); bms = System.Windows.Interop

What are the implications of using unsafe code

独自空忆成欢 提交于 2019-12-22 01:24:24
问题 Aside from the fact that the code itself can access memory directly. What are the other implications of using the "/unsafe" compiler flag and the "fixed" keyword? Are there any knock on effects related to code signing and deployment of my .exe (my app is desktop only)? (This isn't about whether or not I should be doing this, the why is covered in my question here) 回答1: Unsafe code is not verifiable, so you have to be aware of that. In a Full Trust environment, that's not a big deal, but if

calling unmanaged function char returns char *

瘦欲@ 提交于 2019-12-21 21:09:33
问题 I have a function in unmanaged C/C++ code (dll) that returns a structure containing a char array. I created C# struct to receive this return value uppon calling the function. And uppon calling this function i get 'System.Runtime.InteropServices.MarshalDirectiveException' This is C declaration: typedef struct T_SAMPLE_STRUCT { int num; char text[20]; } SAMPLE_STRUCT; SAMPLE_STRUCT sampleFunction( SAMPLE_STRUCT ss ); This is C# declaration: struct SAMPLE_STRUCT { public int num; public string

How to allocate memory with a 16 byte alignment?

对着背影说爱祢 提交于 2019-12-21 12:41:40
问题 I use Marshal.GlobalHAlloc to allocate memory. As documentation says: "This method exposes the Win32 LocalAlloc function from Kernel32.dll.". GlobalAlloc 's documentation says it will be 8 byte aligned but LocalAlloc don't say anything about align. For example I want to allocate 1024 bytes and ensure it is aligned by 16. Will it work when I allocate 1024+16 bytes then I check pointer % 16? If result is 0 it means memory is aligned, when it is not 0, I just increment pointer to fit my

How to find caller assembly name in unmanaged c++ dll

空扰寡人 提交于 2019-12-21 06:24:42
问题 I have an unmanaged c++ dll . I am calling external methods of this unmanaged dll from c# (.net 3.5) I am looking for a way to find witch c# assembly is calling my unmanaged c++ dll (into my c++ dll) (at least, name of assembly) And sure, I don't want to pass any additional parameter to methods . Thanks in advance 回答1: This requires a stack walk. Works well in managed code, that's how code access security is implemented. Does not work so well when there are native stack frames to walk. You

What is the difference between a delegate instance and a method pointer?

浪子不回头ぞ 提交于 2019-12-20 20:21:38
问题 I thought that a delegate instance was interchangeable with a function instance. Take the following code: delegate int AddDelegate(int a, int b); AddDelegate DelegateInstance; public void DoStuff() { //I can call this without a delegate "instance": MethodThatTakesAdd(Add); //I can also call it WITH a delegate "instance" DelegateInstance = Add; MethodThatTakesAdd(DelegateInstance); } public int Add(int a, int b) { return a + b; } public void MethodThatTakesAdd(AddDelegate addFunction) {