Your SlideShare is downloading. ×
Unmanged code InterOperability
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Unmanged code InterOperability

1,723
views

Published on

this ppt helps you learn and present about Unmanged code InterOperability in C# .net. …

this ppt helps you learn and present about Unmanged code InterOperability in C# .net.
This has sample code included

Published in: Education, Technology

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,723
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
50
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Rohit Vipin Mathews
  • 2.  Interoperability is the process of communication between two separate systems. In .NET Interop, the first system is always the .NET Framework; the other system might be any other technology. Unmanaged interoperation is not easy as the managed interop, and its also much difficult and much harder to implement. In unmanaged code interoperation, the first system is the .NET code; the other system might be any other technology including Win32 API, COM, ActiveX, etc.
  • 3.  Interoperation with Native Libraries. Interoperation with COM components.
  • 4.  This is the most famous form of .NET interop with unmanaged code. We usually call this technique, Platform Invocation, or simplyPInvoke. Platform Invocation or PInvoke refers to the technique used to call functions of native unmanaged libraries such as the Windows API.
  • 5. To PInvoke a function, you must declare it in your .NETcode. That declaration is called the Managed Signature.To complete the managed signature, you need to knowthe following information about the function:1. The library file which the function resides in.2. Function name.3. Return type of the function.4. Input parameters.5. Other relevant information such as encoding.
  • 6. using System.Runtime.InteropServices;namespace InterOp_with_API{ class Program { public enum beepType { Beep = -1, OK=0x00, Question=0x20, Exclamation=0x30, Asterix=0x40, }
  • 7. [DllImport("User32.dll",ExactSpelling=true)] static extern bool MessageBeep(uint type); static void Main(string[] args) { MessageBeep((uint)beepType.Exclamation); MessageBeep((uint)beepType.Question); } }}
  • 8.  Marshaling is the process of converting unmanaged types into managed and vice versa . That conversion can be done in many ways based on the type to be converted. For example, BOOL can simply be converted to System.Boolean, and LPCTSTR can be converted to System.String, System.Text.StringBuilder, or even System.Char[]. Compound types (like structures and unions) usually dont have counterparts in .NET code and thus you need to create them manually.
  • 9.  COM Interop is very large and much harder than P/Invoke and it has many ways to implement. COM Interop includes all COM-related technologies such as OLE, COM+, ActiveX, etc.
  • 10.  You cant talk directly to unmanaged code. you have to declare your functions and types in your .NET code. To include a COM-component in your .NET application, you go to the COM tab of the Add Reference dialog and select the COM component that you wish to add to your project and use it!
  • 11.  When you add a COM-component to your .NET application, Visual Studio automatically declares all functions and types in that library for you. It creates a Proxy library (i.e. assembly) that contains the managed signatures of the unmanaged types and functions of the COM component and adds it to your .NET application. The proxy acts as an intermediary layer between your .NET assembly and the COM-component. Therefore, your code actually calls the managed signatures in the proxy library that forwards your calls to the COM-component and returns back the results. Proxy libraries also called Primary Interop Assemblies (PIAs) and Runtime Callable Wrappers (RCWs.)
  • 12.  Now you dont have to ship a proxy/PIA/RCW assembly along with your executable since the information in this assembly can now be embedded into your executable; its called Interop Type Embedding.
  • 13. //Add a reference to the Microsoft Speech Object Library class Program { static void Main(string[] args) { Console.WriteLine("Enter the text to read:"); string txt = Console.ReadLine(); Speak(txt); } static void Speak(string text) { SpVoice voice = new SpVoiceClass(); voice.Speak(text, SpeechVoiceSpeakFlags.SVSFDefault); } }
  • 14.  ActiveX is no more than a COM component that has an interface. To add an ActiveX control to .NET application, right- click the Toolbox, select Choose Toolbox Items, switch to the COM Components tab and select the controls to be used in the application.

×