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

FreeLibrary throws AccessViolationException after using GetModuleFileName (x64 platform)?

I totally have no idea how this could happen. I'm trying to get the actual full name of an executable file (the cmd.exe) using GetModuleFileName. Debugging shows that GetModuleFileName outputs the correct path but right at the call to FreeLibrary,…
Hopeless
  • 4,397
  • 5
  • 37
  • 64
2
votes
1 answer

Error: Access denied when using the function 'CreateVirtualDisk' in WIN7

Right now I am experimenting with the CreateVirtualDisk function to create a VHD from both physical disk and virtual disk. I have got stuck here for one week because of the error: Access is denied when I tried to create a VHD from a physical disk. I…
2
votes
0 answers

C# Z Order of All Open Windows

I am interested in getting the Z order for every window currently open by the user. For example, if they had 4 windows on top of each other, I'd like to know (by window title - which I can already get), the order - i.e. which is deepest in the stack…
Jonathan
  • 147
  • 1
  • 2
  • 10
2
votes
1 answer

Creating an Activation Context with CreateActCtx Win32 API

I am trying to create an activation context using the CreateActCtx Win32 API. There is not a lot of code out there on the internet where this function is used, but I was able to find two blogs that talk about this after a lot of googling and got…
2
votes
1 answer

DllImport wrong encoding

I'm having trouble using Dll-Imported win api function to work correctly, and it's probably related to the way strings are encoded. Actually I'm trying to use CreateProcess from kernel32.dll. It's imported the following way: …
John-Philip
  • 3,392
  • 2
  • 23
  • 52
2
votes
1 answer

Marshalling complex struct from C++ to C#

For a few days now I have been trying to Marshal a complex struct from C++ to C#, basically I have managed to get most of what I am trying to achieve done but now I'm stuck trying to marshal what I believe is a list. In example I will include what I…
Joachim
  • 360
  • 2
  • 12
2
votes
1 answer

Different behavior when marshaling structures in 32 bit than in 64 bit runtimes

I discovered this when I was PInvoking SetupDiCreateDeviceInfoList. The C++ function signature is: HDEVINFO SetupDiCreateDeviceInfoList( _In_opt_ const GUID *ClassGuid, _In_opt_ HWND hwndParent ); In C# I have defined the GUID structure…
Chris
  • 1,213
  • 1
  • 21
  • 38
2
votes
1 answer

Why does this Marshaling work in Mono but fail on MS?

I've boiled this down to a simple example. I am writing some interop code between C# and C, and I have the following very simple struct on the unmanaged side: typedef struct { bool create_if_missing; fdb_custom_cmp_variable custom_cmp; }…
borrrden
  • 33,256
  • 8
  • 74
  • 109
2
votes
2 answers

Convert intptr to ulong array

I am calling a method from C# like this: [DllImport(@"pHash.dll", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr ph_dct_videohash(string file, ref int length); And here is the method I am calling from the library ulong64*…
2
votes
1 answer

C# Marshall void* that is set to NULL

Trying to write a wrapper for a C DLL. The C Native signature is: BOOL WINAPI PBM_OpenCard (DWORD* CardHandle, TCHAR* CardName, void* Module, DWORD ModuleId, WORD ShareFlags, WORD LoadFlags) To which documentation says: "Module set to NULL…
Azriel H
  • 45
  • 4
2
votes
2 answers

AccessViolationException when using PInvoke on functions with parameters

I am currently using PInvoke to call some unmanaged functions from C++ in C#; specifically from PhysX 3.3.3. I am relatively new to PInvoke so I started with some simple methods. I can easily call functions that take no parameters but am having…
Dyn0myte
  • 650
  • 1
  • 5
  • 11
2
votes
0 answers

What is the practice to follow when need to expose a WinAPI enumeration?

I would like to know which is the proper practice to follow when need to expose a WinAPI enumeration. ( I think this question affects both C# and VB.Net because just its a member visibility question ) I have a NativeMethods Class as indicates MSDN…
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
2
votes
1 answer

Explain what problems could have this function (if any)

SCENARIO When P/Invoking, I thinked it could be a great idea to simplify/reduct tons of code by designing a generic function that calls the function, then it checks for the GetLastWin32Error I'm using this code: ''' ''' Invokes the…
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
2
votes
1 answer

Get the method name that was passed through a lambda expression?

Is it possible to retrieve via Reflection the real method name that was passed through a lambda expression? I would like to platform invoke some functions with a better error-handling, then to avoid repeating tons of Marshal.GetLastWin32Error…
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
2
votes
1 answer

When inheriting from SafeHandle, should we reapply the ReliabilityContract attribute?

In the .Net security blog article on SafeHandles, it mentions that you need to apply the ReliabilityContract attribute to the signature of the native method that closes the handle. When we inherit from SafeHandle we have to declare a constructor,…
jo0ls
  • 389
  • 1
  • 9