unmanaged

How to marshall c++ char* to C# string using P/INVOKE

假装没事ソ 提交于 2019-12-23 14:54:18
问题 I'm new to C++. I'm calling a C++ function from C# using a PINVOKE and wanting to get a string back as an out parameter. However I just get an empty string back. The int out parameter works fine. Importing: [DllImport ( @"UnamanagedAssembly.dll", CharSet = CharSet.Ansi)] public static extern int Activate(ref int numActivated, StringBuilder eventsActivated); extern "C" __declspec(dllexport) int Activate(int *p_NumActivated, char *p_EventsActivated) {return Activation::GetInstance()->Activate(p

Managed to unmanaged code call causes access violation… sometimes

非 Y 不嫁゛ 提交于 2019-12-23 14:02:04
问题 This code causes the following exception, sometimes : "Attempted to read or write protected memory. This is often an indication that other memory is corrupt" private static TOKEN_GROUPS GetTokenGroups(IntPtr tokenHandle) { var groups = new TOKEN_GROUPS(); uint tokenInfoLength = 0; uint returnLength; var res = GetTokenInformation(tokenHandle, TOKEN_INFORMATION_CLASS.TokenGroups, IntPtr.Zero, tokenInfoLength, out returnLength); if (!res && returnLength > 0) { tokenInfoLength = returnLength; var

Calling unmanaged C/C++ DLL functions from SQL Server 2008

怎甘沉沦 提交于 2019-12-23 09:59:36
问题 I have a vast library of C/C++ functions, which needs to be called from SQL Server 2008. I have written a C# adapter class which loads these functions from Win32 DLL with DllImport and exposes them to .Net code. This works perfectly fine in most .Net applications. Now, I was trying to use the same technique with SQL Server CLR. I create a set of CLR functions and stored procedures, which call the adapter class. This does not work as an attempts to load unmanaged DLL results in System

Handling exception from unmanaged dll in C#

我是研究僧i 提交于 2019-12-23 09:36:46
问题 I have the following function written in C# public static string GetNominativeDeclension(string surnameNamePatronimic) { if(surnameNamePatronimic == null) throw new ArgumentNullException("surnameNamePatronimic"); IntPtr[] ptrs = null; try { ptrs = StringsToIntPtrArray(surnameNamePatronimic); int resultLen = MaxResultBufSize; int err = decGetNominativePadeg(ptrs[0], ptrs[1], ref resultLen); ThrowException(err); return IntPtrToString(ptrs, resultLen); } catch { return surnameNamePatronimic; }

GCHandle, AppDomains managed code and 3rd party dll

我是研究僧i 提交于 2019-12-23 09:29:22
问题 I have looking at many threads about the exception "cannot pass a GCHandle across AppDomains" but I still don't get it.... I'm working with an RFID Reader which is driven by a DLL. I don't have source code for this DLL but only a sample to show how to use it. The sample works great but I have to copy some code in another project to add the reader to the middleware Microsoft Biztalk. The problem is that the process of Microsoft Biztalk works in another AppDomain. The reader handle events when

.NET - Copy from Unmanaged Array to Unmanaged Array

元气小坏坏 提交于 2019-12-23 09:15:14
问题 I've been looking through the Marshal class, but I can't seem to find a method that allows me to copy from an unmanaged array (IntPtr) to another unmanaged array (IntPtr). Is this possible using .NET? 回答1: You can also DllImport RtlMoveMemory to get the job done: [DllImport("Kernel32.dll", EntryPoint="RtlMoveMemory", SetLastError=false)] static extern void MoveMemory(IntPtr dest, IntPtr src, int size); This will also require FullTrust, however, but as you are working with unmanaged code, I'd

Marshal.PtrToStringUni() vs new String()?

假装没事ソ 提交于 2019-12-23 08:47:10
问题 Suppose i have a pointer of type char* to unicode string, and i know the length: char* _unmanagedStr; int _unmanagedStrLength; and i have 2 ways to convert it to .NET string: Marshal.PtrToStringUni((IntPtr)_unmanagedStr, _unmanagedStrLength); and new string(_unmanagedStr, 0, _unmanagedStrLength); In my tests, both calls gives me exactly the same result, but the new string() is like 1.8x times faster than Marshal.PtrToStringUni() . Why is that performance difference? Is there any another

Memory leak problems: dispose or not to dispose managed resources?

泪湿孤枕 提交于 2019-12-23 05:44:11
问题 I experience strange memory leak in computation expensive content-based image retrieval (CBIR) .NET application The concept is that there is service class with thread loop which captures images from some source and then passes them to image tagging thread for annotation. Image tags are queried from repository by the service class at specified time intervals and stored in its in-memory cache (Dictionary) to avoid frequent db hits. The classes in the project are: class Tag { public Guid Id {

Unload a .NET DLL from an unmanaged process

风流意气都作罢 提交于 2019-12-23 05:39:11
问题 I'm extending my Inno-Setup script with code that I can best implement in C# in a managed DLL. I already know how to export methods from a managed DLL as functions for use in an unmanaged process. It can be done by IL weaving, and there are tools to automate this: NetDllExport (written by me) UnmanagedExports So after exporting, I can call my functions from Pascal script in an Inno-Setup installer. But then there's one issue: The DLL can't seem to be unloaded anymore. Using Inno-Setup's

How do we distinguish between managed and unmanaged resources in C#? Is TextFieldParser unmanaged?

醉酒当歌 提交于 2019-12-23 04:47:14
问题 I recently learned how to use Microsoft.VisualBasic.FileIO.TextFieldParser to parse a text input. In the example given to me, the TextFieldParser is called using the keyword using using (var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(new StringReader(str))) Though after some further researches, I notice that the practice of using using keyword for TextFieldParser is not universal. As far as I understand, .Net Framework has both managed and unmanaged resource. And when we use