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

Identifying if a user is in the local administrators group

My Problem I'm using PInvoked Windows API functions to verify if a user is part of the local administrators group. I'm utilizing GetCurrentProcess, OpenProcessToken, GetTokenInformationand LookupAccountSid to verify if the user is a local admin.…
Adam Driscoll
  • 9,395
  • 9
  • 61
  • 104
2
votes
1 answer

C# Struct to delphi packed record

I have a function extracted from a delphi dll function func_name(par1: Byte; par2: struct1) : boolean Struct 1 is made up of : struct1 = packed record Number: Byte; arrStr : array[1..8] of struct2; end Moreover struct2 is made up…
Combinu
  • 882
  • 2
  • 10
  • 31
2
votes
1 answer

AccessViolationException occured using P/Invoke with Media Foundation Interface in Multithread application

I'm using the P/Invoke in C# to call native function from C++ DLL as below: C++ DLL: extern "C" { // Function: Create Wmv video from sequences image. Codec: WMV3 (VC-1) __declspec(dllexport) bool __stdcall CreateWMV(...) …
Khai Nguyen
  • 103
  • 1
  • 7
2
votes
1 answer

What's the "right" way to get Win32 p/Invoke declarations?

I typically use the site http://www.pinvoke.net/ to grab a DllImport declaration whenever I need to call a Win32 API, and I've noticed it's the de facto standard response on Stack Overflow to API interop questions. Is this what "everyone" does? Is…
Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
2
votes
1 answer

Pinning char[] on P/Invoke call

I have object pool of char buffers and passing this buffer on P/Invoke call. Do i need pinning buffer before call or not? First approach: [DllImport("Name", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] private static…
SulNR
  • 823
  • 7
  • 14
2
votes
1 answer

cannot return delphi array in C# array

I want to use delphi array function in C#. My delphi code: TIntegerArray = array of integer; function Testarray(): TIntegerArray stdcall; export; var res: TIntegerArray2; begin SetLength(res, 10); res[5] := 55; Result := res; end; …
Ali Yousefi
  • 2,355
  • 2
  • 32
  • 47
2
votes
2 answers

Program doesn't work outside Visual Studio

I use unmanaged dll with P/Invoke in this app, and I always tested it inside Visual Studio (with debug mode on x86 CPU because the dlls are only x86), and it works just fine. But when I just start the exe manually after some time (probably at the…
András Geiszl
  • 966
  • 2
  • 16
  • 36
2
votes
2 answers

Marshal structure pointer in VS2010

I have to call a C++ DLL from my C# program. I'm trying to do it using PInvoke - everything works fine in VS2005\ 2008, but after migration to VS 2010, I get this exception: PInvokeStackImbalance was detected Message: A call to PInvoke function …
rkellerm
  • 5,362
  • 8
  • 58
  • 95
2
votes
5 answers

Communicating with all network computers regardless of IP address

I'm interested in finding a way to enumerate all accessible devices on the local network, regardless of their IP address. For example, in a 192.168.1.X network, if there is a computer with a 10.0.0.X IP address plugged into the network, I want to…
Stephen Jennings
  • 12,494
  • 5
  • 47
  • 66
2
votes
1 answer

Is there any alternative to Java's JNA library for C#?

JNA has a great collection of native functions, constants and structures. I need a similar library for C#. But I can not find anything after a lot of searching. Does anybody know any alternative for JNA library in C#?
Dipu
  • 6,999
  • 4
  • 31
  • 48
2
votes
2 answers

Advice on whether to use native C++ DLL or not: PINVOKE & Marshaling?

What's the best way to do this....? I have some Native C++ code that uses a lot of Win32 calls together with byte buffers (allocated using HeapAlloc). I'd like to extend the code and make a C# GUI...and maybe later use a basic Win32 GUI (for use…
Bob
  • 403
  • 1
  • 4
  • 6
2
votes
0 answers

How do i marshal a class with another class inside for using in PInvoke

I wanted to marshal a struct in C++ to use it in C#. It contains nested classes inside. when I debug I get the data except for the nested classes. I am novice in custom marshalling but would be glad if someone can help me on how to get going. I have…
2
votes
1 answer

PInvoke on SafeArray of AnsiBStr

I have an old C++ DLL exporting functions with ASCII string output arguments. I managed to use the first one like this : [DllImport("MyLibrary.dll")] private static extern int GetVersion( [Out, MarshalAs(UnmanagedType.AnsiBStr)] out string…
Tolokoban
  • 2,297
  • 14
  • 17
2
votes
1 answer

IndexOutOfRangeException - Cannot see call stack using PInvoke

I'm developing a C# app that takes data from a SerialPort, then it uses a C++ project (that I cannot change) to compute the read data. The C++ project is using some native C code, that will call C# functions when the data are computed. This is some…
Alessio
  • 2,018
  • 25
  • 26
2
votes
1 answer

How can I pass a reference parameter (&) (not pointer) using p/invoke in C#?

I have a C++ API prototype void Func(int& size); How can I translate it to P/Invoke in C#? From what I know, if I use public static extern Func(ref int size); , the function will receive a pointer to the int instead of the value.
Randy Sugianto 'Yuku'
  • 71,383
  • 57
  • 178
  • 228