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
541
votes
26 answers

"An attempt was made to load a program with an incorrect format" even when the platforms are the same

I'm calling functions from a 32-bit unmanaged DLL on a 64-bit system. What I get is: BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) At first, I had my projects set to…
David Brown
  • 35,411
  • 11
  • 83
  • 132
241
votes
20 answers

Bring a window to the front in WPF

How can I bring my WPF application to the front of the desktop? So far I've tried: SwitchToThisWindow(new WindowInteropHelper(Application.Current.MainWindow).Handle, true); SetWindowPos(new…
Factor Mystic
  • 26,279
  • 16
  • 79
  • 95
154
votes
2 answers

Google Chrome accessible tree cache issue with UI Automation

Google Chrome does not refresh accessibility elements (AutomationElement) when a user scrolls down in the browser. To reproduce it: Enable renderer accessibility with : "chrome --force-render-accessibility" or by setting on Global Accessibility…
Perfect28
  • 11,089
  • 3
  • 25
  • 45
152
votes
20 answers

Unable to load DLL (Module could not be found HRESULT: 0x8007007E)

I have a dll library with unmanaged C++ API code I need to use in my .NET 4.0 application. But every method I try to load my dll I get an error: Unable to load DLL 'MyOwn.dll': The specified module could not be found. (Exception from HRESULT:…
Ingimar Andresson
  • 1,865
  • 3
  • 15
  • 20
108
votes
11 answers

How to deal with files with a name longer than 259 characters?

I'm working on an application which walks through every file in some directories and does some actions with those files. Among others, I must retrieve the file size and the date when this file was modified. Some file full names (directory + file…
Arseni Mourzenko
  • 50,338
  • 35
  • 112
  • 199
100
votes
7 answers

How to get parent process in .NET in managed way

I was looking a lot for method to get parent process in .NET, but found only P/Invoke way.
abatishchev
  • 98,240
  • 88
  • 296
  • 433
94
votes
8 answers

How to remove minimize and maximize buttons from a resizable window?

WPF doesn't provide the ability to have a window that allows resize but doesn't have maximize or minimize buttons. I'd like to able to make such a window so I can have resizable dialog boxes. I'm aware the solution will mean using pinvoke but I'm…
Nidonocu
  • 12,476
  • 7
  • 42
  • 43
83
votes
5 answers

pinvokestackimbalance -- how can I fix this or turn it off?

I just switched to vs2010 from vs2008. Exact same solution, except now every single call to a C++ dll yields a 'pinvokestackimbalance' exception. This exception does not get fired in 2008. I have complete access to the C++ dll and to the calling…
mmr
  • 14,781
  • 29
  • 95
  • 145
81
votes
6 answers

using a class defined in a c++ dll in c# code

I have a dll that was written in c++, I need to use this dll in my c# code. After searching I found that using P/Invoke would give me access to the function I need, but these functions are defined with in a class and use non-static private member…
Dan Vogel
  • 3,858
  • 7
  • 41
  • 57
79
votes
1 answer

What could cause P/Invoke arguments to be out of order when passed?

This is a problem that happens specifically on the ARM, not on x86 or x64. I had this problem reported by a user and was able to reproduce it using UWP on Raspberry Pi 2 via Windows IoT. I've seen this kind of problem before with mismatched…
borrrden
  • 33,256
  • 8
  • 74
  • 109
77
votes
10 answers

Using a 32bit or 64bit dll in C# DllImport

Here is the situation, I'm using a C based dll in my dot.net application. There are 2 dlls, one is 32bit called MyDll32.dll and the other is a 64bit version called MyDll64.dll. There is a static variable holding the DLL file name: string…
Gilad
  • 2,876
  • 5
  • 29
  • 40
70
votes
2 answers

Why are Cdecl calls often mismatched in the "standard" P/Invoke Convention?

I am working on a rather large codebase in which C++ functionality is P/Invoked from C#. There are many calls in our codebase such as... C++: extern "C" int __stdcall InvokedFunction(int); With a corresponding C#: [DllImport("CPlusPlus.dll",…
Kadaj Nakamura
  • 923
  • 1
  • 10
  • 24
66
votes
4 answers

How do I call C++/CLI from C#?

I have a class implemented in C++ that's responsible for the arithmetic computation of the program, and an interface using WPF. I process the input with C# but then how can I use my C++ class? I've seen some comments about making a managed C++…
master chief
  • 771
  • 1
  • 7
  • 8
64
votes
2 answers

Passing strings from C# to C++ DLL and back -- minimal example

I am trying to make the absolute simplest minimal example of how to pass strings to and from a C++ DLL in C#. My C++ looks like this: using std::string; extern "C" { string concat(string a, string b){ return a + b; } } With a…
asutherland
  • 2,849
  • 4
  • 32
  • 50
63
votes
7 answers

C# performance - Using unsafe pointers instead of IntPtr and Marshal

Question I'm porting a C application into C#. The C app calls lots of functions from a 3rd-party DLL, so I wrote P/Invoke wrappers for these functions in C#. Some of these C functions allocate data which I have to use in the C# app, so I used…
kol
  • 27,881
  • 12
  • 83
  • 120
1
2 3
99 100