using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Forms;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using nativeCom;

public class funs{
	static funs _ins;
	public static funs instance{get{if(_ins==null)_ins=new funs();return _ins;}set{_ins=value;}}
	
	public  byte[] StructToBytes(object structObj)
	{
		int size = Marshal.SizeOf(structObj);
		IntPtr buffer = Marshal.AllocHGlobal(size);
		try
		{
			Marshal.StructureToPtr(structObj, buffer, false);
			byte[] bytes = new byte[size];
			Marshal.Copy(buffer, bytes, 0, size);
			return bytes;
		}
		finally
		{
			Marshal.FreeHGlobal(buffer);
		}
	}
	
	unsafe public object clone(object obj){
		
		byte[] bt=StructToBytes(obj);
		fixed(byte* pb=bt ){
			IntPtr itp=new IntPtr(pb);
			
			return Marshal.PtrToStructure(itp,	obj.GetType());
		}
		
	}
	ABlock a1=new ABlock();
	ABlock a2=new ABlock();
	/// <summary>
	/// 把bt总的数值 写入到ab指针中 偏移ofs个值
	/// </summary>
	/// <param name="bt"></param>
	/// <param name="ab"></param>
	/// <param name="ofs"></param>
	
	unsafe public void memToNative_bytes(byte[] bt,ref ABlock ab,int ofs){
		fixed(byte* pb=bt ){
			IntPtr pt=	new IntPtr( (int)ab.pt+ofs);
			
			NativeDllMethod.nativefun("1",((int)pb  ).ToString()+'*'+pt.ToString()+'*'+bt.Length.ToString(),ref a1,ref a2);
//
//			myCom_zyx.Instance.move2Process((int*)pb,new IntPtr( (int)ab.pt+ofs),bt.Length);
		}
		
		
	}
	
	
	unsafe public void memToNative_bytes_(byte[] bt,int addr_){
		ABlock ab= new ABlock();
		int* ttt=(int*)&ab;
		myCom_zyx.Instance.getDataOfStruct<ABlock>(addr_, ref ab,ttt);
		memToNative_bytes(bt,ref ab,0);
		
		
	}
	
	/// <summary>
	/// 获取服务器所在的路径
	/// </summary>
	/// <returns></returns>
	public string getSeverPath(){
		return Application.StartupPath;
	}
	
	/// <summary>
	/// 获取服务规则路径
	/// </summary>
	/// <returns></returns>
	public string getGzPath(){	//待换
		return getSeverPath() + "/mjgz.xml";
	}
}