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

char array marshaling in C#

I am new to C#/.net programming. I am marshaling the following C# struct from WPF code to a C++ class in an unmanaged C++ dll. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] public struct CallbackParams { …
John Qualis
  • 1,663
  • 4
  • 33
  • 52
2
votes
1 answer

C#: marshall strings to utf8 char*

Background I am trying to write a high level libspotify wrapper based on a modified libspotify.net (http://libspotifydotnet.codeplex.com/). As libspotify.net is just a thin (and completely bugged ... ) pinvoke layer, it does not handle the utf8…
Lukas Rieger
  • 676
  • 10
  • 31
2
votes
2 answers

How to copy a file while it's being written as fast as possible?

TL/DR: I have two machines: A and B. I make a testing program, to test the medium (interface) between them - I check for errors when copying file from A to B and then from B to A, but I must do it the fastest I can. So I have on A source file: SRC,…
Tar
  • 8,529
  • 9
  • 56
  • 127
2
votes
2 answers

How to pass struct with string data in PINVOKE?

I'm stuck by passing struct with string data from C# code to C++ dll. c++ code typedef struct { LPCSTR lpLibFileName; LPCSTR lpProcName; LPVOID pPointer1; LPVOID pPointer2; } ENTITY, *PENTITY, *LPENTITY; extern "C"…
Miles Chen
  • 793
  • 1
  • 10
  • 20
2
votes
2 answers

What C# datatype should I use to interface with unmanaged type "char* &sResult"

I am writing a C# code, and there is a code that needs calling an unmanaged C++ library. The signature in the library's header is like this bool GetValueFromFile(char* sPathToFile, char* &sResult); What signature should I translate this in C#? I…
Louis Rhys
  • 34,517
  • 56
  • 153
  • 221
2
votes
2 answers

Minimize/Restore with hidden caption bar makes windows forms window grow

I hit this problem in Windows Forms, after using PInvoke of SetWindowLongPtr to remove the caption (WS_CAPTION) from a Sizable window. After that whenever the window is either Minimized or Maximized and then restored to Normal it grows (by the…
crpatton
  • 113
  • 5
2
votes
1 answer

Could you safely say this code is "unsafe"

this is the calss that implements native pinvok code Code That seem to work though i could not verify it is using unsafe properly signeture with unsafe struct IO_COUNTERS { public ulong ReadOperationCount; public ulong…
LoneXcoder
  • 2,121
  • 6
  • 38
  • 76
2
votes
1 answer

Windows service performs differently after reboot

I may have a problem in understating the behaviour of windows services or the life itself. THE PROBLEM: The service stopped unexpedetly and no recovery actions fired despite being set. The service stopped after ServiceHelper.ChangeStartMode method…
Peuczynski
  • 4,591
  • 1
  • 19
  • 33
2
votes
1 answer

LoadLibrary doesn't work when compiled to AnyCpu

I am developing a c# application that extract a cab file from a setup package. to do that I am using LoadLibrary. this is the pinvoke C# signature. [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Ansi)] internal static extern IntPtr…
user844541
  • 2,868
  • 5
  • 32
  • 60
2
votes
2 answers

C#: problem loading C++ DLL

In my code, I can load "MessageBoxA" from user32.dll and use it, but if I try to load and use a function from my DLL, I get a crash. My C# code: [DllImport("SimpleDLL.dll")] static extern int mymean(int a, int b, int…
Warpin
  • 6,971
  • 12
  • 51
  • 77
2
votes
2 answers

C# - get event from SetWindowText

We are writing a plugin for an existing VB6 application (via COM interop), and we are requiring some functionality that they do not support. We could easily get the required functionality if we could somehow receive an event for when a particular…
jonathanpeppers
  • 26,115
  • 21
  • 99
  • 182
2
votes
1 answer

Passing a struct containing an int array of unknown size from c# to c and back

I'm starting to struggle with this problem. I've searched and searched for help, and everything i've tried doesn't seem to work. I'm obviously doing something wrong. Anyway - i have a c# structure defined as: public struct…
user2477533
  • 201
  • 1
  • 2
  • 10
2
votes
3 answers

Is there a managed API for kernel32.searchpath?

Is there a managed API for kernel32.searchpath? i.e not using a pinvoke. http://www.pinvoke.net/default.aspx/kernel32.searchpath
Simon
  • 33,714
  • 21
  • 133
  • 202
2
votes
1 answer

Click A MessageBox button programmatically

As the title suggests, I'm trying to simulate a button-click in a MessageBox programmatically. I earlier tried to close the MessageBox by finding its handle via its caption, and applying WM_CLOSE or SC_CLOSE in SendMessage(). However, due to the…
PritishC
  • 508
  • 8
  • 18
2
votes
0 answers

should I just set SuppressUnmanagedCodeSecurity for maximum performance?

Am I correct that if I need maximum performance, and I have trusted library I should just set SuppressUnmanagedCodeSecurity attribute to all Dll imports I have? I.e. convert such import: [DllImport("srl.dll", CharSet = CharSet.Ansi)] private…
Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305