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

Passing a C# callback function through Interop/pinvoke

I am writing a C# application which uses Interop services to access functions in a native C++ DLL. I am already using about 10 different functions which are working. Now I am not sure how to handle passing a callback as a parameter so that the DLL…
Trevor Elliott
  • 11,292
  • 11
  • 63
  • 102
26
votes
2 answers

A call to PInvoke function has unbalanced the stack. This is likely because the managed PInvoke .. (.NET 4)

My project run successful without errors in .NET Frame work 3.5. But, When I target it to .NET Frame work 4. I got the error: "A call to PInvoke function has unbalanced the stack. This is likely because the managed PInvoke signature does not match…
taibc
  • 897
  • 2
  • 15
  • 39
26
votes
2 answers

C# PInvoking user32.dll on a 64 bit system

Is it wrong to pinvoke user32.dll on 64 bit Windows, from a 64 bit app? I've done this successfully a number of times and never had an error, but it seems contradictory. Should I look for user64.dll instead?
James Cadd
  • 12,136
  • 30
  • 85
  • 134
25
votes
12 answers

AttachConsole(-1), but Console.WriteLine won't output to parent command prompt?

If I have set my program to be a Windows Application, and used the AttachConsole(-1) API, how do I get Console.WriteLine to write to the console I launched the application from? It isn't working for me. In case it is relevant, I'm using Windows 7…
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
25
votes
3 answers

What is the difference between pInvoke and COM Interop?

Let us say that I am accessing a third-party library, for which the documentation states that I can use pInvoke or create an interop library and use COM. What is the difference between these two techniques, and why might I choose one over the other?
Grant
  • 11,138
  • 32
  • 94
  • 140
25
votes
2 answers

Marshal "char *" in C#

Given the following C function in a DLL: char * GetDir(char* path ); How would you P/Invoke this function into C# and marshal the char * properly. .NET seems to know how to do LPCTSTR but when I can't figure out any marshaling that doesn't cause a…
Adam Haile
  • 30,705
  • 58
  • 191
  • 286
24
votes
5 answers

Why can't I return a char* string from C++ to C# in a Release build?

I'm attempting to call the following trivial C function from C#: SIMPLEDLL_API const char* ReturnString() { return "Returning a static string!"; } With the following P/Invoke declaration (with or without the return attribute, it makes no…
Qwertie
  • 16,354
  • 20
  • 105
  • 148
23
votes
4 answers

Get StartAddress of win32 thread from another process

Background: I've written a multi-threaded application in Win32, which I start from C# code using Process class from System.Diagnostics namespace. Now, in the C# code, I want to get the name/symbol of the start address of each thread created in the…
Nawaz
  • 353,942
  • 115
  • 666
  • 851
23
votes
1 answer

Moving mouse cursor programmatically

To start out I found this code at http://swigartconsulting.blogs.com/tech_blender/2005/08/how_to_move_the.html: public class Win32 { [DllImport("User32.Dll")] public static extern long SetCursorPos(int x, int y); …
Beaker
  • 2,804
  • 5
  • 33
  • 54
23
votes
1 answer

Changing a C# delegate's calling convention to CDECL

I have had this problem with C# when I was using DotNet1.1 The problem is this. I have an unmanaged dll, which has a function which takes a function pointer (among other arguments). When I declare the DLLImport in C# code, I pass a delegate. But the…
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
23
votes
1 answer

What is the difference between [In, Out] and ref when using pinvoke in C#?

Is there a difference between using [In, Out] and just using ref when passing parameters from C# to C++? I've found a couple different SO posts, and some stuff from MSDN as well that comes close to my question but doesn't quite answer it. My guess…
PerryC
  • 1,233
  • 2
  • 12
  • 28
22
votes
4 answers

Why is 'IntPtr.size' 4 on Windows 64 bit?

I think I should get 8 when I use IntPtr.Size. However, I still get 4 on a 64-bit machine with Windows 7 x64. Why?
Adam Lee
  • 24,710
  • 51
  • 156
  • 236
22
votes
3 answers

Is this PInvoke code correct and reliable?

In this question I have searched for a simple solution to unblock files. Thanks to all the comments and answer, I have found a simple solution by PInvoking DeleteFile. It works, but because I've never used file-operations through PInvoke (Win32), I…
HCL
  • 36,053
  • 27
  • 163
  • 213
22
votes
2 answers

Why do I get "PInvokeStackImbalance was detected" for this simple example?

I'm creating a very simple PInvoke sample: extern "C" __declspec(dllexport) int Add(int a, int b) { return a + b; } [DllImport("CommonNativeLib.dll")] extern public static int Add(int a, int b); return NativeMethods.Add(a, b); But whenever I…
Justin
  • 84,773
  • 49
  • 224
  • 367
22
votes
3 answers

How can I pass a pointer to an array using p/invoke in C#?

Example C API signature: void Func(unsigned char* bytes); In C, when I want to pass a pointer to an array, I can do: unsigned char* bytes = new unsigned char[1000]; Func(bytes); // call How do I translate the above API to P/Invoke such that I can…
Randy Sugianto 'Yuku'
  • 71,383
  • 57
  • 178
  • 228