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

PInvoke NetLocalGroupGetMembers runs into FatalExecutionEngineError

I need to use win32 NetLocalGroupGetMembers in C#. I found and tested three solutions. All three fail with an FatalExecutionEngineError. The framework is .net 4.0 Here is a full example: Reference to the api: static class NetworkAPI { …
marsh-wiggle
  • 2,508
  • 3
  • 35
  • 52
2
votes
0 answers

Hook minimize event of third party application

I have following hook class: public sealed class Hook : IDisposable { public delegate void Win32Event(IntPtr hWnd); #region Windows API private const uint WINEVENT_OUTOFCONTEXT = 0x0000; [DllImport("User32.dll", SetLastError =…
Alex Zhukovskiy
  • 9,565
  • 11
  • 75
  • 151
2
votes
2 answers

Set the window state of a hidden window

Time ago I asked this question, it was solved here: Unhide process by its process name? But now, and for unknown reason, the C# or Vb.Net code provided there is not working, and I don't understand why not. I did some modifications to the original…
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
2
votes
1 answer

Passing a C# callback function into managed and unmanaged C++ libraries

I am looking to create a C# program that calls a managed C++ dll, which then executes functionality in a native C++ dll. As part of this call, I want to provide a C# callback function that could be invoked by the native C++ dll. The C# code would…
user5202879
2
votes
2 answers

free allocated memory when interoping c++

populating an array of stuct containing string, i have tested an found out that in c# it performed faster via a pointer : struct name{ int intv; IntPtr strv; } when implementing via GetPacksPtr():(see code/signatures below) that's how i…
Jbob Johan
  • 221
  • 1
  • 7
2
votes
1 answer

How to set up C# structures for WlanHostedNetworkSetProperty function

From the Microsoft Documentation: DWORD WINAPI WlanHostedNetworkSetProperty( _In_ HANDLE hClientHandle, _In_ WLAN_HOSTED_NETWORK_OPCODE OpCode, _In_ DWORD dwDataSize, _In_ …
TsSkTo
  • 1,412
  • 13
  • 25
2
votes
0 answers

How to control UI of external application using WPF without knowing its structure and without moving the mouse?

Imagine there is an application that has a window with a fixed size, where all controls remain at exactly known places (yes, there are such applications). Now imagine that said application's window is quite small and almost unusable on a…
Spontifixus
  • 6,570
  • 9
  • 45
  • 63
2
votes
1 answer

Using Unicode strings in DllImport with a DLL written in Rust

I am trying to call a DLL written in Rust from a C# program. The DLL has two simple functions that take stings (in different manner) and prints to the console. Rust DLL code #![crate_type = "lib"] extern crate libc; use libc::{c_char}; use…
2
votes
0 answers

C# how to get the IP address from the sockaddr

I hook the connect function using deviare , and C#. the issue is I need to get the IP Address from the sockaddr(which is the second param in the connect function) . I checked the answers: C# sockaddr to sockaddr_in …
2
votes
1 answer

PinvokeStackImbalance error (C#)

I'm a bit of a newbie to Pinvoke. I have a large project that calls makes a call to a DLL called vtmail.dll. It worked fine in .Net 3.5 but on upgrading to .NET 4.0 I am getting a PinvokeStackImbalance exception The calling code is a bit like…
komodosp
  • 3,316
  • 2
  • 30
  • 59
2
votes
1 answer

PinvokeStackImbalance occured. This exception is thrown but i have no idea what the error is?

The exception: Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'D:\UI_still_in_progress\user_interface\bin\Debug\WindowsFormsApplication1.vshost.exe'. Basically my application has a background real time process in c++…
Raps
  • 65
  • 3
2
votes
1 answer

OpenProcess failed to open existing process via id (Last Error code: 1008)?

I'm trying to use OpenProcess to open all existing processes via the process ID. But somehow it works just the first call, while the next calls show that it does not work with an error code reported as 1008 (An attempt was made to reference a token…
Hopeless
  • 4,397
  • 5
  • 37
  • 64
2
votes
3 answers

How to check whether application is running fullscreen mode on any screen?

I'd like to check, if any screen hosts application in fullscreen mode. I have solution only for one screen which is code copied from here: [WPF] [C#] How-to : Detect if another application is running in full screen mode. This solution is based on …
Fka
  • 6,044
  • 5
  • 42
  • 60
2
votes
2 answers

How to P/Invoke CryptUIWizExport Function using .NET

Can anyone translate these two cryptui.dll functions/structures into C#.NET [dllimport] wrappers? I would like to P/Invoke the CryptUIWizExport function to display the Windows Certificate Export Wizard. In particular, I need to pass a .NET…
MdbUK
  • 23
  • 4
2
votes
3 answers

passing float pointer as IntPtr (pinvoke)

Ok I have a DLL function declared as: [DllImport(mDllName, EntryPoint = "SetParam")] public static extern bool setParam(string param_name, ref IntPtr param_value); This function takes many different params i.e in C++ this is how you would use…
rukiman
  • 597
  • 10
  • 32