C# 中如何获取变量内存地址?不使用unsafe 可以调用API。拜托了。救命用的。
解决方案 »
- DateTimePicker如何调整高度?
- 如何用递归遍历panel中的子控件
- 提示错误,未将对象引用设置到对象实例
- winform里的datagrid 如何修改标题样式?
- 哪位兄弟可知道怎么重绘treeview的滚动条?
- 请教一些关于WCF需要注意的问题
- 如何让WINFORM程序在网上邻居上共享运行,在使用Dns类不会出DnsPermission错误。
- 我现在想搞个象Windows自带的绘图工具,不知道应该用哪些控件,名字叫什么?请帮帮忙!谢谢,200分,另开贴给分!
- 一个枚举的问题?(在线等待!!)
- 数字的显示格式
- 如何实现dataGridview 只显示选中行
- 继续提问2:关于dataGridView缩放时候闪烁
int number; unsafe
{
// Assign the address of number to a pointer:
int* p = &number; // Commenting the following statement will remove the
// initialization of number.
*p = 0xffff; // Print the value of *p:
System.Console.WriteLine("Value at the location pointed to by p: {0:X}", *p); // Print the address stored in p:
System.Console.WriteLine("The address stored in p: {0}", (int)p);
} // Print the value of the variable number:
System.Console.WriteLine("Value of the variable number: {0:X}", number); System.Console.ReadKey();用这个又没什么不好的
int gc = gh.AddrOfPinnedObject().ToInt32();
gh.Free();
return gc;//变量地址
GCHandle handle = GCHandle.Alloc(obj, GCHandleType.Pinned);
IntPtr addr = handle.AddrOfPinnedObject();
//做一些事情。
//记住,不能像4楼那样释放了handle 返回一个地址,
//因为一旦handle 释放,地址将不再固定,很可能会改变。
//
handle.Free();
有什么特别的目的吗?
函数如下
/// <summary>
/// 生成重启控制卡命令的数据
/// </summary>
/// <param name="hObj">数据对象句柄</param>
/// <param name="pBuffer">结果数据缓冲区</param>
/// <param name="nBufSize">结果数据缓冲区的大小(字节)</param>
/// <returns>>0: 生成数据的长度(字节)-1: 不正确的数据对象句柄-4: 缓冲区长度不足</returns>
[DllImport("CP5000.dll",ExactSpelling=true, CharSet=CharSet.Ansi, SetLastError=true)]
public extern static int CP5000_MakeRestartSysData(long hObj, long pBuffer, int nBufSize);
第二个参数问开发硬件的人。他说是数组内存地址。第三个位数组的长度。
上面是我重新封装的。
原型如下
int CP5000_MakeWriteTimeData(HOBJECT hObj, BYTE *pBuffer, int nBufSize, const BYTE* pTimeBuffer)