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
1 answer

Pinvoke NtOpenFile and NtQueryEaFile in order to read NTFS Extended Attributes in C#

I am trying to code a simple NTFS reader function for Extended Attributes (not Alternate Data Streams !) in C#. It will be used in some powershell scripting later, so i need to strick with C#. So far i've gathered some infos about NtOpenFile: …
Usul
  • 129
  • 8
2
votes
2 answers

Get the cluster size of a disk in C#--Get error on 'GetDiskFreeSpace'

I am trying to get the cluster size of a disk in C#. Everything I have found says to use "GetFreeDiskSpace," but I can't get it to work. It appears as if I am missing a using or something. When I Google the The name 'GetDiskFreeSpace' does not…
Tornado726
  • 352
  • 1
  • 7
  • 16
2
votes
1 answer

PInvoke DllExport: structure marshaling failure

I have some problem with marshalling and PInvoke I have to develop some kind of driver for existing native application (Oracle Siebel CRM, call center integration interface). Sources of the application is black box from my point of view. Here is the…
2
votes
2 answers

DLLImport c++ functions with char* as input or as output parameters

I have two c++ functions that I want to DllImport: bool SendString(const char* pSendStr, long strSize); bool ReadString(char* pReadStr, long& readStrSize); There are a lot of articles that write how to DllImport strings. Alas quite often I see…
Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116
2
votes
1 answer

C# pinvoke marshaling unions

I got some problems translating a C Union to C#. Here's the definition of the Union: union Info { char varChar[8]; short varShort[4]; int varInteger[2]; float varFloat[2]; double varDouble; …
Jeremy
  • 316
  • 3
  • 13
2
votes
1 answer

BadImageFormatException in c#. Expected to contain manifest

I have been testing out some few things with VS2013 Native Tools Command Prompt. So far, I couldn't get my code to load dlls that I made. Here is my dll code, written in c. (Based on msdn example) int __declspec(dllexport) SampleMethod(int…
2
votes
2 answers

How get monitor's friendly name with winapi?

I use Win32_DesktopMonitor class of wmi. But this not return monitor name. But when i use Everest(Aida64) this show me it. I think this app's work with winapi. I find method GetMonitorInfo, but not understand how use it to get monitor…
mehelta
  • 89
  • 1
  • 10
2
votes
1 answer

Using a C library in .NET web application

I have the source code for a C library that I need to use with my C# ASP.Net application. It sounds like the way to handle this would be to create a C++ dll wrapper and DLLImport that into my C# project. I have found this to be kind of ugly as far…
flerngobot
  • 616
  • 1
  • 9
  • 30
2
votes
2 answers

Convert C++ union of structs to C#

I am trying to use C# to use a C++ library that includes a struct that is documented as being laid out like this: struct { long outervar; long othervar; union { struct { long a; } firststruct; struct…
bmprovost
  • 23
  • 1
  • 3
2
votes
3 answers

Do we really need to catch exception for P/Invoke methods?

I am using P/Invoke methods in my .NET application. As the functions are C++ functions; each one has return type like int, intptr or any struct. The return type is enough to tell me if the function was successful or not. Do I still need to catch…
Ram
  • 11,404
  • 15
  • 62
  • 93
2
votes
3 answers

Alternative native api for Process.Start

Ok this is not duplicate of "Alternative to Process.Start()" because my question is something different here. I need to run a process and wait till execution of process and get the output of console. There is way to set RedirectStandardOutput and…
Akash Kava
  • 39,066
  • 20
  • 121
  • 167
2
votes
2 answers

Call function inside another process which is not written in .NET

I want to call a function inside another process and send more then 1 argument through createremotethread. Now, I could do that by sending inline asm but I don't know enough assembly in order to do it that way. Also I don't have any access to the…
basd bfnsa
  • 81
  • 1
  • 10
2
votes
1 answer

PInvokeStackImbalance when calling Delphi dll function

A (non-COM) Delphi dll has a function being exported: function GetQuestions(Digit1, Digit2: string; CountryISO: string):string; I have added this dll as an existing item in Visual Studio 2012 and have set its Build Action to None, Copy to Output…
Wim Ombelets
  • 5,097
  • 3
  • 39
  • 55
2
votes
2 answers

Do I need 2 different PInvokes to Get and Set mouse speed?

I want to get the current mouse pointer speed, and I want to set it. To get it, I use Public Declare Function SystemParametersInfo Lib "user32.dll" Alias "SystemParametersInfoA" (ByVal uAction As Int32, ByVal uParam As Int32, ByRef lpvParam As…
tmighty
  • 10,734
  • 21
  • 104
  • 218
2
votes
1 answer

Keeping PInvoked method alive

Here's my C code: LIBRARY_API bool __cdecl Initialize(void (*FirstScanForDevicesDoneFunc)(void)); And here's C# PINvoke code to work with this DLL: [UnmanagedFunctionPointer(CallingConvention.Cdecl)] public delegate void…
Mike G
  • 185
  • 5
  • 17