P/Invoke & system.runtime.enteropService

Posted on 2006-04-17
Last Modified: 2010-04-16

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 ...:)
Question by:dede11
    LVL 48

    Assisted Solution

    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.

    Author Comment

    thanks  ...

    So, the P/Invoke is a part of InteropServices namespace work , right?
    LVL 5

    Accepted Solution

    System.RunTime.InteropServices namespace is how you would do p/invoke. I don't think there is an option between the two.
    LVL 5

    Expert Comment

    ok! late response :(
    LVL 48

    Expert Comment

    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:
    LVL 48

    Expert Comment

    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);

    Author Comment

    thanks alot for all :) :)

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Problem with C# 1 44
    How to count json data 2 41
    deploying a service to production environment 13 65
    Problem when read a file in C# 17 55
    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now