interop

A working .NET example using SetEntriesInAcl interop in action

时光总嘲笑我的痴心妄想 提交于 2020-01-05 07:46:30
问题 Does anyone has a working example of invoking SetEntriesInAcl method in .NET using P/Invoke? I keep getting error 87 when invoking it and just cannot get what am I doing wrong. Here are my definitions: private enum FileAccessRights { FILE_READ_DATA = 0x0001, } private enum AccessMode { GRANT_ACCESS = 1, REVOKE_ACCESS = 4, } private enum InheritanceFlags { NO_INHERITANCE = 0x0, } private enum TrusteeForm { TRUSTEE_IS_SID = 0, } private enum TrusteeType { TRUSTEE_IS_USER = 1, } private struct

C# call WinApi?

≡放荡痞女 提交于 2020-01-05 03:54:05
问题 I am trying to call a WinAPI function DeviceIoControl in C# with code IOCTL_DISK_SET_DISK_ATTRIBUTES and pass struct SET_DISK_ATTRIBUTES. I am trying do it with this code: const uint GENERIC_READ = 0x80000000; const uint GENERIC_WRITE = 0x40000000; const int FILE_SHARE_READ = 0x1; const int FILE_SHARE_WRITE = 0x2; const uint IOCTL_DISK_SET_DISK_ATTRIBUTES = 0x0007c0f4; const ulong DISK_ATTRIBUTE_READ_ONLY = 0x0000000000000002; [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet

Passing NON null-terminated strings to unmanaged code

僤鯓⒐⒋嵵緔 提交于 2020-01-04 04:07:08
问题 Consider the following struct to be sent over TCP to an unmanaged dll [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] public struct FooMessage { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 42)] public string foo; //More fields... } Using the following function (credit to Cheeso): public byte[] RawSerialize( T item ) { int rawSize = Marshal.SizeOf( typeof(T) ); IntPtr buffer = Marshal.AllocHGlobal( rawSize ); Marshal.StructureToPtr( item, buffer, false ); byte[]

Making COM calls from single thread hangs the thread

霸气de小男生 提交于 2020-01-04 02:44:08
问题 I have an application that does some excel automation through an automation add in. This add-in is multithreaded, and all the threads manage to make calls to the excel COM objects. Because excel can sometimes return a "is busy" exception when making multiple calls, i have wrapped all my calls in a "retry" function. However i feel this is inneficient. I am now trying to make all the calls to excel objects on the same thread, so that all calls are "serialized" by me, therefore reducing the risk

“P/Invoke entry points should exist” with what should be correct entry points stated

有些话、适合烂在心里 提交于 2020-01-04 02:41:13
问题 I'm getting this warning from the Code Analysis tool in Visual Studio 2012. The code looks like this: using System; using System.Runtime.InteropServices; namespace MyProgramNamespace { class NativeMethods { [DllImport("user32.dll", EntryPoint = "GetWindowLongPtr")] public static extern IntPtr GetWindowLongPtr(IntPtr handle, int flag); [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")] public static extern IntPtr SetWindowLongPtr(IntPtr handle, int flag, IntPtr ownerHandle); } } I'm

How to marshal a list in C#

一世执手 提交于 2020-01-03 16:45:51
问题 I have to send a list from C# to C++.The C# list is List<string>MyList and the C++ code accepts it as list<wstring>cppList .How to use marshalas for this. Thanks 回答1: It is always wiser not to use complex type marshaling between native code and managed code. In case of List , these type totally differ from each other as they have different memory layout for each item. So the best way is to write a utility function in a native dll that accepts array of string(char*) and manually build your

How to marshal a list in C#

落花浮王杯 提交于 2020-01-03 16:43:35
问题 I have to send a list from C# to C++.The C# list is List<string>MyList and the C++ code accepts it as list<wstring>cppList .How to use marshalas for this. Thanks 回答1: It is always wiser not to use complex type marshaling between native code and managed code. In case of List , these type totally differ from each other as they have different memory layout for each item. So the best way is to write a utility function in a native dll that accepts array of string(char*) and manually build your

Native C++ and C# interop

允我心安 提交于 2020-01-03 15:18:13
问题 So I'm architecting an application that does necessarily C++ work, but MFC/ATL is too messy for my liking, so I had this brilliant idea of doing all the "thinking" code in native C++ and all the pretty UI code in C#. The problem, though, is interoperability between the two of them. Before I get too carried away with this, I was wondering if this is a solved problem, and there's a really good way to do this. Note that I don't want to mix logic and display in the same module, as it gives rise

Images not displayed when using Interop.Excel to convert excel to PDF

三世轮回 提交于 2020-01-03 12:37:12
问题 I'm using Interop.Excel to convert excel (xlsx)(2010) to PDF for an application. On my development machine it works fine and the images are displayed correctly. However on the server when the excel is converted to PDF the images (some inserted via code and others in the template document) are not displayed in the PDF. The excel file is fine when viewed. Here is my code I use to convert to PDF: Public Shared Function FromExcel(ByVal ExcelFileLocation As String, ByVal PDFFileLocation As String)

How to get around Marshal.Copy (32bit) length limit?

时光怂恿深爱的人放手 提交于 2020-01-03 08:24:49
问题 I'm trying to move data back/forth between managed (C#) and unmanaged (C++ Win32) code. I can use Marshal.Copy and it works just fine until the datasets get bigger > 2GB as Marshal.Copy has a signed 32 bit int (2GB) limit for length. Any idea how to get around this? Currently I use AllocHGlobal(IntPtr) on the managed side and .ToPointer() on the unmanaged side. If I can't use Marshal.Copy to move large data (> 2GB) back/forth what can I use? 回答1: My first reaction was: why are your copying