pinvoke

Easiest way to generate P/Invoke code?

I am an experienced .Net programer, but have not compiled a C/C++ program in my life. Now I have this C-dll, headers and documentation (3rd party, not from Win API), from which I need to call about ten methods. I was thinking of using Platform Invoke. I found these three tools that would create the code for me: PInvoker: http://www.pi...

How to pass null pointer to Win32 API in C# .Net?

I'm looking at the RegisterHotKey Function: http://msdn.microsoft.com/en-us/library/ms646309(VS.85).aspx BOOL RegisterHotKey( __in HWND hWnd, __in int id, __in UINT fsModifiers, __in UINT vk ); I've been using IntPtr to pass in the first argument,m which works fine in most cases. But now I need to deliberately pass a null...

PInvokeStackImbalance -- C# with offreg.dll ( windows ddk7 )

I am trying to create an offline registry in memory using the offreg.dll provided in the windows ddk 7 package. You can find out more information on the offreg.dll here: MSDN Currently, while attempted to create the hive using ORCreateHive, I receive the following error: "Managed Debugging Assistant 'PInvokeStackImbalance' has dete...

Automatically creating C# wrappers from c headers?

Is there a way to automatically create p/invoke wrappers for .net from a c header? Of course I could create them by hand, but maintaining them would be painful, and I'd probably make a mistake somewhere resulting in hard to debug crashes. I tried SWIG, but it created full classes where simple structs would be sufficient. Another proble...

How do I marshal a pointer to an array of pointers to structures?

I have a C function with the following signature: int my_function(int n, struct player **players) players is a pointer to an array of pointers to struct player objects. n is the number of pointers in the array. The function does not modify the array nor the contents of the structures, and it does not retain any pointers after return...

How to refresh to entire device's screen (Windows Mobile)?

Hi everybody, I'm working on a simple application that draws an alpha-blended picture on the screen's Device Context every 2 secs, I want to refresh the screen contents before the drawing operation (To erase the drawn pic), I have used many many trick but unfortunately, the screen won't refresh correctly, some regions still keep portio...

SendMessage vs. WndProc

I'm trying to extend TextBox control to add watermarking functionality. The example I've found on CodeProject is using imported SendMessage function. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)] static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, uint wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam...

Marshal struct to unmanaged array

I have a C# struct to represent a cartesian vector, something like this: public struct Vector { private double x; private double y; private double z; //Some properties/methods } Now I have an unmanaged C dll that I need to call with P/Invoke. Some methods expect a double[3] parameter. The unmanaged C signat...

How to get window opened/closed/minimized messages from a native app?

It's tough to write a good title for this one. I'm working on a WPF application which needs to know about the existence of all other open windows on the system. I'm able to do this by calling the native EnumWindows method just fine, and I can call other native methods to filter out just the windows I'm interested in. This works well. T...

P/Invoke or C++/CLI for wrapping a C library

Have a moderate size (40-odd function) C API that needs to be called from a C# project. The functions logically break up to form a few classes that will be API presented to the rest of the project. Are there any objective reasons to prefer P/Invoke or C++/CLI for the interoperability underneath that API, in terms of robustness, maintain...

AccessViolationException, attempted to read or write protected memory

I'm using a dll that contains unmanaged code for interacting with specific hardware, and I'm trying to use it from C#, but I keep getting an AccessViolationException. What's causing it, and how can I fix it? namespace FingerPrint { public unsafe partial class Form1 : Form { [DllImport("MyDll.dll")] public static ex...

Need to pinvoke a .dll or something to change the mouse cursor in WPF...

I'm trying to override the cursor for the entire computer this.Cursor = Cursors.Pen; System.Windows.Input.Mouse.OverrideCursor = Cursors.Pen; This works within the WPF "Window", but not anywhere outside it. What .dll will let me do this? ...

DllImport and char*

I have a method I want to import from a DLL and it has a signature of: BOOL GetDriveLetter(OUT char* DriveLetter) I've tried [DllImport("mydll.dll")] public static extern bool GetDriveLetter(byte[] DriveLetter); and [DllImport("mydll.dll")] public static extern bool GetDriveLetter(StringBuilder DriveLetter); but...

Problems with CloseMainWindow() to close a Windows Explorer window

Hello! I´m facing a problem when trying to close a Windows Explorer (not Internet Explorer) window through another application, using the "Process.CloseMainWindow()" method; because it doesn´t close the Explorer window, it tries to close the full Windows (Operative System), by the way, Windows XP. The code is as follows: [DllImport("u...

Alternative native api for Process.Start

Ok this is not duplicate of "http://stackoverflow.com/questions/2065592/alternative-to-process-start" because my question is something different here. I need to run a process and wait till execution of process and get the output of console. There is way to set RedirectStandardOutput and RedirectStandardError to true, however this does ...

Can a .NET application targeted for "Any CPU" use P/Invoke calls in multiple environments?

I have a .NET application that uses some API calls, for example GetPrivateProfileString. So far it has always run on 32-bit machines. In order to run on 64-bit machines, must I change the "Platform Target" to "x86"? Or is there a way to let the runtime know which API DLL to invoke depending on the runtime environment? ...

How to use the ListView_GetBkImage macro in C#

How can I use the ListView_GetBkImage macro: http://msdn.microsoft.com/en-us/library/bb761246(v=VS.85).aspx ... from a C#/WinForms application? I think this macro just wraps the SendMessage method, but I'm not sure. I couldn't find any C#-based samples on this. Basically I'm trying to get a LVBKIMAGE ( http://msdn.microsoft.com/en-u...

Do we really need to catch exception for P/Invoke methods?

I am using P/Invoke methods in my .NET application. As the functions are C++ functions; each one has return type like int, intptr or any struct. The return type is enough to tell me if the function was successful or not. Do I still need to catch generic or COM exception in this case? ...

C#: Object with custom marshaller not containing data after PInvoke call

I am having a problem with PInvoking some WinAPI functions that accept WAVEFORMATEX structures as parameters. Since the length of the WAVEFORMATEX structure can vary, I implemented a WaveFormatEX class that is marshalled by a custom marshaller class (which implements ICustmoMarshaller). This is following an example provided by Aaron Lerc...

shared library under ubuntu

hi ,i have compiled srp-2.1.2 under ubuntu using make ,it creat a file libsrp.a. can any one tell me how can i use libsrp.a as shared library?.i want to use libsrp in a c# file under ubuntu by using dllimport.please tell me what is the meaning of libsrp.a file. thanks ok when i am using nm -D libsrp.a then i have c2@ubuntu:~/Desktop...