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

How to use libFLAC to read FLAC tags in c#

Is there an .net c# wrapper for the libFLAC library? If not, how can I read FLAC tags using the libFLAC in a .net framework c# application? If neither, are there other opensource libraries to read flac tags in c#? Thanks!
Marco Bettiolo
  • 5,071
  • 6
  • 30
  • 35
2
votes
1 answer

Compiling Assimp.NET under Mono/Linux

I'm trying to use MasterQ32's OpenWorld.Engine on Ubuntu. I've followed the example scene from the "Documentation". Everything is working well, except asset importing. Interestingly enough, the same error occurs on Windows 7 too: Assimp.NET's…
2
votes
1 answer

Print a BMP file to Printer using command

I need to print a BMP file to USB printer using commands. C++ signature is USB_API BOOL Usb_WritePort(BOOL bUseBulkEndp, LPCVOID lpBuffer, DWORD dwNumberOfBytesToWrite, LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped); This is my C#…
Frebin Francis
  • 1,905
  • 1
  • 11
  • 19
2
votes
2 answers

Can't get SendInput() to work

I'm kind of desperate. I have been trying for hours now, but I just can't get SendInput() to work. To be honest, I can't even get it to be recognized. It always says: Error 1 The type or namespace name 'INPUT' could not be found (are you missing…
Zampano
  • 35
  • 5
2
votes
0 answers

How to unit test P/Invoke mappings?

Assume two related projects. Functionality.dll is a C++ library that contains an Extern "C" API with multiple functions Interop.dll is a C# library that has static public p/invoke declarations to call into the C++ library. I would like to have…
Wilbert
  • 7,251
  • 6
  • 51
  • 91
2
votes
1 answer

Bring up the printer settings dialog and have the changes saved

This is the same as this question, with one remark: We manage to get the printerSettings modified, but how can we save them back as the default printer settings? (the original question does not post/answer this) The code I'm using right…
Ando
  • 11,199
  • 2
  • 30
  • 46
2
votes
1 answer

Interop with unmanaged code in ASP.net vNext

What's the story going to be (if any) around interop with unmanaged code for ASP.net vNext / Core CLR? The key bits (DllImport and friends) appear to be present to allow for unmanaged code interop, but how would things such as packaging and…
jumpinjackie
  • 2,387
  • 4
  • 22
  • 26
2
votes
2 answers

PInvoke or using /clr:pure to compile

I have a set of numerical libraries in C++ and I want to call them interactively in a interpretive language like F# or IronPython. So I have two choices now: Compile the library in native DLL and use PInvoke to call functions in it. Compile the…
Yin Zhu
  • 16,980
  • 13
  • 75
  • 117
2
votes
2 answers

Why aren't there pre-built PInvoke DLLs?

I am building a PowerShell script that uses a few user32.dll functions, and a few gdi32.dll functions. I've had to do this several times in the past. I'm tired of looking at these huge blocks of C# code in a string in my PowerShell code. I am also…
Nacht
  • 3,342
  • 4
  • 26
  • 41
2
votes
1 answer

C# Marshaling an ushort/ulong array

I have a C-DLL + header file and try to p/invoke a function from C#. I also have some example C++ code of how to use the function. Here is the function definition: int GetData(unsigned char* buffer, long bufferSize); The more interesting part is…
Jeremy
  • 316
  • 3
  • 13
2
votes
1 answer

My C++ Unity plugin runs once in the editor but not twice

I have a plugin that calls C++ code. When the game starts, it calls this extern C++ function: void startPlugin() { MyClass::instance = new MyClass(); MyClass::instance->process(); } The process method runs an infinite loop that processes data…
eje211
  • 2,385
  • 3
  • 28
  • 44
2
votes
1 answer

Pinvoke delegates: ensuring lifetime of delegate and NullReferenceException

I've written C# code that passes a delegate to be called by C++ as a function pointer. When the C++ code calls the function pointed to, I get the error: A first chance exception of type 'System.NullReferenceException' occurred in PInvokeTest.exe…
user327301
  • 2,641
  • 2
  • 26
  • 33
2
votes
1 answer

Is there a way to get function pointer for extern method in C#

I want to get the function pointer (ie IntPtr) for an extern method such as: [DllImport("DbgHelp.dll")] static extern void SymFunctionTableAccess64(IntPtr process, ulong addrBase); which, then also be used as a parameter for an extern method such…
zahir
  • 1,298
  • 2
  • 15
  • 35
2
votes
1 answer

C++ API in C# with PInvoke

I have following function written in C++. How to properly declare and call it in C# using PInvoke? SW_ErrCode SW_Connect (const char * server, int timeout, void * tag, SW_SessionID * sh_out) In C# I have following Code: public enum SW_ErrCode …
Kamil
  • 149
  • 1
  • 2
  • 10
2
votes
2 answers

Marshalling array of struct from C# to VC++

I'm trying to marshal an array of structs from C# to VC++. The C++ function looks like this: void myFunc(tFileListEntry* fileList); And tFileListEntry is defined as: typedef struct FILE_LIST { uint16_t FileGroupCounter; uint32_t FileID; …
Robert Hegner
  • 9,014
  • 7
  • 62
  • 98