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

using dllimport

I'm trying to use the DllImport attribute in the following code: [DllImport("grfinger.dll",EntryPoint="_grstartenroll@4")] public static extern int startenroll(int context); to get the function name I used dumpbin /export. When I run the code I get…
sagi
  • 21
  • 2
2
votes
1 answer

PinvokeStackImbalance calling comctl32 With CallingConvention.Cdecl

We've just upgraded our projects from .NET 3.5SP1 to .NET 4.5 and now we're getting the PInvokeStackImbalance MDA on a call to DllGetVersion from comctl32. I've read around and understand that the Framework is no longer resilient to incorrect…
2
votes
1 answer

How does one pass nullptr through P/Invoke declared with a StringBuilder?

Consider a typical P/Invoke declaration like this: [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool LookupAccountName( string SystemName, string…
Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
2
votes
1 answer

Send CTRL_C/SIGINT to a process from C#

I would like to interrupt a command running through cmd.exe. In the code below, I am using ping www.stackoverflow.com -t as an example. public void Run() { System.Diagnostics.ProcessStartInfo si = new…
Leon
  • 1,141
  • 13
  • 25
2
votes
2 answers

Is making my clickonce app partial-trust worth it?

I'm about to start working on a ClickOnce app targeted at internal customers for use on the organization's intranet. I want to ease the setup process, so I thought that developing the app to be partially trusted would be a good idea, but now I'm…
Greg D
  • 43,259
  • 14
  • 84
  • 117
2
votes
1 answer

Calling methods in third-party DLLs

I'm using C# and P-Invoke to get access to objects in the Qt framework (http://qt.digia.com/). I don't seem to be having trouble using functions that return simple types (or void), but whenever I try to use a function that returns an object, the…
2
votes
1 answer

PInvoke error when calling unmanaged (C++) function from C#

I have an unmanged C++ dll that I have written and tested. The unmanged code woks fine when built and run in an unmanged console app. The function declaration is shown below. #ifndef IMPORT_MYLIB # define MYLIB_API __declspec(dllexport) #else # …
nalyd88
  • 4,940
  • 8
  • 35
  • 51
2
votes
0 answers

Enable a display device and set resolution using Pinvoke

I am able to successfully change the resolution on the devices got through EnumDisplayDevices which are active but which devices are not active I can not set their resolution. Is there any way to activate/enable the disabled display devices…
Ali
  • 51
  • 1
  • 5
2
votes
1 answer

Invoke a C DLL function to marshal a string from a type defined char

This is my first attempt at creating a C# wrapper to a C lib. The SDK is 3rd party and outside my control: sdk_defines.h #ifndef SDK_CHAR_T #define SDK_CHAR_T typedef char sdk_char_t; #endif /* SDK_CHAR_T */ #ifndef SDK_CSTR_T #define…
mbursill
  • 2,911
  • 1
  • 32
  • 44
2
votes
3 answers

Should I pin my array before calling GetSystemWow64Directory

I am trying to use the GetSystemWow64Direcory method from my application. The sample from PInvoke.net seems incorrect to me. Don't I have to pin the the array before passing to the unmanaged call? [DllImport("Kernel32.dll")] public static extern…
pstrjds
  • 16,840
  • 6
  • 52
  • 61
2
votes
2 answers

Wrapping a static unmanaged library in an instance class

I'm using an unmanaged library (written in C++) in my code (C#). All the methods provided by the library are static. I use P/Invoke to communicate with the library. Here's the way the library works: There's an initialization method that you need to…
hattenn
  • 4,371
  • 9
  • 41
  • 80
2
votes
1 answer

Interrupting native threads

I am currently investigating how Thread.Interrupt plays together with P/Invoke or native calls. I have read in MSDN that it is not possible to abort (Thread.Abort) a thread which is in the native call (other use cases might apply as well). But I did…
ovanes
  • 5,483
  • 2
  • 34
  • 60
2
votes
0 answers

Do arrays get pinned before they are sent to unmanaged code?

Let's say I'd like to call an unmanaged C++ function from C#. C++ function has the following prototype: int doSomething(int arraySize, int* array) And I call the function from C# as following: [DllImport(MyLibrary, CallingConvention =…
hattenn
  • 4,371
  • 9
  • 41
  • 80
2
votes
1 answer

How to marshal an object returned by a C# delegate before returning it as a value to C++

I have this code in C++ class MyClass { ... }; typedef MyClass (*Callback)(); Callback theCB; static void RegisterCallback( Callback cb ) { theCB = cb; }; static void CallCallback() { MyClass obj = theCB(); } I am using swig but for simplicity…
Éric
  • 181
  • 2
  • 15
2
votes
1 answer

Correct use of SafeHandles in this P/Invoke use case

Working in C# with a native Dll, that uses opaque handles and internal reference counting, I have the following P/Invoke signatures (all decorated with DllImport attribute) [DllImport("somedll.dll"] public extern IntPtr getHandleOfA(IntPtr…
Tom Davies
  • 2,386
  • 3
  • 27
  • 44