pinvoke

Create SecureString from unmanaged unicode string

I am wanting to try to tie the CryptUnprotectData windows API function and the .net SecureString together the best way possible. CryptUnprotectData returns a DATA_BLOB structure consisting of an array of bytes and a byte length. In my program this will be a Unicode UTF-16 string. SecureString has a constructor which takes a char* and ...

P/Invoking CryptImportKey and marshaling structs

I'm trying to P/Invoke into CryptImportKey from C# to set a known key before encrypting data that will be decrypted in a C++ Win32 service at some point. I have the method signature for the P/Invoke and that all works fine but i can't get it to accept my key blob. The C++ structs are in comments below and my C# ones for marshaling are ...

How to import void * C API into C#?

Given this C API declaration how would it be imported to C#? int _stdcall z4ctyget(CITY_REC *, void *); I've been able to get this far: [DllImport(@"zip4_w32.dll", CallingConvention = CallingConvention.StdCall, EntryPoint = "z4ctygetSTD", ExactSpelling = false)] private extern static int z4ctygetSTD(ref...

How to import const int * const buffer[] into C# ?

This is tricky for me. const int * const buffer[] Currently, I have it translated as follows: byte[] buffer Problem is that I'm getting AccessViolation exceptions, when DLL is calling function with that is using above parameter. Thanks for help. ...

P/Invoke, Pinning, and KeepAlive Best Practices

At work we have a native C code responsible for reading and writing to a proprietary flat file database. I have a wrapper written in C# that encapsulates the P/Invoke calls into an OO model. The managed wrappers for the P/Invoke calls have grown in complexity considerably since the project was started. Anecdotally the current wrapper is ...

Can .NET PInvoke dynamically load a native dll from a user specified directory?

I have a .NET application and need to load a native library whose location is specified by the user. PInvoke looks like it'll only load from the global search paths (or a path specified at compile time?). Would the best method be to create a C++/CLI assembly which calls LoadLibrary at runtime? Would C++/CLI be simpler than C# PInvokin...

Retrieve data from const int * const buffer[]

I marshalled correctly to: IntPtr buffer Buffer is pointer to array of 2 pointers to arrays with respective data. The problem is that I get not accurate data, like if there's something missing in the data retrieved (e.g. missimg samples from stream of audio data). // length is parameter IntPtr[] temp = new IntPtr[2]; Marshal.Copy(buf...

How to detect ActiveSync connection on a mobile device?

Im using the CompactFramework 3.5 on a WindowsCE 5.0 device to build an application that should use active sync's connection to get some data. How can i detect if the mobile device is in the cradle and has an active connection? Is there some kind of PInvoke call for this? ...

new IntPtr(0) vs. IntPtr.Zero

Is there any difference between the two statements: IntPtr myPtr = new IntPtr(0); IntPtr myPtr2 = IntPtr.Zero; I have seen many samples that use PInvoke that prefer the first syntax if the myPtr argument is sent by ref to the called function. If I'll replace all new IntPtr(0) with IntPtr.Zero in my application, will it cause any dama...

Exporting functions from C++ dll to C# P/Invoke

I have built a C++ dll that I would like to call from C# code. I'm able to call one function, but the other throws an exception when the C# code tries to load the dll. The header looks like this: extern "C" __declspec(dllexport) BOOL Install(); extern "C" __declspec(dllexport) BOOL PPPConnect(); This produces a dll with slightly conf...

Get DeviceContext of Entire Screen with Multiple Montiors

I need to draw a line (with the mouse) over everything with C#. I can get a Graphics object of the desktop window by using P/Invoke: DesktopGraphics = Graphics.FromHdc(GetDC(IntPtr.Zero)); However, anything I draw using this graphics object is only showing on the left monitor, and nothing on the right monitor. It doesn't fail or anythi...

P/Invoke with [Out] StringBuilder / LPTSTR and multibyte chars: Garbled text?

I'm trying to use P/Invoke to fetch a string (among other things) from an unmanaged DLL, but the string comes out garbled, no matter what I try. I'm not a native Windows coder, so I'm unsure about the character encoding bits. The DLL is set to use "Multi-Byte Character Set", which I can't change (because that would break other project...

Windows Mobile API calls - success but GetLastWin32Error returns error code - should I be worried?

I am a newbie to PInvoke calls. I have googled this as it seems like a simple enough question but no joy. I am making muliple Windows Mobile API calls in a row (to detect if my app is already running and then re-activate it). Everything works fine and dandy but I wanted to put in logging etc for the times when it doesn't work OK. Whi...

How can I flash the taskbar from a partial trusted .NET application?

I'd like to flash the taskbar (as described here for example), but I can't P/Invoke FlashWindowEx (or anything else, for that matter) in the security context my application is running in. Is there another way to get the taskbar to flash? If not, what are my options for getting the user's attention? ...

Conversion to Mono on a Mac

Hi all, I have a project written .NET 2.0 (well, it doesn't use much in the way of 3.5 features, anyway), and I recently got a Mac and would like to convert that project to Mono. The problem is, this project relies on libraries such as FreeImage and a few C++ libraries I've written for this project. I'm a total newb to programming on ...

How to get the active window of a logged on user from a service

I am writing a C# service which has to retrieve information from the currently logged on user like the active window or the last mouse movement. I already learned that I can retrieve these information by using the user32.dll but this does only work from within the user context which calls the methods. This way my service could only retr...

Moving mouse cursor programmatically

To start out I found this code at http://swigartconsulting.blogs.com/tech_blender/2005/08/how_to_move_the.html: public class Win32 { [DllImport("User32.Dll")] public static extern long SetCursorPos(int x, int y); [DllImport("User32.Dll")] public static extern bool ClientToScreen(IntPtr hWnd, ref POINT point); [StructLayout(LayoutKind....

Determining when IE activex control has been repainted.

I was using spy++ and noticed that the IE control I have embedded in a windows form was periodically calling or sending WM_PAINT when it repaints itself. I'm trying to figure out how in C# code I can perform a C# method every time this control sends WM_PAINT. I know just enough pinvoke at this point to be dangerous. Thanks in advance,...

P/Invoke [DllImport] on ASP.NET

Hi, I'm having some problems with concurrency when using DLLImport, I have a Dll that provides some report I need to send over the web, so I have this: [DllImport("Rep.dll", EntryPoint = "PrintRep", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] private static extern string PrintRep(several params...); And ov...

C# P/Invoke structure problem

I am trying to write a C# P/Invoke wrapper for a C API (a native Win dll), and generally this is working fine. The only exception is a specific method which takes a struct as a parameter in the C code. The function is invoked without any exceptions, but it returns false indicating that something failed in the execution. In the API heade...