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
15
votes
3 answers

How can I ignore a field when marshalling a structure with P/Invoke

I want to marshal a structure for use with P/Invoke, but this struct contains a field that is only relevant to my managed code, so I don't want it to be marshaled since it doesn't belong in the native structure. Is it even possible ? I was looking…
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
15
votes
2 answers

How to call a C++ API from C#

I have a pretty big system implemented in C++ I need to interact with. The system has a pretty big API, a number of C++ DLLs. These DLLs export C++ classes, as opposed to a nice C style API. and I need to use them from a new C# project. From what I…
AK_
  • 7,981
  • 7
  • 46
  • 78
15
votes
1 answer

How to marshal a variable sized array of structs? C# and C++ interop help

I have the following C++ structs struct InnerStruct { int A; int B; }; struct OuterStruct { int numberStructs; InnerStruct* innerStructs; }; And a C++ function OuterStruct getStructs(); How can I marshal this to C#? Where the C#…
DevDevDev
  • 5,107
  • 7
  • 55
  • 87
14
votes
3 answers

CreateProcessAsUser Creating Window in Active Session

I am using CreateProcessAsUser from a windows service (please can we stay on-topic and assume I have a very good reason for doing this). Contrary to what everyone else is asking here I am getting a window in my active terminal session (session 1)…
Jonathan Dickinson
  • 9,050
  • 1
  • 37
  • 60
14
votes
4 answers

.NET equivalent of size_t

I have a piece of .NET code which I want to port to 64-bit. The codes basically is a set of P/Invoke calls to some other C dll. One of the functions in the C dll has a parameter 'size_t'. What datatype should I use in my P/Invoke signature so that…
Niranjan U
  • 195
  • 2
  • 6
14
votes
1 answer

Calling GetGUIThreadInfo via P/Invoke

I want to send keyboard input to a window in another process, without bringing that window to the foreground. I can use PostMessage to fake the WM_KEYDOWN and WM_KEYUP; all I need to know is which window handle should receive the keyboard input --…
Joe White
  • 94,807
  • 60
  • 220
  • 330
14
votes
6 answers

Is there an enumeration for system error codes in .Net framework?

I have a library function that returns GetLastError codes (things like these). I need to compare them with specific errors, like ERROR_INVALID_HANDLE. However I don't feel comfortable to define the constants myself. So the question is, is there a…
Todd Li
  • 3,209
  • 21
  • 19
14
votes
2 answers

PInvoke for GetLogicalProcessorInformation Function

I want to call via c#/PInvoke the GetLogicalProcessorInformation function, but I'm stuck with SYSTEM_LOGICAL_PROCESSOR_INFORMATION struct and CACHE_DESCRIPTOR struct. How should I define these structs for correct usage? Main problems: 1.…
VMAtm
  • 27,943
  • 17
  • 79
  • 125
14
votes
2 answers

PInvoke C#: Function takes pointer to function as argument

I'd like to access this function in my c# code, is this possible? so in the end the c++ code would call my function and also apply the struct called "sFrameofData". C++ Code: //The user supplied function will be called whenever a frame of data…
Tistatos
  • 641
  • 1
  • 6
  • 16
14
votes
2 answers

Get active window text (and send more text to it)

I'm creating a small utility in C#, that will add some text to an active textbox when a global hotkey is pressed, it's a type of auto complete function. I have my global hotkey working, but now I don't know how to get the current text in the active…
Einar Egilsson
  • 3,438
  • 9
  • 36
  • 47
14
votes
2 answers

How is the CLR faster than me when calling Windows API

I tested different ways of generating a timestamp when I found something surprising (to me). Calling Windows's GetSystemTimeAsFileTime using P/Invoke is about 3x slower than calling DateTime.UtcNow that internally uses the CLR's wrapper for the same…
i3arnon
  • 113,022
  • 33
  • 324
  • 344
14
votes
2 answers

Entry Point Not Found Exception

I'm trying to use a C++ unmanaged dll in a C# project and I'm getting an error when trying to call a function that says that entry point cannot be found. public class Program { static void Main(string[] args) { IntPtr testIntPtr =…
Robert
  • 6,086
  • 19
  • 59
  • 84
14
votes
2 answers

How do I pinvoke to GetWindowLongPtr and SetWindowLongPtr on 32-bit platforms?

I want to P/Invoke to GetWindowLongPtr and SetWindowLongPtr, and I'm seeing conflicting information about them. Some sources say that, on 32-bit platforms, GetWindowLongPtr is just a preprocessor macro that calls GetWindowLong, and GetWindowLongPtr…
Joe White
  • 94,807
  • 60
  • 220
  • 330
14
votes
3 answers

Exposing an ISO C++ class to C#

I need to expose some C++ classes to C# (I am building on Linux, using mono, so COM is not an option) The evidence I have gathered so far suggests that the best way to approach this is: Write a wrapper C++.Net class around the ISO C++ class Consume…
Stick it to THE MAN
  • 5,621
  • 17
  • 77
  • 93
14
votes
4 answers

P/Invoke dynamic DLL search path

I have an existing app which P/Invokes to a DLL residing in the same directory as the app itself. Now (due to the fact that Canon produces one of the crappiest API's around) I need to support two versions of this API and determine at run-time which…
Dan Byström
  • 9,067
  • 5
  • 38
  • 68