C++代码转C#,转换与传递参数,共享内存,不知道如何写
#define EVENTDELAY 3 //延时
#define XYRUNSPEED 10 //x,y运动速度
#define IO_INPUT 11 //输入IO检测
#define IO_OUTPUT 12 //输出IO检测
#define LINECOORSPACE 24 //运动到某位置
#define ResetMethod 29 //复位动作
#define ZUAxesSpeedS 40 //Z,U轴速度
#define XY_Move_Offset 206 //相对位置移动
#define XYZ_Move_s 208 //三轴联动移动
typedef struct _WORKFILE_
{
long filehead;
unsigned long x;//假如是文件索引,则保存序号
unsigned long y;//假如是文件索引,则保存WORKINS位置
unsigned long z;
unsigned long u;
}WORKFILE;
#define Mac_Coor_LT 0
#define Mac_Coor_RT 1
#define Mac_Coor_LD 2
#define Mac_Coor_RD 3
typedef BOOL(WINAPI*YM_DataProce)(long method,LPCTSTR path,void*lpData,double mapwi,double maphi,long coormethod);
void CShowtestDlg::OnButton2()
{
rwFiles rw;
CString strLangFilePath,path,filepath;
LPTSTR szAppPath = strLangFilePath.GetBuffer(_MAX_PATH);
GetModuleFileName(NULL,szAppPath,_MAX_PATH);
strLangFilePath.ReleaseBuffer();
strLangFilePath = strLangFilePath.Mid(0,strLangFilePath.ReverseFind(_T('\\')));
YM_DataProce tz_process;
path = strLangFilePath+"\\DigOut.dll";
disDll = LoadLibrary(path);
tz_process = (YM_DataProce)GetProcAddress(disDll,"YM_DataProcess");
if(rw.FileSavePathDlg(&filepath,"dig"))
{
BYTE * lpbuf = NULL;
WORKFILE*workf;
lpbuf = new BYTE[200];
workf = (WORKFILE*)lpbuf;
workf->filehead = 0;//第一个结构这个属性无意义,可为任何值
workf->x = 3;//表示后面跟3个结构
workf++;
workf->filehead = EVENTDELAY;
workf->x = 1000;//1秒
workf++;
workf->filehead = XYZ_Move_s;
workf->x = 5000;//x(微米)
workf->y = 20000;//y坐标(微米)
workf->z = 6000;//z坐标(微米)
workf++;
workf->filehead = XYZ_Move_s;
workf->x = 0;//x(微米)
workf->y = 0;//y坐标(微米)
workf->z = 0;//z坐标(微米)
workf++;
//注意第4,第5个参数是毫米单位
tz_process(0,filepath,lpbuf,5,20,Mac_Coor_LT);
}
//对应C#代码
public partial class Form1 : Form
{
private const int EVENTDELAY = 3; //延时
private const int XYRUNSPEED = 10;//x,y运动速度
private const int IO_INPUT = 11; //输入IO检测
private const int IO_OUTPUT = 12; //输出IO检测
private const int LINECOORSPACE = 24; //运动到某位置
private const int ResetMethod = 29; //复位动作
private const int ZUAxesSpeedS = 40;//Z,U轴速度
private const int XY_Move_Offset = 206; //相对位置移动
private const int XYZ_Move_s = 208; //三轴联动移动
[StructLayout(LayoutKind.Sequential)]
public struct WORKFILE
{
public int filehead;
public uint x;//假如是文件索引,则保存序号
public uint y;//假如是文件索引,则保存WORKINS位置
public uint z;
public uint u;
}
private const int Mac_Coor_LT = 0;
private const int Mac_Coor_RT = 1;
private const int Mac_Coor_LD = 2;
private const int Mac_Coor_RD = 3;
[DllImport("DigOut.dll", CharSet = CharSet.Ansi)]
[return: MarshalAs(UnmanagedType.Bool)]//4 字节布尔值(true != 0、false = 0)。这是 Win32 BOOL 类型。
//public static extern bool YM_DataProcess(int method, string path, IntPtr lpData,
//double mapwi, double maphi, int coormethod);
public static extern bool YM_DataProcess(int method, string path, ref WORKFILE wf, double mapwi, double maphi, int coormethod);
//[DllImport("DigOut.dll", CharSet = CharSet.Unicode, SetLastError = true)]
//public static extern bool YM_DataProcess(int method, string path, IntPtr lpData,
// double mapwi, double maphi, int coormethod);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//对应OnButton2()
//这一段不知道如何写 将内存传入到C++的DLL中。
//YM_DataProcess(1, "r:\\1.dig", ref WORKFILE, 0, 0, 0);
}
}
如果有需要 C的源代码可以向我索取。