请教:如何在C#中调用API函数?

mableboy 2003-03-19 09:14:05
我想调用Winsnmp的一些函数,他需要WSNMP32.LIB这个库,我如何把它引到我的程序中来?
...全文
25 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lufly2000 2003-03-20
  • 打赏
  • 举报
回复
关注
mableboy 2003-03-20
  • 打赏
  • 举报
回复
谢谢!
gujianxin 2003-03-19
  • 打赏
  • 举报
回复
/////////////////////////////////////////////////////
/// 从windows目录下读取连接字符串

private const string USER = "User id";
private const string PASS = "password";
private const string SOURCE = "data source";
private const string CATALOG= "catalog";

private const string SECTION= "DataBase";
private static string INI_FILE= System.Environment.GetEnvironmentVariable("WINDIR")+"\\ini.dll";
// @"C:\Winnt\system32\ini.dll";

[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath);
public SQLConnString()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

/// <summary>
/// 取得连接字符串
/// </summary>
/// <returns></returns>
public static string GetConnStr()
{

string ReturnText;
ReturnText="persist security info=False; packet size=4096;data source="+IniReadValue(SOURCE)
+";initial catalog="+IniReadValue(CATALOG)+";user id="
+IniReadValue(USER)+";password="+IniReadValue(PASS);
// ReturnText="provider=SQLOLEDB;data source=172.16.36.222"+
// ";initial catalog=RemoteEdu;user id=sa"
// +";password=1234567890";

return ReturnText;

}


/// <summary>
/// 从配置文件中取配置
/// </summary>
/// <param name="Section"></param>
/// <param name="Key"></param>
/// <returns></returns>
public static string IniReadValue(string Key)
{

StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(SECTION,Key,"",temp,255,INI_FILE);
return temp.ToString();
}
enhydraboy 2003-03-19
  • 打赏
  • 举报
回复
本示例读取并显示可执行文件的 Win32 版本号,在本示例中此版本号与程序集版本号相同。本例中使用的可执行文件为 printversion.exe。本示例使用 Platform SDK 函数 VerQueryValue、GetFileVersionInfoSize 和 GetFileVersionInfo 从指定的版本信息资源中检索指定的版本信息。

本示例使用了指针,因为它可以简化在其签名中使用指向指针的指针(这在 Win32 API 中很常见)的方法的使用。

// printversion.cs
// compile with: /unsafe
using System;
using System.Reflection;
using System.Runtime.InteropServices;

// Give this assembly a version number:
[assembly:AssemblyVersion("4.3.2.1")]

public class Win32Imports
{
[DllImport("version.dll")]
public static extern bool GetFileVersionInfo (string sFileName,
int handle, int size, byte[] infoBuffer);
[DllImport("version.dll")]
public static extern int GetFileVersionInfoSize (string sFileName,
out int handle);

// The 3rd parameter - "out string pValue" - is automatically
// marshaled from Ansi to Unicode:
[DllImport("version.dll")]
unsafe public static extern int VerQueryValue (byte[] pBlock,
string pSubBlock, out string pValue, out uint len);
// This VerQueryValue overload is marked with 'unsafe' because
// it uses a short*:
[DllImport("version.dll")]
unsafe public static extern int VerQueryValue (byte[] pBlock,
string pSubBlock, out short *pValue, out uint len);
}

public class C
{
// Main is marked with 'unsafe' because it uses pointers:
unsafe public static int Main ()
{
try
{
int handle = 0;
// Figure out how much version info there is:
int size =
Win32Imports.GetFileVersionInfoSize("printversion.exe",
out handle);
// Be sure to allocate a little extra space for safety:
byte[] buffer = new byte[size+2];
Win32Imports.GetFileVersionInfo ("printversion.exe",
handle, size, buffer);
short *subBlock = null;
uint len = 0;
// Get the locale info from the version info:
Win32Imports.VerQueryValue (buffer,
"\\VarFileInfo\\Translation", out subBlock, out len);
string spv =
"\\StringFileInfo\\" + subBlock[0].ToString("X4")
+ subBlock[1].ToString("X4") + "\\ProductVersion";
byte *pVersion = null;
// Get the ProductVersion value for this program:
string versionInfo;
Win32Imports.VerQueryValue (buffer, spv,
out versionInfo, out len);
Console.WriteLine ("ProductVersion == {0}", versionInfo);
}
catch (Exception e)
{
Console.WriteLine ("Caught unexpected exception " +
e.ToString() + "\n\n" + e.Message);
}

return 0;
}
}
chinchy 2003-03-19
  • 打赏
  • 举报
回复
http://www.codeproject.com/useritems/WinSnmp_Api.asp

110,568

社区成员

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

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

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