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

How do I tell when the default printer was changed?

I am looking for a way to detect when the default printer was changed on an RDP session. Basically we are trying to record the users last default printer and I feel the easiest way to perform this task is to detect when the user changed printers as…
Steven Combs
  • 1,890
  • 6
  • 29
  • 54
2
votes
1 answer

Direct memory access of C++ DLL in C#

I have searched stackoverflow for this problem but didn't find this exact one. In particular, I've found a ton of questions regarding retrieving C++ string references (char**) when the C# side is managing the memory but not the other way around. The…
poljpocket
  • 318
  • 1
  • 10
2
votes
2 answers

Import C++ DLL in C#, function parameters

I'm trying to import my C++ Dll in C#. It seems to work fine for functions without parameters but i'm having issues with my function which has some. My C++ function : __declspec(dllexport) bool SetValue(const std::string& strV, bool bUpload) { …
user2088807
  • 1,378
  • 2
  • 25
  • 47
2
votes
1 answer

Creating dll library from generated c code

I have a control problem which i solved by using Model predictive control (MPC). I have stated the problem in MATLAB and used FORCES http://forces.ethz.ch/ to solve it. FORCES is a web service from ETH Zürich which generates library-free ANSI-C…
2
votes
1 answer

Why is the deprecated "rsvg_pixbuf_from_file_at_size" faster/more efficient that the non-deprecated method (Cairo)?

I'm using C# and P/Invoke to access the GDK libraries. My goal is to convert an set of SVG files into raster images (specifically, png), and using the GDK libraries seems to be the most reliable/accurate. After doing some reading of the Gnome/Cairo…
2
votes
2 answers

SHGetFileInfo: Description for a files extension too short

I'm using SHGetFileInfo to retrieve certain information about a file or directory, i.e. the icon or the description the file extension. When retrieving the description of the file extension, the string returned from SHGetFileInfo is missing the…
KeyNone
  • 8,745
  • 4
  • 34
  • 51
2
votes
1 answer

How can I use P/Invoke to call a 64-bit C++ DLL from a 64-bit C# Application?

I'm working on some code that involves using P/Invoke to call unmanaged functions from a few C++ DLLs. I'd like to be able to build the application as either 32 or 64 bit. Currently, it only works as x86. I have 32 and 64 bit copies of each of the…
2
votes
3 answers

Why won't my solution work to P/Invoke NotifyServiceStatusChange in C#?

I am trying to P/Invoke the NotifyServiceStatusChange event in C# to check when a service has stopped. I managed to get it to compile and run without any errors, but now, when I stop the service, it doesn't seem to want to notify that its dead. Any…
Alexandru
  • 12,264
  • 17
  • 113
  • 208
2
votes
1 answer

Using .NET structures with WINAPI functions

Let's say I want to invoke GetWindowRect functions. I though that I must create new struct that would represent RECT, but this also works: [DllImport("user32.dll")] public static extern bool GetWindowRect(System.IntPtr hWnd, out Rectangle…
xx77aBs
  • 4,678
  • 9
  • 53
  • 77
2
votes
1 answer

Troubleshooting PInvokeStackImbalance with a third party DLL

I am having difficulty troubleshooting a PInvokeStackImbalance exception that occurs when I attempt to PInvoke a third party DLL. Based on what I describe below, can someone please suggest next steps for troubleshooting? I have spent some time…
CDitchman
  • 65
  • 5
2
votes
1 answer

Calling c dll function which changes passed argument in C#

I'm tyring to pass a C# string to C dll function which supposes to encrypt it. Unfortunately, it does simply nothing. After calling the function, the string is still the same. C function: #include #include extern…
sliwkacz
  • 387
  • 2
  • 4
  • 18
2
votes
1 answer

How to use IOCTL_SCSI_MINIPORT via DeviceIoControl from C#.net?

My task is to implement a reliable solution to retrieve the serial number of the hard drive. Unfortunately the WMI method isn't reliable at all. So I'm looking for another solution. I've found this small piece of software, which does exactly that…
Robin
  • 8,162
  • 7
  • 56
  • 101
2
votes
1 answer

pinvoke return a float

have been working on this for hours, couldn't get it work :( below code gives exception "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." this is fixed, see below update but when i change the…
Xin
  • 575
  • 4
  • 20
2
votes
2 answers

Determining DLLImport arguments and safely calling an unmanaged C function

As a follow-up to my previous question, I finally got the C dll exported and usable in C#, but I'm stuck trying to figure out the proper argument types and calling method. I've researched here on SO but there doesn't seem to be a pattern to how…
Tigress
  • 353
  • 1
  • 3
  • 5
2
votes
3 answers

how to pass data from c# to c++ (just pointer, no marshalling)

My application contains two sides - client side on c++ and server side on c#. I need client side to receive structure with lowest possible latency. So I want it to receive a pointer to a structure and avoid marshaling. Structure is something like…
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305