.net 下對winapi的調用

C#例子

using   System.Runtime.InteropServices;    

[DllImport("user32.dll")]    
  public   static   extern   int   SendMessage(IntPtr   hWnd,int   Msg,int   wParam,int   lParam);    
   
  //此處主要用來讓窗口置於最前(SetWindowPos(this.Handle,-1,0,0,0,0,0x4000|0x0001|0x0002);)    
  [System.Runtime.InteropServices.DllImport("user32.dll")]    
  public   static   extern   bool   SetWindowPos(IntPtr   hWnd,    
   int   hWndInsertAfter,    
   int   X,    
   int   Y,    
   int   cx,    
   int   cy,    
   int   uFlags    
   );      

在C#裏調用Win32函數有這麼幾個要點。

第一:名字要與Win32 API的完全一樣。

第二:函數除了要有相應的DllImport類修飾外,還要聲明成public static extern類型的。

第三:函數的返回值和參數類型要與Win32 API完全一致!

窗口指針 hWnd 可以用 int 也可以用 IntPtr   推薦使用 IntPtr  

VBaisc例子

方法一。

 <DllImport("user32.dll")> _
        Public Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal uFlags As Integer) As Boolean
        End Function

<DllImport("KERNEL32.DLL", EntryPoint:="MoveFileW", SetLastError:=True, _
    CharSet:=CharSet.Unicode, ExactSpelling:=True, _
    CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function MoveFile(ByVal src As String, ByVal dst As String) As Boolean
        '   This   function   copies   a   file   from   the   path   src   to   the   path   dst.  
        '   Leave   function   empty   -   DLLImport   attribute   forces   calls    
        '   to   MoveFile   to   be   forwarded   to   MoveFileW   in   KERNEL32.DLL.  
    End Function

方法二

Declare Function GetUserName Lib "advapi32.dll" Alias _
        "GetUserNameA" (ByVal lpBuffer As String, ByRef nSize As Integer) As Integer

 詳細例子可見另一篇文章 

 VB.NET編程中調用Windows API
本文來自: 中國網管聯盟(bitsCN.com) 詳細出處參考:http://www.bitscn.com/dotnet/vb/200605/24574.html 

VB.Net查看文件中圖標的函數及申明Windows API的方法
本文來自: 中國網管聯盟(bitsCN.com) 詳細出處參考:http://www.bitscn.com/dotnet/vb/200605/24574.html

 

附1 常用Win32數據類型與.NET平臺數據類型的對應表

Figure 2 Non-Pointer Data Types

Win32 Types Specification CLR Type
char, INT8, SBYTE, CHAR 8-bit signed integer System.SByte
short, short int, INT16, SHORT 16-bit signed integer System.Int16
int, long, long int, INT32, LONG32, BOOL, INT 32-bit signed integer System.Int32
__int64, INT64, LONGLONG 64-bit signed integer System.Int64
unsigned char, UINT8, UCHAR, BYTE 8-bit unsigned integer System.Byte
unsigned short, UINT16, USHORT, WORD, ATOM, WCHAR, __wchar_t 16-bit unsigned integer System.UInt16
unsigned, unsigned int, UINT32, ULONG32, DWORD32, ULONG, DWORD, UINT 32-bit unsigned integer System.UInt32
unsigned __int64, UINT64, DWORDLONG, ULONGLONG 64-bit unsigned integer System.UInt64
float, FLOAT Single-precision floating point System.Single
double, long double, DOUBLE Double-precision floating point System.Double
In Win32 this type is an integer with a specially assigned meaning; in contrast, the CLR provides a specific type devoted to this meaning.
       
附2。net封裝的api函數見《Microsoft Win32 to Microsoft .NET Framework API Map
附3。一個第三方的api聲明程序(未驗證過)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章