pinvoke

DllImport incomplete names

不羁的心 提交于 2019-12-24 18:05:07
问题 I am using several P/Invokes under .NET. However, I want my library to work both in Windows and Linux, preferably with the same binaries. Since the native library I depend on is available on multiple platforms, I was hoping to just have them along with my managed library's binaries. Right now I'm using something like this: [DllImport("/usr/lib/libMYLIBNAME.so.1")] But this obviously only works for Linux. I was considering that I could possibly copy that binary from /usr/lib and distribute

stdcall calling convention and using pinvoke in C#

本秂侑毒 提交于 2019-12-24 12:44:20
问题 I created a DLL file which includes two empty functions below. extern "C" __declspec(dllexport) void __stdcall myFunc1() { // just empty function } extern "C" __declspec(dllexport) void __cdecl myFunc2() { // just empty function } In C#, I could call the functions using DLLImport attribute like below. [DllImport("myDLL", CallingConvention=CallingConvention.StdCall)] private extern static void myFunc1(); [DllImport("myDLL", CallingConvention=CallingConvention.Cdecl)] private extern static void

How to share a class in a Visual C++ DLL with C#?

独自空忆成欢 提交于 2019-12-24 11:58:35
问题 I wrote my program in C++ and exported it as a DLL. I have a C# WPF GUI and I want to import the DLL to do the processing. I am very new to C#. How can I use the C++ class in C#? I know I can't just use the .h file. Because I used pointers in C++, in C# I couldn't use pointers. That's why I got confused. Like in C++ i used char* instead of string , and I need to return double* for a big collection of double data, and things like that. 回答1: There are a number of ways: Export the DLL functions

How do I write DllGetClassObject as a C# delegate?

允我心安 提交于 2019-12-24 09:58:16
问题 There is a dll method exported written in delphi: DllGetClassObject: function(const CLSID, IID: TGUID; var Obj): HResult; stdcall; I need to write equivalent method in c# as a delegate. How should it looks like? 回答1: Like this: [UnmanagedFunctionPointer(CallingConvention.StdCall)] public delegate uint DllGetClassObjectDelegate( [MarshalAs(UnmanagedType.LPStruct)] Guid rclsid, [MarshalAs(UnmanagedType.LPStruct)] Guid riid, [MarshalAs(UnmanagedType.IUnknown, IidParameterIndex=1)] out object ppv

Declarations of p/invoke for file io and pseudo terminal

别等时光非礼了梦想. 提交于 2019-12-24 06:57:43
问题 i want to use the file io functions (open, read, write) and the pseudo-terminal(http://linux.die.net/man/4/pts) functions (grantpt, unlockpt, ptsname) from mono. translating the arguments and return values is trivial (still, i would appreciate it if you could verify them) but i can't find the corresponding librarys. My linux distribution is Arch Linux on ARM (Raspberry PI). As the ARM Platform is only 32-Bit, i can just use int32 for int/size_t, etc Thank you very much. internal class

What's the lifetime of an unmanaged DLL once it's been pInovked from managed code?

自闭症网瘾萝莉.ら 提交于 2019-12-24 06:21:59
问题 For example if you have a simple managed console app and a simple unmanaged C++ DLL. You pInvoke into a function in the C++ DLL, does the DLL stay loaded in the unmanaged process until said process comes down? Or does the DLL get unloaded at the completion of every pInvoke call? 回答1: It stays in memory unless you explicity tell it to unload. 来源: https://stackoverflow.com/questions/3920261/whats-the-lifetime-of-an-unmanaged-dll-once-its-been-pinovked-from-managed-cod

Correct use of SafeHandles in this P/Invoke use case

橙三吉。 提交于 2019-12-24 06:03:58
问题 Working in C# with a native Dll, that uses opaque handles and internal reference counting, I have the following P/Invoke signatures (all decorated with DllImport attribute) [DllImport("somedll.dll"] public extern IntPtr getHandleOfA(IntPtr handleToB, int index); //(1) public extern IntPtr makeNewHandleOfA(); //(2) public extern void addRefHandleToA(IntPtr handleToA); //(3) public extern void releaseHandleToA(IntPtr handleToA); //(4) public extern void doSomethingWithHandle(IntPtr handleToA) /

Pinning a delegate within a struct before passing to unmanaged code

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 05:14:08
问题 I'm trying to use an unmanaged C dll for loading image data into a C# application. The library has a fairly simple interface where you pass in a struct that contains three callbacks, one to receive the size of the image, one that receives each row of the pixels and finally one called when the load is completed. Like this (C# managed definition): [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)] public struct st_ImageProtocol { public

C# Z Order of All Open Windows

青春壹個敷衍的年華 提交于 2019-12-24 04:35:48
问题 I am interested in getting the Z order for every window currently open by the user. For example, if they had 4 windows on top of each other, I'd like to know (by window title - which I can already get), the order - i.e. which is deepest in the stack and which is at the front of the stack. Does anyone have an idea as to the code for this? I've been looking at GetTopWindow() and GetNextWindow() but GetTopWindow() seems to return the top window within an application and GetNextWindow() doesn't

DeviceIoControl does not set output buffer

一曲冷凌霜 提交于 2019-12-24 04:13:07
问题 I'm having some problems with DeviceIOControl . I'm trying to read the disk geometry from a physical drive but the output buffer is never set. Here is a sample of my code, a simple function that is supposed to get the disk geometry and return whether the disk is removable: public static bool IsDeviceRemovable(int DriveNo) { string Filename=@"\\.\Physicaldrive"+DriveNo; SafeFileHandle drive=CreateFile( Filename, FileAccess_e.None, FileShare_e.Write|FileShare_e.Read, IntPtr.Zero,