P/Invoke

P/Invoke


1.P/Invoke是什么?有何作用?

2.简单的使用P/Invoke的demo


1.P/Invoke是什么?有何作用?

P/Invoke使得C#程序调用非托管的成为可能,例如可以使用P/Invoke来调用win32 api。


2.简单的使用P/Invoke的demo

2.1 使用P/Invoke直接调用win32 api

public   class  Program
    {
        
///   <summary>
        
///  import kernel32.dll 
        
///   </summary>
        
///   <param name="source"></param>
        
///   <param name="dest"></param>
        
///   <returns></returns>
        [DllImport( " kernel32.dll " , EntryPoint  =   " MoveFile " ,
            ExactSpelling 
=   false ,
            CharSet 
=  CharSet.Unicode,
            SetLastError 
=   true )]
        
static   extern   bool  MoveFile( string  source,  string  dest);

        
static   void  Main( string [] args)
        {
            
//  Call P/Invoke
             string  source  =   @" d:\test.txt " ;
            
string  dest  =   @" d:\test.txt.bak " ;
            
if  (UsingPInvoke.Program.MoveFile(source, dest)  ==   true )
            {
                Console.WriteLine(
" Move File Ok! " );
            }
            
else
            {
                Console.WriteLine(
" Move File Error! " );
            }   
        }

    }  

2.2 C#中的指针

public class UsingPointers
    {
        
const uint GenericRead = 0x80000000;
        
const uint OpenExisting = 3;
        
const uint UseDefault = 0;
        
int fileHandle;
        [DllImport(
"kernel32.dll",
            SetLastError 
= true)]
        
static extern unsafe int CreateFile(
            
string filename,
            
uint desiredAccess,
            
uint shareMode,
            
uint attributes,
            
uint createDisposition,
            
uint flagsAndAttributes,
            
uint templateFile
        );
        [DllImport(
"kernel32.dll",
            SetLastError 
= true)]
        
static extern unsafe bool ReadFile(
            
int hFile,
            
void* lpBuffer,
            
int bBytesToRead,
            
int* nBytesRead,
            
int overlapped
        );
        
public UsingPointers(string filename)
        {
            
this.fileHandle = CreateFile(
                filename,
                GenericRead,
                UseDefault,
                UseDefault,
                OpenExisting,
                UseDefault,
                UseDefault
            );
        }
        
public unsafe int Read(byte[] buffer, int index, int count)
        {
            
int byteRead = 0;
            
// 将byte[]转换成byte*
            fixed(byte* bytePointer = buffer)
            {
                
// 调用win32函数
                ReadFile(
                    fileHandle,
                    bytePointer 
+ index,
                    count,
                    
&byteRead,
                    
0
                    );
            }
            
return byteRead;
        }
    }

 

注意编译时,需要更改项目属性:

 

下面是一个p/invoke的具体例子,通过p/invoke来播放wav文件:http://www.codeproject.com/Articles/175030/PlaySound-A-Better-Way-to-Play-Wav-Files-in-Csharp.aspx  


本博客中的文章均是在学习和编码过程中的总结,其中难免存在错误之处,给您的带来的不便尽请谅解,同时欢迎您提出意见。

转载于:https://www.cnblogs.com/xuqiang/archive/2010/12/21/1953355.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值