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

LookupAccountSid taking +600 ms per call

I'm using GetTokenInformation with the TokenGroups flags to retrieve all the groups that a particular token is part of. I'm then looping through each of the returned SIDs and using LookupAccountSid to grab the user name and domain. I call…
Adam Driscoll
  • 9,395
  • 9
  • 61
  • 104
2
votes
5 answers

.NET WinForms: How to use an API call that requires a window handle?

Short version How do i use an API call when i cannot guarantee that the window handle will remain valid? i can guarantee that i'm holding a reference to my form (so the form is not being disposed). That doesn't guarantee that the form's handle…
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
2
votes
0 answers

Calling a Matlab DLL function from C# via P/Invoke does not give expected output

I wrote a simple matlab function that concatenates two strings: function [z] = testCat(x,y) z=strcat(x,y); end I used the following command to create a DLL out of it: mcc -B csharedlib:libtestCat testCat This is what the signature of the function…
2
votes
1 answer

void ** handles with P/Invoke

I am using a C API DLL from a 3rd party vendor. The problem I have is that I can't seem to find a good template for Marshalling the following C code: API_Open( void ** handle ); API_Close( void * handle ); The calls are simplified, but the handle…
Brian
  • 53
  • 5
2
votes
1 answer

How to correctly call P/Invoke methods in a class library?

I have multiple projects in a Visual Studio 2015 solution. Several of these projects do P/Invokes like: [DllImport("IpHlpApi.dll")] [return: MarshalAs(UnmanagedType.U4)] public static extern int GetIpNetTable(IntPtr pIpNetTable,…
Alan Wayne
  • 5,122
  • 10
  • 52
  • 95
2
votes
1 answer

P/Invoke vkGetPhysicalDeviceFeatures Access Violation

I'm working on a Vulkan wrapper for C# (like I'm sure many people are) and I'm having a bit of a problem with vkGetPhysicalDeviceFeatures it either doesn't return data, or throws Access Violations Unanaged Side - Signature: The signature from the…
klyd
  • 3,939
  • 3
  • 24
  • 34
2
votes
2 answers

NUnit unit test cannot find a unmanaged dll in the test class

I'have the following situation: I have a delphi dll (unmanaged) that works. I have a win forms application (a proof of concept application) that works. The dll (and all its dependencies) are copied in the Bin/Debug directory of the…
Nikola Stjelja
  • 3,659
  • 9
  • 37
  • 45
2
votes
3 answers

C# call to a C DLL is only partly functional

I am learning C# from my C++/CLR background by rewriting a sample C++/CLR project in C#. The project is a simple GUI (using Visual Studio/ Windows Forms) that performs calls to a DLL written in C (in fact, in NI LabWindows/CVI but this is just ANSI…
IKO
  • 23
  • 5
2
votes
2 answers

Is there a table of the matching types for marshaling in P/Invoke & InterOP?

I am almost buried by how the different dialects of types are matched between .NET world and native world. Such as MFC CList and other stuffs. I am desperately hoping for this: Some kind of table or cheetsheet that lists all the mappings between…
smwikipedia
  • 61,609
  • 92
  • 309
  • 482
2
votes
3 answers

BadImageFormatException: PInvoke ImportDll with hdf5dll.dll

Ok, I have the HDF5 library downloaded from the official site, and I have a few DLLs, including hdf5dll.dll, and hdf5_hldll.dll. I have what I think to be some wrappers around the native calls, in my classes H5, H5LT, H5F, and H5T. Example from…
maxwellb
  • 13,366
  • 2
  • 25
  • 35
2
votes
1 answer

Safe Handles when marshaling Win32 structs (PROCESS_INFORMATION)

I am in the process of converting my Win32 p/invoke code to use SafeHandle classes instead of the typical IntPtr handles. While everything works pretty well in DllImport method signatures, I cannot for the life of me get them to work when marshaling…
Erik
  • 12,730
  • 5
  • 36
  • 42
2
votes
2 answers

How do I get the copyright property of the entry application

My problem is this: I am writing a dll in C#, but it is Com Visible, so it can be referenced by a .NET application or used as a COM component by a non-.NET application (Forms, VB6, etc. etc.). I want to get at the properties of the calling…
Dave
  • 3,429
  • 2
  • 26
  • 29
2
votes
1 answer

Extend another application's title bar

I'm working on an open-source .NET clone (GitHub) of DeskPins by Elias Fotinis (direct download off Google Drive). Its main function is to make other windows always-on-top. When they are on top, a pin icon is added to the title bar, which looks like…
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
2
votes
1 answer

In .NET CF 2.0, will a global keyboard hook interfere with P/Invokes that require a keypress?

My details: custom mobile device running Windows CE 4.2, Compact Framework 2.0 SP1. C# app making decent use of P/Invokes with no problems until now. I've written a low-level keyboard hook (similar to, but not identical to, this CodeProject post)…
RJ Cantrell
  • 161
  • 3
  • 20
2
votes
1 answer

Find javascript alert window in IE process

I have a multithreaded UI test tool that starts up instances of Internet Explorer. I would like to find a javascript alert box using the PInvoke API. Finding it globally works fine: IntPtr globalAlertHwnd = Pinvoke.FindWindow("#32770", "Message from…
Johan
  • 35,120
  • 54
  • 178
  • 293