Sunday, September 19, 2010

Platform Invoke Tutorial

Q. what achually it is?
Platform Invocation Services (PInvoke) allows managed code (for example code written in C sharp)to call unmanaged functions that are implemented in a DLL( for example th ecode you write in CUDA and create a dll of it).

for this you are required to tell your C Sharp Application that your application CONTAIN  A CALL TO DLL.
1. USE NAMESPACE:: System.Runtime.InteropServices.
2. you must provide the C# compiler with a declaration of the unmanaged function.
for this include fallowing statiment just After the constructor of your class.

 [DllImport("STRING PATH OF DLL ")]
        public static extern RETURN TYPE  FUNTION NAME ( @ PARAMS);

Declare the method with the static and extern C# keywords.
1 why static :: to associate the dll method with the calling class.
2.  why extern:: to tell C sharp compiler that the method is from outside( not belongs to C sharp class).
3.RETURN TYPE :: It is the return type of the funtion which is being declared in C dll .
here the important thing is thats C#  does not support various return type as C ( like pointer )
hence we are required to convert these return type into C# compitable return type.
4. Funtion name:: may or may not be same as DLL funtion name.
5.Formal params to function:: the are also not supported and required to get change.

MORE DETAIN IN NEXT BLOG....

No comments:

Post a Comment