[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 346
  • Last Modified:

P/Invoke & system.runtime.enteropService

hello

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 ...:)
0
dede11
Asked:
dede11
  • 3
  • 2
  • 2
2 Solutions
 
AlexFMCommented:
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.
0
 
dede11Author Commented:
thanks  ...

So, the P/Invoke is a part of InteropServices namespace work , right?
0
 
RoninTheCommented:
System.RunTime.InteropServices namespace is how you would do p/invoke. I don't think there is an option between the two.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RoninTheCommented:
ok! late response :(
0
 
AlexFMCommented:
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:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp09192002.asp
0
 
AlexFMCommented:
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);
 Console.WriteLine(s);
Release(ptr);
0
 
dede11Author Commented:
thanks alot for all :) :)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now