100分求救 C# 调用C写的Dll并传递参数 在线等 本帖最后由 kernel001 于 2011-12-07 09:33:11 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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, CharSet = CharSet.Unicode)] public struct WORKFILE { int filehead; uint x;//假如是文件索引,则保存序号 uint y;//假如是文件索引,则保存WORKINS位置 uint z; 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.Unicode, SetLastError = true)] public static extern bool YM_DataProcess(int method, string path, IntPtr lpData, double mapwi, double maphi, int coormethod); 楼上的有几个小小的问题:1、C#结构体成员默认访问标识为private,需要改为public2、C语言默认的字符集为单字节的,不能使用Unicode3、WIN32的BOOL类型不能简单转换为bool,因为它是4个字节正确的转换: [StructLayout(LayoutKind.Sequential)] public struct WORKFILE { public int filehead; public uint x;//假如是文件索引,则保存序号 public uint y;//假如是文件索引,则保存WORKINS位置 public uint z; public uint u; } [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); 感谢两位的回答另外我想咨询怎么样把我定义的WORKFILE 内存指针 传到 IntPtr lpData中来,要怎么样转换。WORKFILE wkf= new WORKFILE();wkf.filehead = 0;wkf.x = 0; YM_DataProcess(1, "r:\\1.dig", wkf,0, 0, 0); 使用 ref WORKFILE作为参数类型public static extern bool YM_DataProcess(int method, string path, ref WORKFILE wf, double mapwi, double maphi, int coormethod); [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] 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, ref WORKFILE lpData, double mapwi, double maphi, int coormethod); static void Main(string[] args) { WORKFILE wf = new WORKFILE(); wf.x = 0; wf.y = 0; wf.z = 0; bool ret = YM_DataProcess(1, "r:\\1.dig", ref wf, 0.0, 0.0, 0); }我就想传Intptr怎么老不对呢,原来是workfile的ref啊 无法连接邮件服务器是怎么回事? Microsoft.Office.Interop.Owc11导出excel的问题! 如何将http://flash.weather.com.cn/sk2/101090101.xml保存到本地 如何把textbox里的路径传递给外部调用的EXE程序 C#,具体如何打印TextBox的内容? 关于richTextBox保存的问题,在线等,高手给点指点!!! 紧急文件 求高手帮我做个小系统 C# 访问SQL ,代码纠错!简单~~~ 在水晶报表里怎样分页小计项? 在VS中得SQL语句。。。。 base64字符无效的问题
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, CharSet = CharSet.Unicode)]
public struct WORKFILE
{
int filehead;
uint x;//假如是文件索引,则保存序号
uint y;//假如是文件索引,则保存WORKINS位置
uint z;
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.Unicode, SetLastError = true)]
public static extern bool YM_DataProcess(int method, string path, IntPtr lpData,
double mapwi, double maphi, int coormethod);
1、C#结构体成员默认访问标识为private,需要改为public
2、C语言默认的字符集为单字节的,不能使用Unicode
3、WIN32的BOOL类型不能简单转换为bool,因为它是4个字节正确的转换:
[StructLayout(LayoutKind.Sequential)]
public struct WORKFILE
{
public int filehead;
public uint x;//假如是文件索引,则保存序号
public uint y;//假如是文件索引,则保存WORKINS位置
public uint z;
public uint u;
} [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);
WORKFILE wkf= new WORKFILE();
wkf.filehead = 0;
wkf.x = 0;
YM_DataProcess(1, "r:\\1.dig", wkf,0, 0, 0);
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,
ref WORKFILE lpData, double mapwi, double maphi, int coormethod); static void Main(string[] args)
{
WORKFILE wf = new WORKFILE();
wf.x = 0;
wf.y = 0;
wf.z = 0;
bool ret = YM_DataProcess(1, "r:\\1.dig", ref wf, 0.0, 0.0, 0);
}
我就想传Intptr怎么老不对呢,原来是workfile的ref啊