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

Illegal parameter in converting C SDK to C#

I am trying to conver a C SDK to C# and am running into a "Illegal Parameter" error on the conversion of a C function. The details of the C SDK function are listed below #ifndef LLONG #ifdef WIN32 #define LLONG LONG #else //WIN64 #define LLONG…
Bob S
  • 23
  • 3
2
votes
3 answers

How to relate WAVE_MAPPER audio line with its audio device

I'm developing an application that among other things, enumerates all input audio devices (using SetupAPI) and then for each audio device, it lists all input audio lines (using winmm.dll). Basically, the way I'm relating the two is getting the…
Padu Merloti
  • 3,219
  • 3
  • 33
  • 44
2
votes
1 answer

passing an array of structures from C# to C++ using IntPtr

I have a structure --> public struct readers { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1000)] public IntPtr[] tag_list; }; Marshalling has to be done in between "IntPtr[] tag_list" & structure TAG_IN_PARAM …
roheet boss
  • 33
  • 1
  • 6
2
votes
1 answer

How can I capture all application/window messages from start of process?

I am trying to figure out exactly how I can capture all window messages of a process/window, from the time it was launched in c#. The process would not be my own so I would need to use some kind of hook. My goal is to start capturing all messages in…
user1632018
  • 2,485
  • 10
  • 52
  • 87
2
votes
2 answers

Where can I learn more about P/Invoke?

Lately, I've been doing a lot of interaction with unmanaged libraries and I keep coming back to SO to ask questions about certain method signatures because I'm not a C/C++ programmer (although it's not completely alien to me). There are situations…
David Brown
  • 35,411
  • 11
  • 83
  • 132
2
votes
1 answer

P/Invoke from C to C# without knowing size of array

Right know in my code I have structure declared as like this, with fixed this 16, know at compile time. struct CONSOLE_SCREEN_BUFFER_INFOEX { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)] public int ColorTable[]; } but what I need is to be…
inside
  • 3,047
  • 10
  • 49
  • 75
2
votes
1 answer

How to pass string input to console from C#?

I follow the below steps to pass a string input to a console from C#: I allocate a console to Visual Studio Process using AllocConsole() method, by pInvoking Kernel32.dll. I call a Perl process from Visual Studio (C#). The Perl process attaches to…
CSharpLearner
  • 21
  • 2
  • 4
2
votes
0 answers

Convert WAV to FLAC with libFLAC in C#

I've tried to port the libFLAC encoding example to C# and came up with this code: public class LibFLAC { public static void Test() { string inputPath = Path.Combine("D:\\", "_", "test.pcm"); string outputPath =…
2
votes
1 answer

Marshalling LPWSTR * from a C function

I have a sample function from a native code HRESULT getSampleFunctionValue(_Out_ LPWSTR * argument) This function outputs the value in argument. I need to call it from the Managed code [DllImport("MyDLL.dll", EntryPoint =…
Pankaj Parag
  • 437
  • 1
  • 6
  • 17
2
votes
1 answer

Native Integration in Silverlight 4

a question concerning the new concept of Trusted Applications in Silverlight 4: I gather that trusted applications run outside the browser with elevated trust. Will it therefore be possible to call arbitrary functions in unmanaged DLLs (by means of…
user220079
  • 21
  • 2
2
votes
1 answer

Convert LPWSTR * array of string pointers to C#

I'm trying to convert the following method from C++ to C# HRESULT GetDevices( [in, out] LPWSTR *pPnPDeviceIDs, [in, out] DWORD *pcPnPDeviceIDs ); In the MSDN documents it says: pPnPDeviceIDs [in, out] A caller-allocated array of string…
megamania
  • 271
  • 3
  • 9
2
votes
0 answers

Subscribing to system events in C# with P/Invoke in Mono

I have an application in which I need to respond to events such as a user change in the system time (like the Microsoft.Win32.SystemEvents.TimeChanged event), the changing of display settings (SystemEvents.DisplaySettingsChanging and…
Lars
  • 145
  • 2
  • 5
2
votes
1 answer

SHBrowseForFolder on WinCE does not layout correctly

I'm attempting to p/invoke the SHBrowseForFolder API to prompt the user to select a folder and I am seeing the title static / label field float on top of the tree control. The code was downloaded from an article referenced in multiple place on the…
tcarvin
  • 10,715
  • 3
  • 31
  • 52
2
votes
1 answer

Safe and Correct Struct Marshalling

Unmanaged and Managed Memory Regions I am attempting to execute unmanaged code from a C-library. One of the methods takes a void* as a parameter but under the hood it's cast to a struct of type nc_vlen_t C struct for nc_vlen_t /** This is the type…
lukecampbell
  • 14,728
  • 4
  • 34
  • 32
2
votes
2 answers

How to pass an unsigned long to a Linux shared library using P/Invoke

I am using C# in Mono and I'm trying to use pinvoke to call a Linux shared library. The c# call is defined as: [DllImport("libaiousb")] extern static ulong AIOUSB_Init(); The Linux function is defined as follows: unsigned long AIOUSB_Init()…
Jim Young
  • 25
  • 5