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 to P/Invoke a function that allocates and returns pointer to new array

I have a C function that I would like to call from C# program. Function compresses inputted byte array and outputs new compressed array. It looks like this: extern __declspec(dllexport) int Compress(chandle handle, unsigned char *inputBuf, unsigned…
zigzag
  • 579
  • 5
  • 17
2
votes
1 answer

How to diagnose a corrupted suffix pattern in a mixed managed/unmanaged x32 .NET application

I've got a .NET application that pinvokes several libraries, all 32 bit (the application is 32 bit as well). I recently started getting crash bugs that occurred when the GC started freeing memory, and when I attached I saw that it was an access…
ianschol
  • 586
  • 2
  • 13
2
votes
3 answers

Pointer math in C#

I am trying to use some pinvoke code to call a C function. The function fills a buffer with data. The structure is set up as a DWORD for the length, followed by a string. How do I extract the string from the IntPtr? IntPtr buffer =…
Adam Tegen
  • 25,378
  • 33
  • 125
  • 153
2
votes
3 answers

C# Code hangs after Pinvoke SendMessage

I am automating test procedures for a separate Windows Form application. I am currently using pinvoke to communicate with the other application. In this application, there is a button which creates a new pop up window when clicked. I am…
bocaj006
  • 23
  • 3
2
votes
2 answers

PlaySound WinCE

I need to play sound when click on button. I found this dll which is in c++. So I use p invoke, but an error pop up: Error 2 The best overloaded method match for 'WinCE.Sound.PlaySound(string, System.IntPtr, int)' has some invalid arguments…
franzp
  • 109
  • 1
  • 2
  • 15
2
votes
1 answer

Limitations of C# to native interop

I know that C# allows interoperability with native code using PInvoke (An Overview of Managed/Unmanaged Code Interoperability) We are planning to develop new code, and are considering 2 options: Native solution + managed (C#) wrapper around interop…
lysergic-acid
  • 19,570
  • 21
  • 109
  • 218
2
votes
1 answer

Using GhostScript 9.10 in Windows with Unicode characters in parameters

I want to use Ghostscript in a .NET / C# application to convert a .tiff file to PDF. My problem: When the file path contains non-ansi characters (e.g. Umlaute), the function gsapi_init_with_args fails. (With GS 8.x, it works fine!). I found…
Dirk Huber
  • 902
  • 6
  • 12
2
votes
1 answer

Convert c++ struct to struct c#

i have a problem with convert a struct c++ to c# struct, i will leave the following code: C++ struct SDK_ALARM_INPUTCONFIG { bool bEnable; int iSensorType; SDK_EventHandler hEvent; }; struct SDK_EventHandler { unsigned int …
2
votes
3 answers

How can C# call a ml64 dll directly without an intermediate C++ layer?

Is there a simple way to create a Visual Studios solution having two projects - the first a ml64 assembly program that creates a .dll - and a second C# program that invokes a method in the .dll? For example: ml64 code file div.asm: .code ;…
user113670
  • 29
  • 3
2
votes
2 answers

Getting the size of the array pointed to by IntPtr

I have a native C++ function that I call from a C# project using pinvoke. extern "C" _declspec(dllexport) void GetCmdKeyword( wchar_t** cmdKeyword, uint pCmdNum ) { int status = 1; int count = 0; int i = 0; if(…
Tyler Durden
  • 1,188
  • 2
  • 19
  • 35
2
votes
2 answers

Pinvoking a native function with array arguments

I am completely confused with how to go about calling functions in native dll with array arguments. Example: The function is defined in the C# project as: [DllImport("Project2.dll", SetLastError = true, CallingConvention =…
Tyler Durden
  • 1,188
  • 2
  • 19
  • 35
2
votes
1 answer

Using WM_SETTEXT to set Notepad text is not affecting Text_Changed event in the Notepad instance

I've almost completed a project that will basically, take the contents of a .txt file, open a new notepad instance using 'Process.Start("notepad")' and then set the text using the "WM_SETTEXT" constant. I have this working beautifully so I don't…
2
votes
2 answers

Nested structures in C# P/Invoke

I am trying to call into an unmanaged DLL that has the following structure: typedef struct { int num_objects; ppr_object_type *objects; } ppr_object_list_type; ppr_coordinate_type; typedef struct { int model_id; …
Alex Kilpatrick
  • 411
  • 1
  • 4
  • 16
2
votes
1 answer

Passing list of struct to C++ DLL from C# Application

I am trying to pass struct as a parameter from C# application to the C++ MFC DLL. DLL fills records in the struct object and return back to the C# application. So here I used "out" keyword in C# application to call C++ method. When I execute it, it…
Pankaj
  • 234
  • 3
  • 14
2
votes
1 answer

How to convert C function signature to C# and call that function in the native DLL?

int __stdcall getProps( /* [ in ] */ void * context, /* [ in ] */ const UINT32 index, /* [ in ] */ WCHAR * const pwszFilterPath, /* [ in, out ]*/ UINT32 * const…
user3104842