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
22
votes
2 answers

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…
Ope
  • 777
  • 1
  • 7
  • 13
21
votes
1 answer

How do I handle null or optional DLL struct parameters

How do I deal with optional struct arguments in dll methods called from C# using pinvoke? For example, the lpSecurityAttributes parameter here should be passed null when absent. The correct way of passing struct's seems to be using ref, but it…
River
  • 8,585
  • 14
  • 54
  • 67
21
votes
1 answer

P/Invoke in Mono

What's the current status of Mono's Platform Invoke implementation on Linux and on Solaris?
Dmitry Shechtman
  • 6,548
  • 5
  • 26
  • 25
21
votes
2 answers

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…
Winner
  • 253
  • 1
  • 2
  • 6
21
votes
3 answers

Unable to find an entry point when calling C++ dll in C#

I am trying to learn P/Invoke, so I created a simple dll in C++ KingFucs.h: namespace KingFuncs { class KingFuncs { public: static __declspec(dllexport) int GiveMeNumber(int i); }; } KingFuns.cpp: #include…
King Chan
  • 4,212
  • 10
  • 46
  • 78
20
votes
2 answers

P-Invoke in .net core with Linux

Is there way to implement P/Invoke (dllimport) in .NET Core on Linux ? Example : I have C++ MyLib.dll compiled with .net framework. If it is possible to use like this or it's not support to call native win api with linux using .net-core…
Elshan
  • 7,339
  • 4
  • 71
  • 106
20
votes
2 answers

Correct way to marshal SIZE_T*?

I have the following C++ function definition, which I am trying to call through PInvoke from managed code: bool FooBar(SIZE_T* arg1); My managed declaration looked as follows: [DllImport("mydll", SetLastError=true, CharSet=CharSet.Unicode)] private…
sooniln
  • 14,607
  • 4
  • 29
  • 35
20
votes
1 answer

Can I use SafeHandle instead of IntPtr?

I've searched the internet far and wide but didn't find a good explanation. My question is pretty simple. I have a DLL which has a function called Initialize and one of the parameters is a pointer that will receive a handle to be used with…
Davio
  • 4,609
  • 2
  • 31
  • 58
19
votes
6 answers

Capture screen on server desktop session

I have developed a GUI test framework that does integrationtesting of our company website on a scheduled basis. When something fails, it'll take a screenshot of the desktop, among other things. This runs unattended on a logged in user on a dedicated…
Silas Hansen
  • 1,669
  • 2
  • 17
  • 23
19
votes
2 answers

Synchronizing 2 processes using interprocess synchronizations objects - Mutex or AutoResetEvent

Consider the following scenario: I'm running my application which, during its execution, has to run another process and only after that 2nd process finishes inner specific initialization, can my first process continue. E.g: ... // Process1 code does…
VitalyB
  • 12,397
  • 9
  • 72
  • 94
19
votes
5 answers

Check if a DLL is present in the system

quick question. I want to find out if a DLL is present in the system where my application is executing. Is this possible in C#? (in a way that would work on ALL Windows OS?) For DLL i mean a non-.NET classic dll (a Win32 dll) (Basically I want to…
feal87
  • 927
  • 3
  • 11
  • 29
19
votes
2 answers

C# - Convert unsafe byte* to byte[]

I have an unsafe byte* pointing to a native byte array of known length. How can I convert it to byte[]? An unsafe sbyte* pointing to a zero-terminated native string can be converted to a C# string easily, because there is a conversion constructor…
kol
  • 27,881
  • 12
  • 83
  • 120
18
votes
2 answers

Why is the handling of exceptions from CloseHandle different between .NET 4 and 3.5?

I'm encountering a situation where a PInvoke call to CloseHandle is throwing an SEHException in a .NET 4 application when run under a debugger. Unlike others who have encountered similar issues migrating from 3.5 to 4, I'm not particularly bothered…
jeffora
  • 4,009
  • 2
  • 25
  • 38
18
votes
1 answer

Understanding difference between use of fixed{}, Marshal.AllocHGlobal()and GCHandle.Alloc()

Let me start by saying I've looked and found descriptions of the use of fixed{}, Marshal.AllocHGlobal()and GCHandle.Alloc() throughout this forum and in many links on the web. However, I have yet to find a concise explanation of when to use the…
Hyped
  • 183
  • 1
  • 4
18
votes
2 answers

Retrieve Window Size without Windows Shadows

I'm trying to capture desktop windows in C# based on Window handles. I'm using .NET and using PInvoke to GetWindowRect() to capture the window rectangle. I've got the Window selection and rectangle capture working fine. However, the window…
Rick Strahl
  • 17,302
  • 14
  • 89
  • 134