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

How to update the change time of a file from c#?

Files can have a change date. This date is not the same as the last modified date or the last access date. Change date is not visible through the UI or .NET API. There a two Win32 functions GetFileInformationByHandleEx for reading and…
Yavuz
  • 630
  • 6
  • 20
2
votes
0 answers

For some reason finalization of safe handle is killing my application

For some reason finalization of safe handle is killing my application if method that should open handle fails. I have next unmanaged function: extern "C" __declspec(dllexport) HRESULT WINAPI ConnectCommunicationPort(_Out_ PHANDLE hPort) { …
Brans Ds
  • 4,039
  • 35
  • 64
2
votes
1 answer

Handle Pinvoke for 32bit and 64bit DLL

I try to develope an C# Interface for using an USB Hardware Device. I access the API DLL Via PInvoke pattern from the manufacturer. There are two DLLs with the same name. But one is for 32Bit Systems and the other one for 64Bit Systems. I want that…
REMberry
  • 172
  • 1
  • 2
  • 12
2
votes
3 answers

struct remains unaltered after passing by reference into unmanaged C DLL function

I'm writing a wrapper in C# for an unmanaged C DLL. In the DLL I have the following method which returns pointer struct (struct code near end of post): struct zint_symbol *ZBarcode_Create() { struct zint_symbol *symbol = (struct…
user3141031
2
votes
1 answer

Change the minimum size of an external window

is it possible to change the minimum size of an external window and resize it. Suppose the size of the external application window is (400,400) and the minimum size is (200,200), is there any way we can change it to say, (100,100). I have the handle…
Why
  • 626
  • 11
  • 29
2
votes
1 answer

Why is a bad pointer being passed into my native exported methods from C#?

This is my C# code: [DllImport("Tomb.dll")] public static extern unsafe uint InjectManualMap(ulong pId, [MarshalAs(UnmanagedType.LPCStr)]string dllPath); and for whatever reason when I attempt to use my C++ code: extern "C" DllExport…
jimbabwe
  • 23
  • 2
2
votes
1 answer

How is IntPtr Marshalled?

Until recently I thought that marshalling an IntPtr in P/Invoke would just involve a 'blitting' operation; i.e. simply copying the pointer from e.g. C++ to C#. However I was talking to someone recently that mentioned there was more to it than that.…
Xenoprimate
  • 7,691
  • 15
  • 58
  • 95
2
votes
1 answer

Marshalling an array of strucs to call an unmanaged function from C#

I have to call an unmanaged function from C# and must provide an array of coordinates (doubles) to it. How does the marshalling work for this case correctly? On the unmanaged side: typedef struct dPoint3dTag { double x, y, z; } dPoint3d; void…
Tom Torell
  • 51
  • 5
2
votes
2 answers

Blittable Value Types

Here is a list of blittable types. It contains Int32 and Int64. But I don't see just plain "int" on the list. How does C# treat the plain "int" type? Does it just get replaced with Int32 or Int64 depending on the system? Or is there a subtle…
Michael Covelli
  • 2,113
  • 4
  • 27
  • 41
2
votes
1 answer

SuppressUnmanagedCodeSecurity At Class Level

If I add the [SuppressUnmanagedCodeSecurity()] attribute to a class, is it equivalent to adding it to each function in the class?
Michael Covelli
  • 2,113
  • 4
  • 27
  • 41
2
votes
2 answers

ShSetFolderPath works on win7, doesn't on XP

I'm trying to use ShSetFolderPath function in C#. I work on Win7, I've managed to use ShSetKnownFolderPath and it works fine. Since this function is unavaible in WinXP, i tried to invoke ShSetFolderPath. Because i'm not familiar with invoking, I've…
pipboy3k
  • 123
  • 2
  • 10
2
votes
3 answers

Is it possible to get the name of current active application

User can switch active application by Alt+Tab or by clicking on their icons in TaskBar. Is it possible to get the name (or other unique characteristic) of current active application? I want to write a program which collects statistic of the…
Nike
  • 1,297
  • 3
  • 18
  • 21
2
votes
1 answer

Read a specific sector of DVD by Pinvoke in C#

I am reading directly from a disk using C# and pinvoking the kernel32 ReadFile method.i want just read a particular sector for save time but ReadFile read from first to N sector. How can read only own sector with my choice? …
2
votes
3 answers

Hard time using DLL import in C#

Good morning/day/evening! I have recieved dll which has to retrieve Tags from SCADA system (Indusoft Web Studio). It came alongside with VC++ and VB samples that actually work perfectly fine. Currently i need to get those values and show them on web…
IIy333o
  • 33
  • 5
2
votes
1 answer

PInvoke DnsQueryEx Async

I'm new to marshaling and will be happy for any advise. In Windows 8 and Windows Server 2012 we have a new Windows API function: DnsQueryEx It allows DNS queries to be made asynchronously. Using C#, I am trying to call it in order to receive MX…