pinvoke

Wrapping msctls_hotkey32 in .NET Windows Forms

Hi all. I'm trying to get a hotkey control working in C# based on both the Control class and the msctls_hotkey32 win32 class. What I have works, though it's not perfect: class Hotkey : TextBox { protected override CreateParams CreateParams { get { CreateParams parms = base....

Proper way of passing a pointer for P/Invoke function.

Dear skilled. I’m developing an entity which allows user to copy multiple files in async manner with cancellation ability (and reporting progress as well). Obviously the process of copying runs in another thread, different from thread where CopyAsync was called. My first implementation uses FileStream.BeginRead/BeginWrite with a buffer ...

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

Hey guys, 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 numActivat...

P/Invoke errors only on Vista/Windows Server 2008

I have code that uses methods from the SSPI dll (security.dll) via P/Invoke, which has worked perfectly on every platform tested (Windows XP, Windows Server 2003 both x86 and x64), however we are in the process of migrating to Windows Server 2008, and have found that the P/Invoke calls are crashing the process. I have put together the f...

C# - DLLImport and function default values

I'm interfacing with a native 3rd party C++ DLL via C# and the provided interop layer looks like below: C#: [DllImport("csvcomm.dll")] public static extern int CSVC_ValidateCertificate(byte[] certDER, int length); C++: CSVC_Status_t CSVCOMM_API CSVC_ValidateCertificate(BYTE* certDER, DWORD length, DWORD context = CONTEXT_DEFAUL...

What approach can I take to calling into a non c-style native c++ interface which from just the dll from .NET ?

I've been browsing the web for over 48 hours reading up on interoperability and playing around with Pinvoke and C++/Cli (very limited the latter). There is an unmanaged c++ api (a namespace containing a lot of exported structs which contain methods, all pure virtual and implemented across several cpp files as classes and what not) and I ...

Obtain a filename from a file handle?

I have the ntdll.dll's NtCreateFile() function hooked to allow/deny the access of certain files. Unlike kernel32.dll's CreateFile() which easily gives you the full path to the file in question, ntdll.dll's NtCreateFile() function only gives you the handle to the file. I need to obtain the full path of the file from a file handle, to cons...

What exceptions should I expect from P/Invoke unsafe code?

Hi skilled, In my solution I've written the following: [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] [return: MarshalAs(UnmanagedType.Bool)] static extern unsafe bool CopyFileEx(string lpExistingFileName, string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, Boolean* pbCanc...

Managed to unmanaged code call causes access violation... sometimes

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 = Ge...

Is Intptr sufficient when marshalling for wrapping a c++ interface (all abstract) which works by passing interface handles?

I am trying to wrap an unmanaged c++ interface composed of several abstract structs (with all pure virtual methods) and a small factory namespace which returns handles (shared_ptrs) to these structs. It seems that, because of this, I might be able to get away with merely marshalling pointers through as System.IntPtr types (although them...

How to read the partition table of removable drive

I need to read the partition table of a USB stick. I am using PInvoke for this. I can create file successfully But how to read the partition table? Here is the code: namespace PInvokeWithForms { public partial class Form1 : Form { const uint GENERIC_READ = 0x80000000; const uint OPEN_EXISTING = 3; IntPtr...

How do I pinvoke to GetWindowLongPtr and SetWindowLongPtr on 32-bit platforms?

I want to P/Invoke to GetWindowLongPtr and SetWindowLongPtr, and I'm seeing conflicting information about them. Some sources say that, on 32-bit platforms, GetWindowLongPtr is just a preprocessor macro that calls GetWindowLong, and GetWindowLongPtr doesn't exist as an entry point in user32.dll. For example: The pinvoke.net entry for S...

writing unmanaged DLL consumable by C#/.NET code

I need to implement a small part of my application logic in native code. To test PInvoke capabilities I created a simple solution with an unmanaged C++ Win32 Dll and a WPF project that consumes the dll functions using PInvoke. The problem I run into is that i receive exception about "unbalancing the stack" and "possible signature mismatc...

Querying GPS location (only long. & lat.) from WM6 phone using .NET CF

Hello there, I am looking for a very simple solution of how to retrieve GPS data from WM6 cell phone. I am not looking for a GPS wrapper library (Google seems to find ONLY GPS libs...). I'd appreciate just a piece of code which does exactly this: start gps, retrieve long and lat, end gps. Nothing more. I guess with .NET Compact Framework...

PInvoke a function from a DLL compiled in C

I have the following C function typedef struct ekeycore_ctx_ ekeycore_ctx; typedef struct ekeycore_enum_ ekeycore_enum; typedef struct ekeycore_device_ { char *serial; char *portname; char *node; BOOL present; BOOL used; } ekeycore_device; typedef struct ekeycore_simple_ ekeycore_simple; typedef enum { EKEYCO...

Detect laptop lid closure and opening

Is it possible to detect when a laptop's lid is open or closed? From what I've read, this isn't possible, but SO has helped me with the impossible before. The only thing I've found that might be in the right direction is an MSDN blog post about IOCTLs needed to report power buttons. Is it possible to "sniff" these as the OS calls them...

Optimize managed to native calls

What can be done to speed up calling native methods from managed code? I'm writing a program which needs to be able to manage arbitrarily-sized lists of objects and retrieve information from them at high speed, which it feeds into scripts. Scripts are bits of compiled C# code. I'm writing a basic interface layer from the C++ (native) DL...

I want to call a C# delegate from C++ unmanaged code. A parameterless delegate works fine , but a delegate with parameters crashed my program.

The Following is code of a function from a unmanged dll. It takes in a function pointer as argument and simply returns value returned by the called function. extern __declspec(dllexport) int _stdcall callDelegate(int (*pt2Func)()); extern __declspec(dllexport) int _stdcall callDelegate(int (*pt2Func)()) { int r = pt2Func(); re...

Embed C++ Libraries into .Net Libraries

In my .Net assemblies I would have to make use of some native (C++ ) dlls. Usually we need to copy the C++ dlls into the bin folder and use PInvoke to call it. To save the distribution cost, I want to embed the C++ into my .Net dll direct, so that the number of assemblies distributed would be less. Any idea how to do this? ...

Difficulties with p/invoke and EnumDisplaySettingsEx

I am attempting to retrieve information on all the various monitors (and their adapters) present on a system. So far, my attempts at using EnumDisplayDevices to list all the monitors has worked great - it found two, the first being "\\.\DISPLAY1\Monitor0" (the second is just 2 and 1, respectively, but it's irrelevant to this question). A...