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

AccessViolationException in PInvoke function call

I'm trying to write a wrapper for C library but I'm really struggling with this error. I tried many approaches, here is one of them: [DllImport(DRIVER_FILENAME)] [return: MarshalAs(UnmanagedType.U4)] private static extern uint…
msu
  • 75
  • 3
  • 9
2
votes
2 answers

PInvoke GetPackageId failing with error 122

I am trying to PInvoke this function (GetPackageId) from kernel32.dll: http://msdn.microsoft.com/en-us/library/windows/desktop/hh446607(v=vs.85).aspx I defined the structs and imports as follows: [StructLayout(LayoutKind.Sequential)] public…
2
votes
1 answer

P/Invoke, C# returns a string to native code

My code works, but I am afraid of the memory within P/Invoke. Here is the delegate I will call from native C++ code. [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode)] public delegate string…
Mr.Wang from Next Door
  • 13,670
  • 12
  • 64
  • 97
2
votes
1 answer

How to obtain a serial number of scanner with TWAIN?

I would like to obtain the serial number of a scanner in C# using twain_32.dll. I've found out it has to be done with dscap method with the icap_serialnumber capability. Can anyone give me an example how to do this?
Hans V
  • 21
  • 2
2
votes
2 answers

Managing C++ Garmin API in C#

I want to call Garmin API in VB.Net Compact Framework project. The API is in C++, so I'm making a C# dll project as intermediate way between API dll and VB.NET. I have some problems while executing my code because it throws a NotSupportedException…
borjolujo
2
votes
1 answer

Available space left on drive - WinAPI - Windows CE

I've forgotten the WinAPI call to find out how much space is remaining on a particular drive and pinvoke.net isn't giving me any love. It's compact framework by the way, so I figure coredll.dll. Can anyone with a better memory jog mine?
Quibblesome
  • 25,225
  • 10
  • 61
  • 100
2
votes
2 answers

Pinvoke- to call a function with pointer to pointer to pointer parameter

I have a function in C with this signature: int addPos(int init_array_size, int *cnt, int *array_size, PosT ***posArray, char *infoMsg); and here is what PosT looks like: typedef union pu { …
jambodev
  • 351
  • 2
  • 3
  • 9
2
votes
2 answers

Does P/Invoke provide less memory to the target DLL?

My issue is that I have a 32-bit DLL which I cannot modify. The DLL can sometimes require around 1.5 gigs of memory or so under normal operation. When I use a C++/unmanaged code test program, the DLL will only go out of memory at about 2 gigs. This…
Jas Laferriere
  • 804
  • 10
  • 12
2
votes
3 answers

How can I create this struct in C#?

I am trying to create the following struct in this msdn article. I am trying to learn the whole FieldOffset but have no clue where to start. I basically did something like this. [StructLayout(LayoutKind.Explicit, Size=12)] public struct…
Steven Combs
  • 1,890
  • 6
  • 29
  • 54
2
votes
1 answer

Correct PInvoke signatures to use nested structs with reserved memory from C#?

I've got an unmanaged DLL that I am trying to use that makes use of some nested structs with reserved members at the end of them. I'm having an issue converting them to C#. I've tried to strip down the code to what is essential. The ParentStruct…
Tim
  • 14,999
  • 1
  • 45
  • 68
2
votes
1 answer

Unable to load DLL 'MyDll.dll': The specified module could not be found. asp.net p/invoke

I am trying o p/invoke a c++ class i have created into a asp.net web application. I have used the following tutorial - p/invoke I can get it working when creating a c# console applicatioin but not a web application I get the following error when I…
blob
  • 61
  • 4
2
votes
1 answer

Error getting device handle

I have been trying to interface to a device driver for a GPS interface card. The examples that I have from the vendor are in C++. I am trying to get it working in C# using pinvoke class HaveQuick { [DllImport("setupapi.dll", CharSet =…
ed askew
  • 123
  • 1
  • 10
2
votes
1 answer

what's the proper way to retrieve text from a Rich Text Control using the Windows API?

The GetRTF() method below sort of works, but it retrieves only metadata: public string GetRTF(IntPtr handle) { string result = String.Empty; using (System.IO.MemoryStream stream = new MemoryStream()) { …
John Smith
  • 4,416
  • 7
  • 41
  • 56
2
votes
1 answer

Minimum user permissions required to pinvoke from a web app?

I've got an ASP.NET MVC app that p/invokes FindFirstFile and FindNextFile. When I create an app pool for the web site to run under, I'll specify a specific user account that has the minimum required permissions to make these calls. What permissions…
Padgett
  • 21
  • 2
2
votes
1 answer

Can you use .net 3.5 Action or Func as Marshalled unmanaged delegates?

After reading Dynamically calling unmanaged dlls in .net I've been trying to modify the code to my liking. I made a class that implements idisposable to wrap load calls in and free them when needed. However I can't seem to figure out the syntax if…
Maslow
  • 18,464
  • 20
  • 106
  • 193
1 2 3
99
100