Questions tagged [pinvoke]

P/Invoke is an implementation specification created by Microsoft of the Common Language Infrastructure (CLI) for invocation of native code libraries from managed code.

P/Invoke stands for Platform Invocation Services. It is a specification created by Microsoft in order to provide interoperability with unmanaged code from managed applications. Data passed to/from P/Invoke should usually be converted to/from CLI types before it can be used. This process is called .

The wiki website for P/Invoke method declarations is pinvoke.net.

The base namespace in .NET Framework is System.Runtime.InteropServices.

Sample code in C# of a class using P/Invoke (from pinvoke.net):

class Beeper
{
    public enum beepType
    {
        SimpleBeep  = -1,
        OK    = 0x00,
        Question  = 0x20,
        Exclamation  = 0x30,
        Asterisk  = 0x40,
    }

    [DllImport("User32.dll", ExactSpelling=true)]
    private static extern bool MessageBeep(uint type);

    static void Main(string[] args)
    {
        Class1.beep(beepType.Asterisk);
        Thread.Sleep(1000);
        Class1.beep(beepType.Exclamation);
        Thread.Sleep(1000);
        Class1.beep(beepType.OK);
        Thread.Sleep(1000);
        Class1.beep(beepType.Question);
        Thread.Sleep(1000);
        Class1.beep(beepType.SimpleBeep);
    }

    public static void beep(beepType type)
    {
        MessageBeep((uint)type);
    }
}
3756 questions
14
votes
3 answers

How do I write a custom marshaler which allows data to flow from native to managed?

In attempting to write a custom marshaler related to this question (P/Invoke from C to C# without knowing size of array), I have come across something I cannot understand. This is the first ever custom marshaler that I have written so no doubt I'm…
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
14
votes
4 answers

P/Invoke to dynamically loaded library on Mono

I'm writing a cross-platform .NET library that uses some unmanaged code. In the static constructor of my class, the platform is detected and the appropriate unmanaged library is extracted from an embedded resource and saved to a temp directory,…
Gordon Leigh
  • 1,263
  • 2
  • 11
  • 23
14
votes
1 answer

How to pass a nullable type to a P/invoked function

I have a few p/invoked functions (but I'm rewriting my code at the moment so I'm tidying up) and I want to know how to use/pass a nullable type as one of the parameters. working with int types isn't a problem but given the…
Dark Star1
  • 6,986
  • 16
  • 73
  • 121
13
votes
2 answers

Naming Windows API constants in C#

The naming convention for constants in C# is Pascal casing: private const int TheAnswer = 42; But sometimes we need to represent already-existing constants from the Windows API. For example, I don't know how to name this: /// /// With…
Camilo Martin
  • 37,236
  • 20
  • 111
  • 154
13
votes
3 answers

Can I force MSTest to use a new process for each test run?

We're using the VS 2010 test runner (MSTest) for automated functional testing. When we run tests from Visual Studio, VS creates a process called QTAgent32.exe, and it runs the tests in that process. We've found that when we do multiple test runs,…
Richard Beier
  • 1,712
  • 20
  • 25
13
votes
1 answer

Get pointer (IntPtr) from a Span staying in safe mode

I would like to use Span and stackalloc to allocate an array of struct and pass it to an interop call. Is it possible to retrieve a pointer (IntPtr) from the Span without being unsafe ?
Mitsuru Furuta
  • 131
  • 1
  • 6
13
votes
1 answer

Excel as inlay frame in WPF has disabled ExcelWorksheet

I found a solution to setup Excel instance in WPF by using the SetParent() function of Windows. Problem is, that mouse and keyboard is not reacting to the sheet but to the Workbook it does. Full sample Project Download here I also tried with…
Nasenbaer
  • 4,810
  • 11
  • 53
  • 86
13
votes
8 answers

MAPI and managed code experiences?

Using MAPI functions from within managed code is officially unsupported. Apparently, MAPI uses its own memory management and it crashes and burns within managed code (see here and here) All I want to do is launch the default e-mail client with…
Ishmaeel
  • 14,138
  • 9
  • 71
  • 83
13
votes
4 answers

Did P/Invoke environment change in .NET 4.0?

I've started upgrading a .NET 2.0 WinForms application to .NET 4.0. Well, OK, the upgrade process was just a matter of switching platform target, but making it actually work. I assumed that's all there would be to it. But it seems that something…
Bryce Wagner
  • 2,640
  • 1
  • 26
  • 43
13
votes
4 answers

C++ and C# interoperability : P/Invoke vs C++/CLI

In the course of finding a way to interoperate between C# and C++ I found this article that explains about P/Invoke. And I read a lot of articles claiming that C++/CLI is not exact C++ and requires some effort to modify from original C++ code. I…
prosseek
  • 182,215
  • 215
  • 566
  • 871
13
votes
4 answers

Access C global variable 'errno' from C#

Is it possible to access the "errno" variable in C# when P/Invoking? This is similar to Win32 GetLastError().
joemoe
  • 5,734
  • 10
  • 43
  • 60
13
votes
6 answers

Best practices for organizing .NET P/Invoke code to Win32 APIs

I am refactoring a large and complicated code base in .NET that makes heavy use of P/Invoke to Win32 APIs. The structure of the project is not the greatest and I am finding DllImport statements all over the place, very often duplicated for the same…
Paul Sasik
  • 79,492
  • 20
  • 149
  • 189
13
votes
1 answer

Enum Size in Bytes

What is the size of the enum below in bytes? public enum MMTPCnxNckRsn { MMTPCnxNckRsnNoAnswer = -2, MMTPCnxNckRsnSendError = -1, MMTPCnxNckRsnOk = 0, MMTPCnxNckRsnInvalidMember = 1, MMTPCnxNckRsnHubNotReady = 2, …
Paridokht
  • 1,374
  • 6
  • 20
  • 45
13
votes
1 answer

Pinvoke DeviceIoControl parameters

I'm working on a C# project using DeviceIoControl. I've consulted the related Pinvoke.net page for my signature: [DllImport("Kernel32.dll", SetLastError = false, CharSet = CharSet.Auto)] public static extern bool DeviceIoControl( SafeFileHandle…
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
12
votes
2 answers

Feature detection when P/Invoking in C# and .NET

i'm trying to find a good way to detect if a feature exists before P/Invoking. For example calling the native StrCmpLogicalW function: [SuppressUnmanagedCodeSecurity] internal static class SafeNativeMethods { [DllImport("shlwapi.dll", CharSet =…
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219