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
18
votes
7 answers

Removing the Title bar of external application using c#

My application starts up another external application. I want to remove the title bar of this external application once it has started. Is this feasible, and if so how would it be done? Based on the comments I am using the working code below //Finds…
Anuya
  • 8,082
  • 49
  • 137
  • 222
18
votes
4 answers

In .NET, how do I Create a Junction in NTFS, as opposed to a Symlink?

I'm trying to create an NTFS Junction. From the cmd line I can do this using the junction.exe tool from sysinternals. The output of a DIR cmd for a junction looks like this: Volume in drive C has no label. Volume Serial Number is C8BC-2EBD …
Cheeso
  • 189,189
  • 101
  • 473
  • 713
18
votes
3 answers

C# calling C function that returns struct with fixed size char array

So, there have been many variants of this question, and after looking at several I still can't figure it out. This is the C code: typedef struct { unsigned long Identifier; char Name[128]; } Frame; Frame GetFrame(int index); This is the C#…
kevin.key
  • 285
  • 1
  • 3
  • 8
17
votes
2 answers

Hooking into Windows message loop in WPF window adds white border on the inside

I am trying to create a WPF window with WindowStyle="None" (for custom buttons and no title) that cannot be resized. Setting ResizeMode to NoResize removes the aero border, which I want to keep. I could set the min/max size properties and be done…
Ed S.
  • 122,712
  • 22
  • 185
  • 265
17
votes
1 answer

int vs IntPtr when you have a handle?

First a background question: In general, what is the difference between int and IntPtr? My guess is that it is an actual object rather than a value like an int or byte is. Assuming that is true: So they are not the same. Yet I see handles…
Vaccano
  • 78,325
  • 149
  • 468
  • 850
17
votes
1 answer

C# not catching unhandled exceptions from unmanaged C++ dll

I've got an unmanaged C++ dll which is being called from a C# app, I'm trying to get the C# app to catch all exceptions so that in the event of the dll failing due to an unmanaged exception then the user will get a half-decent error message (the C#…
daz-fuller
  • 1,191
  • 1
  • 10
  • 18
17
votes
1 answer

Lock Windows workstation programmatically in C#

I ran into this example for locking Windows workstation: using System.Runtime.InteropServices; ... [DllImport("user32.dll", SetLastError = true)] static extern bool LockWorkStation(); ... if (!LockWorkStation()) throw new…
Ron Klein
  • 9,178
  • 9
  • 55
  • 88
17
votes
2 answers

Attach form window to another window in C#

I want to attach a form to another window (of another process). I try to do this by using [DllImport("user32.dll", SetLastError = true)] private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); setParentWindow(myWindowHwnd,…
Horst Walter
  • 13,663
  • 32
  • 126
  • 228
16
votes
2 answers

Step through the "managed to native transition" in Visual Studio?

While trying to answer this question I decided I need to step through the marshalling process by hand, in the debugger view. Unfortunately, Visual Studio seems to jump right over all this interesting code. Here's the P/Invoke call to…
Roman Starkov
  • 59,298
  • 38
  • 251
  • 324
16
votes
3 answers

How do I bring an unmanaged application window to front, and make it the active window for (simulated) user input

I am assuming I need to use pinvoke but I am not sure which function calls are needed. Scenario: a legacy application will be running, I will have Handle for that application. I need to: bring that application to the top (in front of all other…
Gerald Davis
  • 4,541
  • 2
  • 31
  • 47
16
votes
3 answers

C# P/Invoke: Marshalling structures containing function pointers

Sorry for the verbose introduction that follows. I need insight from someone knowing P/Invoke internals better than I do. Here is how I'm marshalling structures containing function pointers from C to C#. I would like to know whether it's the…
Gregory Pakosz
  • 69,011
  • 20
  • 139
  • 164
16
votes
3 answers

Passing a C# class object in and out of a C++ DLL class

I've been working on a prototype code application that runs in C# and uses classes and functions from older C++ code (in the form of an imported DLL). The code requirement is to pass in a class object to the unmanaged C++ DLL (from C#) and have it…
notsodev
  • 1,447
  • 2
  • 13
  • 20
15
votes
2 answers

Hide another app's taskbar button

I would like to be able to hide another application's window from the taskbar, without hiding the window itself. Specifically, I want to have several different Web browsers running, visible, available in the Alt+Tab list, but not taking up space on…
Joe White
  • 94,807
  • 60
  • 220
  • 330
15
votes
6 answers

C# full screen console?

I have seen that Windows can switch to the very basic console interface when updating the video drivers and I have also seen programs like Borland C++ doing this. I'd really like to know how to make this with a console application in C# (or VB.NET…
Vercas
  • 8,931
  • 15
  • 66
  • 106
15
votes
1 answer

C# Native Interop - Why most libraries use LoadLibrary and delegates instead of SetDllDirectory and simple DllImport

There is a great answer on SO about how to set the search directory for DllImport at runtime. Works just fine with two lines of code. However, many open source projects instead use LoadLibrary function. There are "rumors" that calling native methods…
V.B.
  • 6,236
  • 1
  • 33
  • 56