C#怎么调用API?

qqwshl 2010-01-26 06:49:11
1、我是初学者,不懂这么调用API,需要怎么个格式?
2、我下载了几个API大全,但是我不知道每个API都是干什么的怎么办?
...全文
274 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wuyq11 的回复:]
using System.Runtime.InteropServices; //引用此名称空间
//使用DllImportAttribute特性来引入api函数
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName,
string lpWindowName);
DllImport参数:
CallingConvention 参数指示入口点的调用约定。如果未指定 CallingConvention,则使用默认值 CallingConvention.Winapi。
CharSet 参数指示用在入口点中的字符集。如果未指定 CharSet,则使用默认值 CharSet.Auto。
EntryPoint 参数给出 dll 中入口点的名称。如果未指定 EntryPoint,则使用方法本身的名称。
ExactSpelling 参数指示 EntryPoint 是否必须与指示的入口点的拼写完全匹配。如果未指定 ExactSpelling,则使用默认值 false。
PreserveSig 参数指示方法的签名应当被保留还是被转换。当签名被转换时,它被转换为一个具有 HRESULT 返回值和该返回值的一个名为 retval 的附加输出参数的签名。如果未指定 PreserveSig,则使用默认值 true。
SetLastError 参数指示方法是否保留 Win32"上一错误"。如果未指定 SetLastError,则使用默认值 false。
说明看看API
[/Quote]
我还没学,如梦,所有API都是这样的吗?
hzsagile 2010-01-26
  • 打赏
  • 举报
回复
//参考

public class WinProcess
{
[StructLayout(LayoutKind.Sequential)]
public class SECURITY_ATTRIBUTES
{
public int nLength;
public string lpSecurityDescriptor;
public bool bInheritHandle;
}

[StructLayout(LayoutKind.Sequential)]
public struct STARTUPINFO
{
public int cb;
public string lpReserved;
public string lpDesktop;
public int lpTitle;
public int dwX;
public int dwY;
public int dwXSize;
public int dwYSize;
public int dwXCountChars;
public int dwYCountChars;
public int dwFillAttribute;
public int dwFlags;
public int wShowWindow;
public int cbReserved2;
public byte lpReserved2;
public IntPtr hStdInput;
public IntPtr hStdOutput;
public IntPtr hStdError;
}

[StructLayout(LayoutKind.Sequential)]
public struct PROCESS_INFORMATION
{
public IntPtr hProcess;
public IntPtr hThread;
public int dwProcessId;
public int dwThreadId;
}

[DllImport("Kernel32.dll", EntryPoint = "CreateProcess", CharSet = CharSet.Auto)]
public static extern bool CreateProcess(StringBuilder lpApplicationName,
StringBuilder lpCommandLine,
SECURITY_ATTRIBUTES lpProcessAttributes,
SECURITY_ATTRIBUTES lpThreadAttributes,
bool bInheritHandles,
int dwCreationFlags,
StringBuilder lpEnvironment,
StringBuilder lpCurrentDirectory,
ref STARTUPINFO lpStartupInfo,
ref PROCESS_INFORMATION lpProcessInformation
);

//本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/bear775520/archive/2006/04/07/654717.aspx

}//end class WinProcess

hzsagile 2010-01-26
  • 打赏
  • 举报
回复
或者用结构

[StructLayout(LayoutKind.Sequential)]
public struct struct_OsVersionInformation
{
public int dwOSVersionInfoSize;
public int dwMajorVersion;
public int dwMinorVersion;
public int dwBuildNumber;
public int dwPlatformID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
//资料来源:学网(www.xue5.com),原文地址:http://www.xue5.com/itedu/200802/109538.html
public string szCSDVersion;
public ushort wServicePackMajor;
public ushort wServicePackMinor;
public ushort wSuiteMask;
public byte wProductType;
public byte wReserved;
}// end struct struct

//申明
[DllImport("Kernel32.dll", EntryPoint = "GetVersionEx")]
private static extern bool GetVersionEx2(ref struct_OsVersionInformation pStruct_VersionInformation);


//重载GetOSVersion函数 struct_OsVersionInformation 结构(静态成员重载)
public static void GetOSVersion(ref struct_OsVersionInformation struct_osvi)
{
//建立结构实例
struct_osvi = new struct_OsVersionInformation();
//为新建的结构分配内存
struct_osvi.dwOSVersionInfoSize = Marshal.SizeOf(struct_osvi);
//结构引用
if (!GetVersionEx2(ref struct_osvi))
{
throw new Exception("GetVersionEx调用失败");
}

}

//使用

struct_OsVersionInformation struct_osvi_temp = new struct_OsVersionInformation();
GetOSVersion(ref struct_osvi_temp);

hzsagile 2010-01-26
  • 打赏
  • 举报
回复
[StructLayout(LayoutKind.Sequential)]
public class OsVersionInformation
{
public int dwOSVersionInfoSize;
public int dwMajorVersion;
public int dwMinorVersion;
public int dwBuildNumber;
public int dwPlatformID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
//资料来源:学网(www.xue5.com),原文地址:http://www.xue5.com/itedu/200802/109538.html
public string szCSDVersion;
public ushort wServicePackMajor;
public ushort wServicePackMinor;
public ushort wSuiteMask;
public byte wProductType;
public byte wReserved;
}//end class OsVersionInformation



//API调用申明

[DllImport("Kernel32.dll")]
private static extern bool GetVersionEx([In, Out]OsVersionInformation OVI);

//重载GetOSVersion函数 OsVersionInformation 类(静态成员重载)
public static OsVersionInformation GetOSVersion()
{
//建立类实例
OsVersionInformation osvi = new OsVersionInformation();

//为新建的类分配内存
osvi.dwOSVersionInfoSize = Marshal.SizeOf(osvi);

//类传递
if (!GetVersionEx(osvi))
{
throw new Exception("GetVersionEx调用失败");

}
return osvi;
}

//调用
OsVersionInformation ovi = new OsVersionInformation();
ovi = GetOSVersion();

TIM178 2010-01-26
  • 打赏
  • 举报
回复
大哥我也在学用API搞个播放器什么的,好麻烦,我也有些实例,一块学习吧!
fuheng 2010-01-26
  • 打赏
  • 举报
回复
学习!
热情的菜鸟 2010-01-26
  • 打赏
  • 举报
回复
这种问题大家最喜欢回答,一搜一大堆
遇到类型转换方面的东西就没人吭声了
whb147 2010-01-26
  • 打赏
  • 举报
回复
学习
ljgljb 2010-01-26
  • 打赏
  • 举报
回复
看下最简单的关机,,注销的api吧。。
网上有例子
lmc19860810 2010-01-26
  • 打赏
  • 举报
回复
学习
chowyi 2010-01-26
  • 打赏
  • 举报
回复
学习
wangxianshou 2010-01-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wuyq11 的回复:]
using System.Runtime.InteropServices; //引用此名称空间
//使用DllImportAttribute特性来引入api函数
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName,
string lpWindowName);
DllImport参数:   
CallingConvention 参数指示入口点的调用约定。如果未指定 CallingConvention,则使用默认值 CallingConvention.Winapi。   
CharSet 参数指示用在入口点中的字符集。如果未指定 CharSet,则使用默认值 CharSet.Auto。 
EntryPoint 参数给出 dll 中入口点的名称。如果未指定 EntryPoint,则使用方法本身的名称。   
ExactSpelling 参数指示 EntryPoint 是否必须与指示的入口点的拼写完全匹配。如果未指定 ExactSpelling,则使用默认值 false。   
PreserveSig 参数指示方法的签名应当被保留还是被转换。当签名被转换时,它被转换为一个具有 HRESULT 返回值和该返回值的一个名为 retval 的附加输出参数的签名。如果未指定 PreserveSig,则使用默认值 true。   
SetLastError 参数指示方法是否保留 Win32"上一错误"。如果未指定 SetLastError,则使用默认值 false。   
说明看看API
[/Quote]
正解,说的太好了
fangxiaofelix 2010-01-26
  • 打赏
  • 举报
回复
楼上正解
wuyq11 2010-01-26
  • 打赏
  • 举报
回复
using System.Runtime.InteropServices; //引用此名称空间
//使用DllImportAttribute特性来引入api函数
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName,
string lpWindowName);
DllImport参数:
CallingConvention 参数指示入口点的调用约定。如果未指定 CallingConvention,则使用默认值 CallingConvention.Winapi。
CharSet 参数指示用在入口点中的字符集。如果未指定 CharSet,则使用默认值 CharSet.Auto。
EntryPoint 参数给出 dll 中入口点的名称。如果未指定 EntryPoint,则使用方法本身的名称。
ExactSpelling 参数指示 EntryPoint 是否必须与指示的入口点的拼写完全匹配。如果未指定 ExactSpelling,则使用默认值 false。
PreserveSig 参数指示方法的签名应当被保留还是被转换。当签名被转换时,它被转换为一个具有 HRESULT 返回值和该返回值的一个名为 retval 的附加输出参数的签名。如果未指定 PreserveSig,则使用默认值 true。
SetLastError 参数指示方法是否保留 Win32"上一错误"。如果未指定 SetLastError,则使用默认值 false。
说明看看API
鸭梨山大帝 2010-01-26
  • 打赏
  • 举报
回复
[DllImport]

http://blog.csdn.net/abaowu/archive/2004/11/24/193626.aspx

using System;
using System.Runtime.InteropServices;
class MainApp
{ //通过DllImport引用user32.dll类。MessageBox来自于user32.dll类
 [DllImport("user32.dll", EntryPoint="MessageBox")]
 public static extern int MessageBox(int hWnd, String strMessage, String strCaption, uint uiType);
 public static void Main()
 {
  MessageBox( 0, "您好,这是 PInvoke!", ".net", 0 );
 }
}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/abaowu/archive/2004/11/24/193626.aspx
  • 打赏
  • 举报
回复
既然是大全,应该有API参数及返回值说明

110,538

社区成员

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

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

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