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

How to P/Invoke when pointers are involved

In an attempt to learn to use PInvoke in C#, I'm a little unsure how to handle various cases with pointers involving simple value types. I'm importing the following two functions from an unmanaged DLL: public int USB4_Initialize(short*…
Will Eddins
  • 13,628
  • 5
  • 51
  • 85
12
votes
3 answers

C/C++ Interoperability Naming Conventions with C#

Consider the following Win32 API struct: typedef struct _SECURITY_ATTRIBUTES { DWORD nLength; LPVOID lpSecurityDescriptor; BOOL bInheritHandle; } SECURITY_ATTRIBUTES, *PSECURITY_ATTRIBUTES, *LPSECURITY_ATTRIBUTES; When porting this object…
Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
12
votes
2 answers

How to correctly define PRINT_NOTIFY_INFO_DATA?

I was playing with a project from codeproject which basically monitors the printing activity on the computer. However it does not work correctly for 64 bit configuration. The below portion of the code was the issue. This code is called whenever…
Jehonathan Thomas
  • 2,110
  • 1
  • 28
  • 34
12
votes
4 answers

Dynamically P/Invoking a DLL

What is the best way to dynamically P/Invoke unmanaged code from .NET? For example, I have a number of unmanaged DLL's with common C-style exports between them. I would like to take the path to a DLL and then P/Invoke a function based on the…
Nick
  • 13,238
  • 17
  • 64
  • 100
11
votes
1 answer

Use a C library from C# code

I have a library in C-language. is it possible to use it in C sharp. http://zbar.sourceforge.net/ is the link of library i want to use
murmansk
  • 845
  • 2
  • 11
  • 28
11
votes
2 answers

Any difference between malloc and Marshal.AllocHGlobal?

I write a module in C# that exports some functions to be used in C. I need to allocate some memory for some structs to be passed between C <-> C#. What I allocate in C I do with malloc, and in C# I do with Marshal.AllocHGlobal() (to allocate…
bzamfir
  • 4,698
  • 10
  • 54
  • 89
11
votes
1 answer

How can I stream data from a managed assembly to a native library and back again?

How can I stream data (text) from a managed assembly to a native library and stream data (text) back to the managed assembly? Specifically, I want to expose a System.IO.Stream of some sort on the .NET side, and (most importantly) a FILE * on the…
Steve
  • 31,144
  • 19
  • 99
  • 122
11
votes
3 answers

How do I marshal a structure as a pointer to a structure?

I am trying to pass a structure from C# into C++ library. I pass structure as an object, and C++ function expects it as a pointer (void *). I am having problem passing the structure. [DllImport("MockVadavLib.dll", CharSet = CharSet.Ansi)] public…
THX-1138
  • 21,316
  • 26
  • 96
  • 160
11
votes
0 answers

Safely use SuppressUnmanagedCodeSecurity

I'm currently creating a managed wrapper to an unmanaged dll. Point is the wrapper does a TON of calls to the unmanaged dll but exports very few methods itself. From the research I did this should be safe but I want to make sure I get this…
user836755
  • 111
  • 1
  • 4
11
votes
2 answers

How can I pass an F# delegate to a P/Invoke method expecting a function pointer?

I'm attempting to set up a low-level keyboard hook using P/Invoke in an F# application. The Win32 function SetWindowsHookEx takes a HOOKPROC for its second argument, which I've represented as a delegate of (int * IntPtr * IntPtr) -> IntPtr, similar…
Ben
  • 6,023
  • 1
  • 25
  • 40
11
votes
3 answers

FindWindowEx from user32.dll is returning a handle of Zero and error code of 127 using dllimport

I need to handle another windows application programatically, searching google I found a sample which handles windows calculator using DLLImport Attribute and importing the user32.dll functions into managed ones in C#. The application is running, I…
teenup
  • 7,459
  • 13
  • 63
  • 122
11
votes
1 answer

P/Invoke, c#: unsigned char losing a byte

Im working towards a dll file for a software's SDK and i'm trying to call a function to get information about the host of the software. there are two unsigned char variables(HostMachineAddress, HostProgramVersion) in the struct the function wants…
Tistatos
  • 641
  • 1
  • 6
  • 16
11
votes
3 answers

How to import void * C API into C#?

Given this C API declaration how would it be imported to C#? int _stdcall z4ctyget(CITY_REC *, void *); I've been able to get this far: [DllImport(@"zip4_w32.dll", CallingConvention = CallingConvention.StdCall, EntryPoint =…
Mike Chess
  • 2,758
  • 5
  • 29
  • 37
11
votes
1 answer

C# how can i pin an object in memory without marshalling the object?

Ok here it goes, i'm using oracle's ContentAccess library in C#, the library is programmed in C. I use some functions of the library to extract text from diffrent files. the c library uses async communication by function pointers (Delegates). i have…
Ronald
  • 1,278
  • 2
  • 10
  • 14
11
votes
5 answers

Is there a complete user32.dll wrapper library available for .NET?

I'm doing alot of interop to user32.dll at the moment through VB.NET. As user32.dll is not at .NET level but at native level, I need to declare the functions using the Declare statement. Although this works great, I keep declaring them over and…
pimvdb
  • 151,816
  • 78
  • 307
  • 352