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
12
votes
4 answers

Hiding an application from the taskbar

I have been struggling to hide another application from the taskbar from my application. I have been using the SetWindowLong function in order to set/remove WS_EX_APPWINDOW on the extended style. I have tried both setting and removing the property…
René Sackers
  • 2,395
  • 4
  • 24
  • 43
12
votes
2 answers

Calling UNIX and Linux shared object file .so from c#

Is there a way for a Shared Object file written in C and built on Unix to be called from C# P/Invoke? Or do I need to use Java or something like that?
Natasha Thapa
  • 979
  • 4
  • 20
  • 41
12
votes
3 answers

How to dynamically load and unload a native DLL file?

I have a buggy third-party DLL files that, after some time of execution, starts throwing the access violation exceptions. When that happens I want to reload that DLL file. How do I do that?
Poma
  • 8,174
  • 18
  • 82
  • 144
12
votes
4 answers

Returning a string from PInvoke?

I am using PInvoke for interoperability between Native Code (C++) and Managed Code (C#). I just write a simple function which gets a string from C++ code. My code looks like C# Code: [DllImport("MyDll.dll")] private static extern string…
Jame
  • 21,150
  • 37
  • 80
  • 107
12
votes
2 answers

Move window when external application's window moves

I've got an always on-top application (basically a status display) that I want to follow around another program and always sit just to the left of the minimize button. I can get the Rect representing the "target" process using the following code…
ScottishTapWater
  • 3,656
  • 4
  • 38
  • 81
12
votes
2 answers

Show Authentication dialog in C# for windows Vista/7

I want to get network login credentials from a user. I'm using .NET 3.5 with C#. Up until now I used the CredUIPromptForCredentials call (a very useful link on how to use it can be found here) My problem is that the CredUIPromptForCredentials API…
Rubinsh
  • 4,883
  • 10
  • 34
  • 41
12
votes
6 answers

How to get monitor EDID in OS X?

I'm looking to pull the EDID information in OS X / macOS? It looks like it's stored in the IORegistry. Is there a way to access it with the current monomac libraries? Can I do it with standard interop or do I need to write a custom shim? It looks…
Joel Barsotti
  • 3,031
  • 7
  • 35
  • 59
12
votes
1 answer

SetProcessDpiAwareness not having effect

I've been trying to disable the DPI awareness on a ClickOnce application. I quickly found out, it is not possible to specify it in the manifest, because ClickOnce does not support asm.v3 in the manifest file. The next option I found was calling the…
René Sackers
  • 2,395
  • 4
  • 24
  • 43
12
votes
1 answer

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…
Daniel Stutzbach
  • 74,198
  • 17
  • 88
  • 77
12
votes
1 answer

How to marshal a C++ enum in C#

I need to create a wrapper between C++ and C#. I have a function very similar to this: virtual SOMEINTERFACE* MethodName(ATTRIBUTE_TYPE attribType = ATTRIBUTE_TYPE::ATTRIB_STANDARD) = 0; The enum is declared like this: enum class ATTRIBUTE_TYPE { …
Rock3rRullz
  • 357
  • 1
  • 4
  • 21
12
votes
1 answer

Reading Dependency walker output

I am having some problems using one of the Dlls in my application and I ran dependency walker on it. i am not sure how to read it but I got following results Does it suggest any x86-x64 incompatibilty? is there anyway I can solve this issue? Error:…
Lost
  • 12,007
  • 32
  • 121
  • 193
12
votes
4 answers

Can't find PInvoke DLL error in Windows Mobile

I am having a lot of trouble getting a basic scenario to work on windows mobile 5.0 emulator. I have a winforms app that eventually calls into native code. Deployment works fine and all the native DLLs are copied in the same folder as the winforms…
Dilip
  • 213
  • 2
  • 3
  • 9
12
votes
3 answers

How does DllImport really work?

I like to understand how DllImport really works. I need a plain English explanation- meaning simple explanation. Does it statically link with the exported method from the DLL, like an "include file" directive/static library? Or does it dynamically…
Tom Silverman
  • 207
  • 3
  • 5
12
votes
1 answer

Are P/Invoke [In, Out] attributes optional for marshaling arrays?

Assume that there is a native function with a pure-C interface like the following one, exported from a native DLL: // NativeDll.cpp extern "C" void __stdcall FillArray( int fillValue, int count, int* data) { // Assume parameters…
Mr.C64
  • 41,637
  • 14
  • 86
  • 162
12
votes
2 answers

Performance differences between P/Invoke and C++ Wrappers

In the process of learning P/Invoke, I asked this previous question: How to P/Invoke when pointers are involved However, I don't quite understand the implications of using P/Invoke in C# over creating a wrapper in Managed C++. Creating the same…
Will Eddins
  • 13,628
  • 5
  • 51
  • 85