pinvoke

Get the cluster size of a disk in C#--Get error on 'GetDiskFreeSpace'

余生颓废 提交于 2020-01-04 13:37:07
问题 I am trying to get the cluster size of a disk in C#. Everything I have found says to use "GetFreeDiskSpace," but I can't get it to work. It appears as if I am missing a using or something. When I Google the The name 'GetDiskFreeSpace' does not exist in the current context it brings up everything except for this specific error. If I do an exact phrase search, Google says nothing is found and then displays the non-exact phrase search results. I am trying to determine where the GetFreeDiskSpace

C# Marshal.Sizeof() when using custom marshaler

孤街浪徒 提交于 2020-01-04 09:54:18
问题 Is it possible to use Marshal.SizeOf() on a structure which is using a custom marshaler? For example: struct Abcde { public int test1; [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(MyCustomMarshaler)] public string customString1; } If I call: var size = Marshal.SizeOf(typeof(Abcde)); an exception is thrown which says that no meaningful size or offset can be computed. I noticed that ICustomMarshaler has a method called GetNativeDataSize() but the exception is thrown

Preventing computer from sleeping

删除回忆录丶 提交于 2020-01-04 09:07:53
问题 I have an application with some sort of media playing and I do not want the computer to sleep when my application runs. I searched around and came to know that this can be done by P/Invoke. Neither should the display be turned off and neither should the computer go to sleep. So, I did the following to test this: b.Click += (x, y) => { SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_AWAYMODE_REQUIRED); Debug.WriteLine("Power line executed"); }; [DllImport("kernel32

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[]

C# to C++ Array?

╄→гoц情女王★ 提交于 2020-01-04 02:54:32
问题 I have a library provided, which i know uses C++. I imported the DLL as such: [DllImport("pst")] private static extern int pst_get_sensor(ref PSTSensor sensor); The PSTSensor is a struct, in a C++ example it was defined as such: struct PSTSensor { char name[80]; /**< Device name */ int id; /**< Device identifier (for other tracking interfaces */ float pose[16]; /**< Device pose estimate as row-major matrix */ double timestamp; /**< Time the data was recorded */ }; The problem is, that besides

“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

RegisterHotkey Fn Modifier?

戏子无情 提交于 2020-01-03 18:50:54
问题 I know you can use CTRL , ALT , SHIFT , etc. modifiers for RegisterHotKey, but what about the Fn key? I used to have some bloatware on my laptop that changed brightness when Fn+Up/Down was pressed. I removed all the bloatware, and I want to write a program that does the same thing. Any help is appreciated! 回答1: Fn key is modifier but not software like ALT , SHIFT , etc. which are processed by operation system. Fn is hardware key modifier so it changes keycode keyboard sends to operation

Platform Invoke F# callback functions

╄→尐↘猪︶ㄣ 提交于 2020-01-03 17:25:44
问题 I am using F# on a Raspberry Pi 2 (ARM 7 & thus mono). I am currently trying to use the WiringPi library, written in C. I have successfully managed to use some of the functions using P/Invoke. Now I am trying to use interrupts (see http://wiringpi.com/reference/priority-interrupts-and-threads/) but I am stumped by this function with the following C signature int wiringPiISR (int pin, int edgeType, void (*function)(void)); Which has been translated (see https://github.com/danriches/WiringPi

How to marshal collection in c# to pass to native (C++) code

自古美人都是妖i 提交于 2020-01-03 14:17:52
问题 I am working on an enterprise application development. the entire application is developed in c++, except the UI, which is developed in c#, now its time to hookup the UI with c++ code. After detailed study i choose PInvoke in order to do so. All is successful, the only case where i stuck is that how can i pass collection to C++ code. e.g: C# Side Code List<string> lst = new List<string>(); lst.Add("1"); lst.Add("2"); lst.Add("3"); lst.Add("4"); C++ Side Code std::vector<std::string> vStr; Now

How to marshal collection in c# to pass to native (C++) code

回眸只為那壹抹淺笑 提交于 2020-01-03 14:17:08
问题 I am working on an enterprise application development. the entire application is developed in c++, except the UI, which is developed in c#, now its time to hookup the UI with c++ code. After detailed study i choose PInvoke in order to do so. All is successful, the only case where i stuck is that how can i pass collection to C++ code. e.g: C# Side Code List<string> lst = new List<string>(); lst.Add("1"); lst.Add("2"); lst.Add("3"); lst.Add("4"); C++ Side Code std::vector<std::string> vStr; Now