An introduction to the P/Invoke Library open source project

 

Introduction and Invitation


With the .NET Framework, it is much easier and faster to build an application. However there are still times when you need to use Win32 API, which usually causes headaches to .NET programmers. That's the intent I started this project - P/Invoke Library.


Due to limited spare time, I've added the Win32 functions to this library in an ad-hoc manner, so don't be surprised at the code at the time of this writing. If you want to join in this project and improve it, I'd be very glad.

 

The P/Invoke Interop Assistant and the pinvoke.net wiki are great references for P/Invoke too.

 

Sharing Code between Platforms


This library has too builds - one for the .NET Framework, and the other for the .NET Compact Framework. As you know most of the API have the same interface between the two platforms, so sharing the source code is necessary to reduce the work involved. There are at least two means to share classes between desktop and mobile - sharing the binary, or sharing the source code files.

 

To share the binary, we need to build the assembly under the Windows Mobile platform, and then we can use the same binary on desktop too. The reason is that the .NET Compact Framework (NET CF) assemblies are “retargetable”, which means that when we use an assembly in NET CF (e.g. mscorlib) on desktop, the desktop CLR will instead load its counterpart built for desktop. So a NET CF assembly (binary) can be used on desktop directly, provided that it is not platform-dependent. Because the desktop assemblies are not retargetable, we cannot do the reverse.

 

In case we have to call platform-specific classes / API at run time, we can branch these calls based on whether or not 
    Environment.OSVersion.Platform == PlatformID.WinCE 


To use the second option, i.e. sharing source code, there are again two options - using the #directives (similar to C++) or using linked files. The following steps show how to use linked files to share source code:

 

    1.  Create a source code file which contains the common (or shared) part of a class. This class should be partial class. The code file is added in one project; in another project you add a link to this file.

    2.  In the desktop project add a partial class that contains code specific to the desktop platform;

    3.  In the smart devices project add a partial class that contains code specific to smart devices.

 

The P/Invoke project uses linked files to share code, as you can see in the source code. In this way we can avoid the lots of #derectives for the most part. But #derective is still helpful, as used here:

    public static partial class User32
    {
#if PocketPC
        
private const string User32Dll = "coredll.dll";
#else
        
private const string User32Dll = "user32.dll";
#endif
        [DllImport(User32Dll, SetLastError 
= true)]
        
public static extern int GetWindowLong(IntPtr hWnd, GWL nIndex);

 

An interesting Demo - a Win32 Windows application in C#


Have you ever thought of building a classic-style Win32 Windows application using C#, rather than using C/C++? Here is a demo to show you how to do that with the P/Invoke library:

Win32 in C#

 

Links


The P/Invoke Library project hosted on CodePlex

http://www.codeplex.com/pinvoke

 

 

0
0
(请您对文章做出评价)
« 上一篇:NDepend - a tool to Analyze existing code
» 下一篇:Most Popular Websites
posted @ 2008-08-28 17:51 Warren Tang 阅读(1080) 评论(4)  编辑 收藏 网摘 所属分类: dotNET

  回复  引用  查看    
#1楼2008-08-28 21:05 | aspnetx      
在codeplex看到的,看地址才知道是园子里的兄弟,呵呵来支持一下.
  回复  引用  查看    
#2楼[楼主]2008-08-28 21:09 | Warren Tang      
Thank you!
  回复  引用  查看    
#3楼2008-09-06 01:24 | fox23      
Great~ thanks for your sharing
  回复  引用  查看    
#4楼[楼主]2008-09-09 00:29 | Warren Tang      
My pleasure.