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
37
votes
1 answer

Getting Symbols from debugged process MainModule

I started writing a debugger in C#, to debug any process on my operating system. For now, it only can handle breakpoints (HW, SW, and Memory), but now I wanted to show the opcode of the process. My first attempt was with nidsasm (NASM), but this is…
Strece
  • 396
  • 2
  • 9
36
votes
7 answers

Should DWORD map to int or uint?

When translating the Windows API (including data types) into P/Invoke, should I replace DWORD with int or uint? It's normally unsigned, but I see people using int everywhere instead (is it just because of the CLS warning? even the .NET Framework…
user541686
  • 205,094
  • 128
  • 528
  • 886
34
votes
5 answers

Unblock File from within .net 4 c#

Is there a possibility to unblock a file that is downloaded from the internet from within a c# program. Surfing the internet I have learned, that the information is written in an alternative stream of a (NTFS) file that contains the current zone…
HCL
  • 36,053
  • 27
  • 163
  • 213
34
votes
5 answers

How to return text from Native (C++) code

I am using Pinvoke for Interoperability between Native(C++) code and Managed(C#) code. What i want to achieve is get some text from native code into my managed code. For this i try lot lot of things,e.g passing string/stringbuilder by ref, using…
Jame
  • 21,150
  • 37
  • 80
  • 107
30
votes
5 answers

No console output when using AllocConsole and target architecture x86

I have a WinForms project, and if the user want's a debug console, I allocate a console with AllocConsole(). All console output works normally with the target architecture set to "Any CPU", but when I change it to "x86" it doesn't output anything…
teamalpha5441
  • 741
  • 1
  • 6
  • 20
29
votes
2 answers

Is there generally a noticeable performance hit when calling PInvoke on Win32 / COM methods?

I'm wondering whether anyone has a decent explanation or overview on the negative aspects of using DLLImport / PInvoke on Win32 methods from managed .Net code? I plan to make use of various Win32 methods and would like to have a greater…
Brian Scott
  • 9,221
  • 6
  • 47
  • 68
29
votes
4 answers

Implementing IDisposable on a sealed class

I don't think this question has been asked before. I'm a bit confused on the best way to implement IDisposable on a sealed class—specifically, a sealed class that does not inherit from a base class. (That is, a "pure sealed class" which is my made…
zebrabox
  • 5,694
  • 1
  • 28
  • 32
28
votes
5 answers

What exactly happens during a "managed-to-native transition"?

I understand that the CLR needs to do marshaling in some cases, but let's say I have: using System.Runtime.InteropServices; using System.Security; [SuppressUnmanagedCodeSecurity] static class Program { [DllImport("kernel32.dll", SetLastError…
user541686
  • 205,094
  • 128
  • 528
  • 886
28
votes
5 answers

Detect laptop lid closure and opening

Is it possible to detect when a laptop's lid is open or closed? From what I've read, this isn't possible, but SO has helped me with the impossible before. The only thing I've found that might be in the right direction is an MSDN blog post about…
Brad
  • 159,648
  • 54
  • 349
  • 530
28
votes
3 answers

Is there any way to debug c++ dll called from C# DllImport?

I wonder if there is any way to debug c++ dll called from C# PInvoke in VS 2010. I tried to attach the project into c# application but it didn't work - didn't stop at a break point. I also tried to record anything with OutputDebugString in C++…
Tae-Sung Shin
  • 20,215
  • 33
  • 138
  • 240
27
votes
6 answers

new IntPtr(0) vs. IntPtr.Zero

Is there any difference between the two statements: IntPtr myPtr = new IntPtr(0); IntPtr myPtr2 = IntPtr.Zero; I have seen many samples that use PInvoke that prefer the first syntax if the myPtr argument is sent by ref to the called function. If…
Yuval Peled
  • 4,988
  • 8
  • 30
  • 36
27
votes
7 answers

How to find that Mutex in C# is acquired?

How can I find from mutex handle in C# that a mutex is acquired? When mutex.WaitOne(timeout) timeouts, it returns false. However, how can I find that from the mutex handle? (Maybe using p/invoke.) UPDATE: public class InterProcessLock :…
TN.
  • 18,874
  • 30
  • 99
  • 157
27
votes
2 answers

How do I pass a const char* to a C function from C#?

I try to call a plain C-function from an external DLL out of my C#-application. This functions is defined as void set_param(const char *data) Now I have some problems using this function: How do I specify this "const" in C#-code? public static…
Elmi
  • 5,899
  • 15
  • 72
  • 143
27
votes
4 answers

CUDA bindings for .net?

I know that there are a lot of CUDA language bindings, such as PyCUDA, but are there any good bindings for .Net? The only one I've seen is this one, but I'd like to know if there are any others.
TraumaPony
  • 10,742
  • 12
  • 54
  • 74
27
votes
3 answers

When passing a managed byte[] array through PInvoke to be filled in by Win32, does it need to be pinned?

Suppose you're calling a Win32 function that will fill in your byte array. You create an array of size 32, empty. Then pass it in to the Win32 function to be filled int, and use it later in your managed code. Does there exist the chance that the…
Leeks and Leaks
  • 1,258
  • 1
  • 16
  • 26