怎样调用API?

caozping 2003-07-24 09:52:33
在C#中怎样可以API函数?
有没有API的类什么的?
...全文
117 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
newlife2002_313 2003-08-04
  • 打赏
  • 举报
回复
MARK
Poseidon2 2003-07-24
  • 打赏
  • 举报
回复
方法各位都说了,我只想提醒一下,请注意数据类型的对应关系,因为你看api的函数声明一般都是基于VB或VC的,一般如下:
如果是字符串指针,在C#中用StringBuilder,用BYTE[]也行;
如果是int或long用int,是指针的话加一个ref或out;
其它的我也不多说了,只要注意到这点后,上网可以找些东东看看,还有是结构也要注意一下。
lihonggen0 2003-07-24
  • 打赏
  • 举报
回复
ms-help://MS.VSCC/MS.MSDNVS.2052/vblr7/html/vastmDeclare.htm
ms-help://MS.VSCC/MS.MSDNVS.2052/vbcn7/html/vaconCallingWindowsAPIs.htm
ArLi2003 2003-07-24
  • 打赏
  • 举报
回复
download api explorer help u

from: http://zpcity.com/arli
雪狼1234567 2003-07-24
  • 打赏
  • 举报
回复
参见如下简单例子:
// Copyright
// Microsoft Corporation
// All rights reserved

// MsgBox.cs

using System;
using System.Runtime.InteropServices;

public class LibWrap
{
// int MessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType);

[ DllImport( "User32.dll", EntryPoint="MessageBox", CharSet=CharSet.Auto )]
public static extern int MsgBox( int hWnd, String text, String caption, uint type );

// this will cause incorrect output in message window

[ DllImport( "User32.dll", EntryPoint="MessageBoxW", CharSet=CharSet.Ansi )]
public static extern int MsgBox2( int hWnd, String text, String caption, uint type );

// this will cause an exception

[ DllImport( "User32.dll", EntryPoint="MessageBox", CharSet=CharSet.Ansi, ExactSpelling=true )]
public static extern int MsgBox3( int hWnd, String text, String caption, uint type );
}

public class MsgBoxSample
{
public static void Main()
{
LibWrap.MsgBox( 0, "Correct text", "MsgBox Sample", 0 );
LibWrap.MsgBox2( 0, "Incorrect text", "MsgBox Sample", 0 );
try
{
LibWrap.MsgBox3( 0, "No such function", "MsgBox Sample", 0 );
}
catch( EntryPointNotFoundException )
{
Console.WriteLine( "EntryPointNotFoundException thrown as expected!" );
}
}

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
DialogResult r = MessageBox.Show ( "是否关闭窗口!" , "关闭窗口!" , MessageBoxButtons.YesNo , MessageBoxIcon.Question ) ;
if (!(DialogResult.Yes ==r))
{
e.Cancel = true;
}
}
}
wzs_wzs123 2003-07-24
  • 打赏
  • 举报
回复
using System.Runtime.InteropServices;
[DllImport("gdi32.dll")]
private static extern int GetPixel (IntPtr hdc , int x1,int y1);[DllImport("gdi32.dll")]
private static extern int SetPixel (IntPtr hdc , int x1,int y1,int color);

111,097

社区成员

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

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

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