• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

C# 中使用非托管代码(使用指针的代码)

OC/C/C++ 水墨上仙 2450次浏览

本示例演示了如何在 C# 中使用非托管代码(使用指针的代码)。

// printversion.cs
// 编译时使用:/unsafe
using System;
using System.Reflection;
using System.Runtime.InteropServices;
// 为此程序集指定一个版本号:
[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);
   
	// 自动将第三个参数“out string pValue”从 Ansi
	// 封送处理为 Unicode:
	[DllImport("version.dll")]
	unsafe public static extern bool VerQueryValue (byte[] pBlock,
		string pSubBlock, out string pValue, out uint len);
	// 此 VerQueryValue 重载被标记为“unsafe”,因为
	// 它使用 short*:
	[DllImport("version.dll")]
	unsafe public static extern bool VerQueryValue (byte[] pBlock,
		string pSubBlock, out short *pValue, out uint len);
}
public class C 
{
	// Main 被标记为“unsafe”,因为它使用指针:
	unsafe public static int Main () 
	{
		try 
		{
			int handle = 0;
			// 确定有多少版本信息:
			int size =
				Win32Imports.GetFileVersionInfoSize("printversion.exe",
				out handle);
			if (size == 0) return -1;
			byte[] buffer = new byte[size];
			if (!Win32Imports.GetFileVersionInfo("printversion.exe", handle, size, buffer))
			{
				Console.WriteLine("Failed to query file version information.");
				return 1;
			}
			short *subBlock = null;
			uint len = 0;
			// 从版本信息获取区域设置信息:
			if (!Win32Imports.VerQueryValue (buffer, @"\VarFileInfo\Translation", out subBlock, out len))
			{
				Console.WriteLine("Failed to query version information.");
				return 1;
			}
			string spv = @"\StringFileInfo\" + subBlock[0].ToString("X4") + subBlock[1].ToString("X4") + @"\ProductVersion";
			byte *pVersion = null;
			// 获取此程序的 ProductVersion 值:
			string versionInfo;
			
			if (!Win32Imports.VerQueryValue (buffer, spv, out versionInfo, out len))
			{
				Console.WriteLine("Failed to query version information.");
				return 1;
			}
			Console.WriteLine ("ProductVersion == {0}", versionInfo);
		}
		catch (Exception e) 
		{
			Console.WriteLine ("Caught unexpected exception " + e.Message);
		}
      
		return 0;
	}
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明C# 中使用非托管代码(使用指针的代码)
喜欢 (0)
加载中……