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

Will struct modifications in C# affect unmanaged memory?

My gut reaction is no, because managed and unmanaged memory are distinct, but I'm not sure if the .NET Framework is doing something with Marshaling behind the scenes. What I believe happens is: When getting a struct from my unmanaged DLL, it is the…
userx
  • 3,769
  • 1
  • 23
  • 33
11
votes
3 answers

How to map Win32 types to C# types when using P/Invoke?

I am trying to do something like this in C#. I found out how to call Win32 methods from C# using P/Invoke from this link. However I met some difficulties in implementing P/Invoke. For example, one of the methods that I would like to access is…
Louis Rhys
  • 34,517
  • 56
  • 153
  • 221
11
votes
1 answer

Is the CallingConvention ignored in 64-bit .NET applications?

When interacting with a 64-bit native library through an explicitly 64-bit .NET application via P/Invoke, is the CallingConvention property in the DllImport attribute effectively ignored? I ask this because on "traditional" x86 you have to specify…
Xenoprimate
  • 7,691
  • 15
  • 58
  • 95
11
votes
2 answers

What's the size and alignment of C# fixed bool array in struct?

When doing P/Invoke, it is important to make the data layout match. We can control the layout of struct by using some attribute. For example: struct MyStruct { public bool f; } gives a size of 4. While we can tell compiler to make it a 1 byte…
Xiang Zhang
  • 2,831
  • 20
  • 40
11
votes
2 answers

Getting icon of "modern" Windows app from a desktop application?

I have developed a function which returns the icon of the window for a given window handle. It looks like this. private static BitmapSource GetWindowIcon(IntPtr windowHandle) { var hIcon = default(IntPtr); hIcon = SendMessage(windowHandle,…
Mathias Lykkegaard Lorenzen
  • 15,031
  • 23
  • 100
  • 187
11
votes
1 answer

C# Marshalling bool

Scenario This should be an easy task, but for some reason I can't get it going as intended. I have to marshal a basic C++ struct during a reversed-P/Invoke call (unmanaged calling managed code). The issue only arises when using bool within the…
PuerNoctis
  • 1,364
  • 1
  • 15
  • 34
11
votes
2 answers

How to free IntPtr in C#?

How to free ptrSentFromPinvokedDLL? IntPtr ptrSentFromPinvokedDLL= IntPtr.Zero; int resultFromVendor = CallVendorDll(ref ptrSentFromPinvokedDLL); resultFromVendor = DoMoreWorkFromVendorDLL( ptrSentFromPinvokedDLL, "workonthis"); // Free…
47d_
  • 3,527
  • 5
  • 20
  • 20
11
votes
2 answers

Read other process current directory in C#

I am trying to get current working directory of selected process. I am sure that it is possible, because tools like Process Explorer can show this information. I found out that this information is stored in PEB. Moreover I found Oleksiy blog post:…
Piotr Stapp
  • 19,392
  • 11
  • 68
  • 116
11
votes
5 answers

Stepping into a P/Invoke call in disassemby view

My C# code is calling an unmanaged third-party library function via P/Invoke, and the unmanaged function is having some strange side effects. I want to debug into it and see what it's doing. If I debug my C# code, and try to "Step Into" the P/Invoke…
Joe White
  • 94,807
  • 60
  • 220
  • 330
11
votes
1 answer

PInvoke char* in C DLL handled as String in C#. Issue with null characters

The function in C DLL looks like this: int my_Funct(char* input, char* output); I must call this from C# app. I do this in the following way: ...DllImport stuff... public static extern int my_Funct(string input, string output); The input string is…
user2124150
  • 113
  • 1
  • 4
11
votes
2 answers

Does P/Invoke on 64-bit windows require different signatures than on 32-bit?

When I create a signature that refers to user32.dll for example should I be building this with user64.dll if the target is a 64-bit computer? [DllImport("user32.dll", CharSet = CharSet.Auto)] public static extern bool ChangeClipboardChain( …
Brett Ryan
  • 26,937
  • 30
  • 128
  • 163
11
votes
3 answers

Release unmanaged memory from managed C# with pointer of it

The question in short words is : How to free memory returned from Native DLL as ItrPtr in managed code? Details : Assume we have simple function takes two parameters as OUTPUT, The first one is Reference Pointer to byte array and the second one is…
khaled
  • 216
  • 1
  • 2
  • 8
11
votes
2 answers

How do I marshall a pointer to a pointer of an array of structures?

My C declarations are as follows: int myData(uint myHandle, tchar *dataName, long *Time, uint *maxData, DATASTRUCT **data); typedef struct { byte Rel; __int64 Time; char Validated; unsigned char Data[1]; } DATASTRUCT; My C# declarations…
user1470994
  • 301
  • 1
  • 2
  • 9
11
votes
4 answers

How to make C (P/invoke) code called from C# "Thread-safe"

I have some simple C-code which uses a single global-variable. Obviously this is not thread-safe, so when I call it from multiple threads in C# using P/invoke, things screw up. How can I either import this function separately for each thread, or…
BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283
10
votes
2 answers

How to Call Windows API

Possible Duplicate: Windows API and .net languages I want to call native Windows API from .NET Framework. I want to have an easy way where I can call native API from high-level layer like other .NET APIs. Please refer to any resource which you…
Uzair Anwaar
  • 225
  • 1
  • 4
  • 16