C# API 传地址的例子

 

/// Return Type: BOOL->int
///param0: HWND->HWND__*
///param1: LPARAM->LONG_PTR->int
[System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.StdCall)]
public delegate int WNDENUMPROC(System.IntPtr param0, ref System.IntPtr param1);

//在向导生成的定义中增加ref

 

/// Return Type: BOOL->int
///lpEnumFunc: WNDENUMPROC
///lParam: LPARAM->LONG_PTR->int
[System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "EnumWindows")]
[return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, ref int lParam);

//在向导生成的定义中增加ref

 

int find(System.IntPtr param0,  ref System.IntPtr param1)
{
    StringBuilder s = new StringBuilder(256);

    if ((int)param1 == 0)
        NativeMethods.GetWindowTextA(param0, s, s.Capacity);

 

    if ((int)param1 == 1)
        NativeMethods.GetWindowTextW(param0, s, s.Capacity);

 

    if (s.ToString().Trim().IndexOf(textBox1.Text.Trim()  ) != -1)
    {
          //解释成:将param0的值给param1地址指向的位置  
          param1=param0; 

          return 0;
    }

    return 1;
}

 

private void button3_Click(object sender, EventArgs e)
{
     WNDENUMPROC findproc = new WNDENUMPROC(find);

     int h = 0;

     System.IntPtr hMainWnd;
     if (NativeMethods.EnumWindows(findproc, ref h) == false)
     {
          hMainWnd =(System.IntPtr) h;
          MessageBox.Show(hMainWnd.ToString());
     }
     else
         MessageBox.Show("No found");

 }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值