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

Using a method from C that's pointer dependent in C#

I'm making a wrapper for a C library. There is a method that changes 2 ints by the user giving 2 int pointers to the method. So if I have void changenums(int* a, int* b) what is a safe way to access this method in c#?
user2348979
  • 143
  • 1
  • 2
  • 8
2
votes
1 answer

P/Invoke Interop Assistant: Is this actually correct?

I've got the following structs in C++: (Using pragma pack 1) typedef struct _wfs_cdm_physicalcu { LPSTR lpPhysicalPositionName; CHAR cUnitID[5]; ULONG ulInitialCount; ULONG ulCount; ULONG …
hb.
  • 1,705
  • 5
  • 22
  • 43
2
votes
1 answer

I use Delphi to call a DLL but still have an error

This is the invoke code unit CY; interface uses windows; type TMember_Rd = Record iID: integer; sCode: Array[0..255] of char; sName: Array[0..255] of char; end; PTMember_Rd = ^TMember_Rd; function GetMember(sStore, sMachine:…
2
votes
1 answer

C# Search Byte Array inside Process Memory

I'm developing a little memory scanner for a specific application. When I select a process to scan, the first thing I want to do is to verify that the process is an instance that specific application... and to do this I must find a signature that…
Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
2
votes
1 answer

LogonUser call - does the user need login rights to that machine?

I've had a hard time locating much information on this method and so I thought I'd ask out here and see if you wonderful people could help. What we basically have is this ... [DllImport("advapi32.dll", SetLastError = true)] private static extern…
cavillac
  • 1,311
  • 12
  • 22
2
votes
0 answers

WinUSB asynchrous call in .NET with PInvoke

I have programmed a microcontroller with USB and connected it a PC using WinUSB. I am able to communicate with my uC in c++ and in VB.NET using pinvoke with synchronous (blocking, overlapped = NULL) calls. I just got asynchronous calls working in…
Manny
  • 426
  • 4
  • 11
2
votes
2 answers

Accessing managed object in a c# callback passed to a native dll

I'm trying to pass a C# callback function to a native dll. It works fine, but I couldn't find a way to access the parent object of the callback method. Here's a code which demonstrates what I want to do: class MyForm: Form { public delegate void…
Max
  • 19,654
  • 13
  • 84
  • 122
2
votes
3 answers

What is the difference between structures containing bool vs uint when using PInvoke?

Ok, I'm now very confused. After my last question had several people comment about changing bool to uint I verified they are the same size by: Console.WriteLine("sizeof bool = {0}", Marshal.SizeOf(typeof(bool))); Console.WriteLine("sizeof…
csharptest.net
  • 62,602
  • 11
  • 71
  • 89
2
votes
1 answer

C# imports C++ Unmanaged dll and create multiple instance from one dll?

i have an unmanaged dll written in C++ which has a one simple function. int temp =0; int func1(void) { return temp +=2; } and exported function is UNMANAGED_DLL1_API int recepret(void) { return func1(); } Then i have a C# project that…
2
votes
1 answer

How can I call SHExtractIconsW from C#?

how to use SHExtractIconsW dll function in C#, I managed to do this in AutoIt, Local $arResult = DllCall('shell32.dll', 'int', 'SHExtractIconsW', _ 'wstr', $sIcon, _ 'int', $aDetails[5], _ 'int', $iSize, _ 'int',…
nyongrand
  • 618
  • 10
  • 24
2
votes
2 answers

Issue in PInvoke with char** parameter, String array is not working

I have a DLL file that is written in C. I tried to use it in managed code but some how my function is not working properly. Here is the C code. int preProcessImagesC (char *p_trainingFilePath, char **p_vecImageFilesOrDirs); …
2
votes
0 answers

wm_gettext returning blank until tabbed through

I am creating an application that is supplimenting the data provided on our third part application. They don't have an open API so I have been force to using WM_GETTEXT to get the information that we are looking at. This works fine for the the…
2
votes
1 answer

How to marshall cvMat and CV_MAT_ELEM

I'm trying to use some openCV Ml functions in c#, but i dont want to use the emugcv because my teachers wont let me, so i'm trying to marshall this two lines of code using p/invoke. (oh and should i use the normal dll or the d.dll ?? ) and…
virgula24
  • 523
  • 5
  • 23
2
votes
1 answer

How do I read the names from a PE module's export table?

I've successfully read a PE header from an unmanaged module loaded into memory by another process. What I'd like to do now is read the names of this module's exports. Basically, this is what I have so far (I've left out a majority of the PE parsing…
David Brown
  • 35,411
  • 11
  • 83
  • 132
2
votes
1 answer

C# PInvoke to get ACL of files and folders by Win32 API

which PInvoke do I need to verify the permissions (CanRead, CanWrite, CanExecute...) for an UNC-Path(\UNC\?\ or \?\, Files and Folders). With System.IO I would use fileInfo.GetAccessControll().GetAccessRules to get the AuthorizationRuleCollection…
DerAbt
  • 337
  • 1
  • 5
  • 14