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

Is it possible for an object to "pin" itself and avoid garbage collection?

I'm trying to write a drop-in replacement for System.Media.SoundPlayer using the waveOut... API. This API makes a callback to a method in my version of SoundPlayer when the file/stream passed it has completed playback. If I create a form-scoped…
MusiGenesis
  • 74,184
  • 40
  • 190
  • 334
2
votes
1 answer

P/Invoke on 32 bit and 64 bit systems

Let us pick the following Win API call as an example: BOOL MessageBeep(UINT uType); // from User32.dll The input parameter is UINT to specify the beep type, which is can be both 32bit and 64bit integer, depending on which Windows version we call it…
user256890
  • 3,396
  • 5
  • 28
  • 45
2
votes
2 answers

How to define unmanaged dll dependency in C#

I want to link an unmanaged C++ library to a C# app. I am using the PInvoke process because the unmanaged C++ dll has multiple dependencies that won't compile with CLR. When I compile the example code below, I am getting the following errors. I…
Myca A.
  • 81
  • 7
2
votes
1 answer

How to utilize the Windows Vista/7 Dialogs?

I have noticed that the Windows Vista/7 dialogs look MUCH better than just a MessageBox. How can I use them with a P/Invoke?
Mohit Deshpande
  • 53,877
  • 76
  • 193
  • 251
2
votes
3 answers

Is there any event triggered when a new window is added to the desktop

I want to know whether there is any event triggered when a new window appears/comes on the desktop. I am open to using COM,WMI,WinApis, UIAutomation or any other method but the language of choice is C#. The actual requirement: A process has 1 main…
Why
  • 626
  • 11
  • 29
2
votes
0 answers

Serial port: Polling vs Event-Driven approach

I am using Win32 API (Overlapped IO) for serial port communication. I have used PInvoke to call the Win32 API from my C# code. In my previous implementation, the receiver thread reads data by using Polling mechanism (Infinite while loop reads RX…
petchirajan
  • 4,152
  • 1
  • 18
  • 20
2
votes
2 answers

Enumerating Network Sessions

I wanted to pull data about the connected network users from the Computer Management -> Shared Folders -> Sessions tab into my c# application. Can anybody guide me on what namespaces have to be used along with some sample code to import username and…
ricky2002
  • 281
  • 2
  • 6
  • 12
2
votes
2 answers

Dealing With Odd FieldOffset

I'm trying port the following C++ struct to C#. #pragma pack(push,1) struct FatBootSectorStruct { UCHAR BS_jmpBoot[3]; // 0 UCHAR BS_OEMName[8]; // 3 USHORT BPB_BytsPerSec; // 11 UCHAR BPB_SecPerClus; …
SameOldNick
  • 2,397
  • 24
  • 33
2
votes
2 answers

Marshalling/DllImport of Binary/Hex Data from C++ to C#

I have a C++ Header File that gives me access to two functions that, after cutting stuff that is not necessary, are this: extern __declspec (dllimport) bool __cdecl GetBinary(unsigned short* _allocatedBufferSizeBufferLength, char*…
private_meta
  • 561
  • 4
  • 19
2
votes
0 answers

Can safearrays take, and marshall, user defined types?

I would like to move fairly complex types from the managed to native world and visa versa. Currently this is being done by creating multidimensional safearrays, which has the advantage that marshalling is done for you, but means we end up with…
Ben
  • 1,106
  • 1
  • 9
  • 17
2
votes
1 answer

How to get the icon associated with a specific folder?

I was required in one of my projects to get the icon for specific folders from their paths. For example: If I use C:\Users\Username\Desktop I want to get the icon associated with the Desktop folder If I use the path of a folder that has a custom…
vbtheory
  • 373
  • 3
  • 17
2
votes
1 answer

FindResource not functioning with Bitmaps

(source: khason.net) var ID = 1234; var FilePath = "C:\\file.dll"; IntPtr hMod = LoadLibraryEx(FilePath, IntPtr.Zero, 2); //LOAD_LIBRARY_AS_DATAFILE = 2 IntPtr hRes = FindResource(hMod, "#" + ID, "PNG"); byte[] Bytes = new…
Elmo
  • 6,409
  • 16
  • 72
  • 140
2
votes
1 answer

Call stdlib.h functions from C#

For different purposes I need to provide an interface that implementents the following functions in C# malloc calloc realloc free But I would prefer to not to code it myself (why reinvent the wheel). A better solution would be when my…
Mr. JWolf
  • 1,375
  • 2
  • 14
  • 33
2
votes
2 answers

C# pInvoke with long in both 32 & 64 Bit Linux:

I need to call the Linux-function sysinfo It's declaration is int sysinfo(struct sysinfo *info); with Until Linux 2.3.16, sysinfo() used to return information in the following structure: struct sysinfo { long uptime; /* Seconds…
Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442
2
votes
3 answers

P/Invoke and Mono: EntryPointNotFoundException

I'm trying to access the Wine implementation of some user32 functions on Kubuntu Linux. I have the Wine 1.1.31 package installed. When try running this simple test program in MonoDevelop, I get a System.EntryPointNotFoundException. using…
Zach Johnson
  • 23,678
  • 6
  • 69
  • 86