P/Invoke & system.runtime.enteropService


i want to know what is the diff bitween the :
 1 - use the P/invoke to access the API function from .NET.
2- use the System.RunTime.InteropServices namespace which provide colliction of classes useful to the native API from .NET.

is there as same ?
if there is diff, which is better to use ?

thanks ...:)
Who is Participating?
System.RunTime.InteropServices namespace is how you would do p/invoke. I don't think there is an option between the two.
PInvoke is technology which allows to call native API from .NET programs. It works like VB6 API calls. PInvoke function declaration contains Dll name, function name and prototype, which is sufficient information to make dynamic call.

InteropServices namespace contains classes which are used for PInvoke calls. Data exchange between managed and unmanaged functions requires marshalling data between managed and unmanaged memory. InteropServices.IntPtr structure represents unmanaged pointer. InteropServices.Marshal class functions allow to make the following operations:
1) Allocation and releasing unmanaged memory blocks.
2) Copying information between unmanaged and menaged memory.
These operations are used when unmanaged function is called from managed code.

Generally, PInvoke contains default marshalling which allows to use simple function parameters like int, string, plain structures etc. Using Marshal class is required in PInvoke calls when function parameters are not trivial and cannot be passed using default marshalling.
dede11Author Commented:
thanks  ...

So, the P/Invoke is a part of InteropServices namespace work , right?
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

ok! late response :(
PInvoke is done using InteropServices. Simple parameters are marshalled using default marshalling. For example, if function has int parameter, PInvoke call internally allocates 4 bits unmanaged memory on the stack, copies int parameter to it and calls unmanaged function. Default PInvoke marshalling uses Marshal class internally.
For more complicated functions programmer uses Marshal class to create function parameters and read function output parameters.
Different functions with default marshalling are described here:
This is example of non-default marshalling:

// Function gets char** and allocates ansi string
SERVERDLL_API void Function4(char** p)
    *p = new char[20];
    strcpy(*p, "string from C");

// Function releases char* string
SERVERDLL_API void Release(char* p)
    delete[] p;

C# code:

[DllImport ("ServerDll.dll")]
public static extern void Function4(out IntPtr p);
[DllImport ("ServerDll.dll")]
public static extern void Release(IntPtr p);

IntPtr ptr;
Function4(out ptr);
string s = Marshal.PtrToStringAnsi(ptr);
dede11Author Commented:
thanks alot for all :) :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.