请教out参数的用法

lxkim 2005-03-04 12:24:09
[StructLayout(LayoutKind.Sequential)]
public class POINT
{
public int x;
public int y;


public POINT()
{
}

public POINT(int x, int y)
{
this.x = x;
this.y = y;
}
}
[DllImport("user32.dll")]
static extern bool GetCursorPos(out POINT lpPoint);

GetCursorPos的参数是out型的

在调用时
POINT lppoint = new POINT(0,0);
GetCursorPos(out lppoint);
label1.Text = lppoint.x.ToString();
label2.Text = lppoint.y.ToString();
为什么会出错?提示未将对象引用设置到对象实例
...全文
249 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
egxsun 2005-03-04
  • 打赏
  • 举报
回复
类型要匹配,如果用 class POINT ,就要这样:

[DllImport("user32.dll")]
static extern bool GetCursorPos(POINT lpPoint);
调用时直接:GetCursorPos(lppoint);

struct 类型就要:
[System.Runtime.InteropServices.DllImport( "user32.dll" , EntryPoint="GetCursorPos" )]
internal extern static int GetCursorPos( out POINTAPI lpPoint );
调用时: GetCursorPos(out lppoint);
如果[System.Runtime.InteropServices.DllImport( "user32.dll" , EntryPoint="GetCursorPos" )]
internal extern static int GetCursorPos( ref POINTAPI lpPoint );
调用时: GetCursorPos(ref lppoint);
lxkim 2005-03-04
  • 打赏
  • 举报
回复
上面的改一下:
internal struct POINTAPI
{
internal int x;
internal int y;
}
[System.Runtime.InteropServices.DllImport( "user32.dll" , EntryPoint="GetCursorPos" )]
internal extern static int GetCursorPos( ref POINTAPI lpPoint );
调用时
POINTAPI _POINTAPI = new POINTAPI();
GetCursorPos(ref lppoint);
label1.Text = lppoint.x.ToString();
label2.Text = lppoint.y.ToString();

这样可以,或者用:

internal struct POINTAPI
{
internal int x;
internal int y;
}
[System.Runtime.InteropServices.DllImport( "user32.dll" , EntryPoint="GetCursorPos" )]
internal extern static int GetCursorPos( out POINTAPI lpPoint );
调用时
POINTAPI _POINTAPI = new POINTAPI();
GetCursorPos(out lppoint);
label1.Text = lppoint.x.ToString();
label2.Text = lppoint.y.ToString();

lx1920 2005-03-04
  • 打赏
  • 举报
回复
internal struct POINTAPI
{
internal int x;
internal int y;
}
[System.Runtime.InteropServices.DllImport( "user32.dll" , EntryPoint="GetCursorPos" )]
internal extern static int GetCursorPos( ref POINTAPI lpPoint );
调用时
POINTAPI _POINTAPI = new POINTAPI();
GetCursorPos(out lppoint);
label1.Text = lppoint.x.ToString();
label2.Text = lppoint.y.ToString();

thinhunan 2005-03-04
  • 打赏
  • 举报
回复
out 参数不必初始化
pedit 2005-03-04
  • 打赏
  • 举报
回复
你把POINT设置为结构试试
ofei 2005-03-04
  • 打赏
  • 举报
回复
GetCursorPos是静态方法 要带上类名.GetCursorPos()来调用

完整的出错信息?
xxuu503 2005-03-04
  • 打赏
  • 举报
回复
POINT lppoint;
GetCursorPos(out lppoint);
label1.Text = lppoint.x.ToString();
label2.Text = lppoint.y.ToString();

110,532

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧