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 use SetWindowLong in C#?

I use this function: [DllImport ( "user32.dll" )] static extern int SetWindowLong ( IntPtr hWnd, int nIndex, uint dwNewLong ); and pass values from these: http://pinvoke.net/default.aspx/Constants/Window%20styles.html enum WindowLongFlags : int { …
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
2
votes
1 answer

P/Invoke Marshaling float*&

I'm trying to call some c++ dll functions from c# application using P/Invoke. Their headers are: void f1(float* a, float* b, long n, float* r); void f2(float* a, float* b, long n, float*& r); The difference in the usage is that f1 receives as…
Higi M.
  • 65
  • 1
  • 5
2
votes
2 answers

Getting the position of a windows application from C#

I need some help with trying to get the position of the notepad window. I'm pretty sure I need to use the GetWindowRect function, which is below, but I do not know how to use it. I've put what is below in to a win form project, but myRect.X,…
hurnhu
  • 888
  • 2
  • 11
  • 30
2
votes
1 answer

Updating an C dll struct array and its elements values from C# code using dllimport

I have C code which will be build as a dynamic library (DLL) , which i would like to call C function from C# using dLL created from the C code C code : struct data { char data_val1[100]; float data_val2; float data_val3[50]; }; typedef struct…
cslrnr
  • 694
  • 1
  • 8
  • 24
2
votes
1 answer

Marshalling C Struct with array of structures in it

I referred the similar questions in this forum but didn't get the solution for my problem. I have been struggling with marshaling problem for a while. I have a structure that contains an array of another structure, The platform is Win CE. I am using…
arya2arya
  • 291
  • 2
  • 21
2
votes
0 answers

Force Shutdown/Reboot

How can I force the windows computer to shutdown by using the P/Invoke Declaration for ExitWindowsEx in VB.Net? I am not talking about Process.Start("shutdown -s -f- -t 0") I know there is way to do the same thing using the Windows API. How can I…
nexno
  • 429
  • 8
  • 26
2
votes
1 answer

How to Correctly Call C++ DLL from C# using Correct Parameter Types

I have been provided with a DLL which is to be called by C# et al. The DLL contains two methods as follows extern "C" { __declspec(dllexport) BSTR GroupInit(LPCTSTR bstrIniFile, bool bDiagErr, bool bProcErr); __declspec(dllexport) void G(); }…
MoonKnight
  • 23,214
  • 40
  • 145
  • 277
2
votes
1 answer

Method's type signature is not PInvoke compatible

I am trying to use a function from a c dll, in a c# application, I am facing this error each time i try to run the application and call the function in question. At first i thought maybe this is because of the wrong signature i am using, but i tried…
Hossein
  • 24,202
  • 35
  • 119
  • 224
2
votes
1 answer

Logging to Event Viewer on Windows RT 8.1

I am working on an LOB (side-loading) app and I need to log events, crashes to ETW (Event Viewer logs). I see that most suggest writing own file IO wrapper. With Windows 8.1, we have new logging capabilities in "Windows.Foundation.Diagnostics" which…
Traze
  • 180
  • 8
2
votes
3 answers

How to convert a C structure in a C# application?

I am trying to use a structure from a C DLL in a C# application. I can't get the equivalent data types for the structure I am dealing with. I am dealing with these data types from C: struct teststruct { unsigned short protocol_type; //c#'s…
Hossein
  • 24,202
  • 35
  • 119
  • 224
2
votes
1 answer

Issues in structure conversion from C++ to C# .Net Compact Framework

I am converting c/c++ structures into C# standards. C/C++ Structures: typedef struct _sta_conn_info{ STA_CONNECT_STATE connect_state;//Enum STA_ASSOC_STATE assoc_state;//Enum unsigned char …
arya2arya
  • 291
  • 2
  • 21
2
votes
1 answer

Generate C# wrapper from C include file

Suppose I have a dll written in pure C. I have an inculde file (.h) so I can use the dll from a VS 2012 C project. Is there any way to generate a C# wrapper class based on the metainfo in the include file, or I must write all the [DllImport]...…
g.pickardou
  • 32,346
  • 36
  • 123
  • 268
2
votes
1 answer

Only first character is passed in pinvoke string

I have a c++ dll that I am using using Pinvoke. The method signature is as follow: c++: EXTERN_C BOOL std_call MyCppMethod(LPCSTR param1, LPCSTR param2); C#: [DllImport("MyDll.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static…
user844541
  • 2,868
  • 5
  • 32
  • 60
2
votes
2 answers

How to write the C# prototype to P/Invoke a function which takes a char* as an argument, and returns its value there

As the title suggests, I'm trying to write some C# code to interop with a C DLL (It's a Windows device driver, basically). The error code function signature in C is: UINT DLLErrorMsg( DWORD errorCode, char * pBuf, UINT nSize ); nSize is the size…
Clayton Hughes
  • 1,015
  • 2
  • 9
  • 22
2
votes
1 answer

detecting window text change with pinvoke

Is there any way to see if the contents of a 'window' have changed, that is referenced only by a handle? Such as one obtained from a pinvoke FindWindow?
Ciel
  • 17,312
  • 21
  • 104
  • 199