请大家帮我结局一个c#调用win32 api dll的问题
C定义如下:
BOOL effnEDClientCheckPointInfoByTime(
efSTReadRtInfoRequest*pReqBuffer,//
efSTReadRtInfoResponse * pResBuffer,//
unsigned long Count //
);
结构如下:
#define EFMI_POINTNAMELENGTH 48
#define EFMI_POINTCOMMLENGTH EFMI_POINTNAMELENGTH
//查询实时记录请求包结构
typedef struct defefSTReadRtInfoRequest
{
long time; //
char pointName[EFMI_POINTNAMELENGTH+1];//EFMI_POINTNAMELENGTH+1;
char ednaServ[EFMI_POINTCOMMLENGTH+1]; //
}efSTReadRtInfoRequest;
//查询实时记录回复包结构
typedef struct defefSTReadRtResponse
{
double value;
long time;
unsigned short status;//
char pointName[EFMI_POINTNAMELENGTH+1];//
}efSTReadRtInfoResponse;
/////////////////////////////
//c#的程序如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace ednaClient
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(100, 76);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(80, 24);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(100, 168);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(152, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(416, 318);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
[StructLayout(LayoutKind.Sequential,CharSet= CharSet.Unicode)]
public struct defefSTReadRtInfoRequest
{
public Int32 time;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 48)]
public string pointName;//
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 48)]
public string ednaServ;//
}
//****************************************************************
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct defefSTReadRtResponse
{
public double value;
public Int32 time;
public UInt16 status;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 48)]
public string pointName;//
};
[DllImport(@"efEDClient.dll", EntryPoint = "effnEDClientInit", CharSet = CharSet.Unicode)]
public static extern bool effnEDClientInit();
[DllImport(@"efEDClient.dll", EntryPoint = "effnEDClientCheckPointInfoByTimeEx", CharSet = CharSet.Unicode)]
public static extern bool effnEDClientCheckPointInfoByTimeEx(ref defefSTReadRtInfoRequest requestBuffer, out defefSTReadRtResponse responseBuffer, UInt32 count);
private void button1_Click(object sender, System.EventArgs e)
{
if(!effnEDClientInit())
{
textBox1.Text="网络不通,初始化参数可能有问题!";
return;
}
string[] p = new string[4] { "GWDCS01.UI_PUSH.LBACT103", "GDHYSSIS.U31_RTS.1TURC:J15_01.PNT", "GDHYSSIS.U31_RTS.1TURC:J15_04.PNT", "GDHYSSIS.U31_RTS.1TURC:J15_11.PNT"};
uint nCount=2;
try
{
for(int i = 0; i < nCount; i++)//显示输出数据
{
defefSTReadRtInfoRequest req = new defefSTReadRtInfoRequest();
System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding();
string arrP, arrServ;
arrP = p[i];
arrServ = "GWDCS01.UI_PUSH";
req.pointName = arrP.PadRight(48);
req.ednaServ = arrServ.PadRight(48);
// Array.Copy(converter.GetBytes(arrP), 0, req.pointName,0, converter.GetBytes(arrP).Length);
// Array.Copy(converter.GetBytes(arrServ), 0, req.ednaServ,0, converter.GetBytes(arrServ).Length);
req.time = 0;
defefSTReadRtResponse iResp = new defefSTReadRtResponse();
iResp.pointName= new string(' ',48);
if (effnEDClientCheckPointInfoByTimeEx(ref req, out iResp, nCount))
{
Console.WriteLine( "值:" + iResp.value.ToString() + "---" + iResp.status.ToString() + "time:" + iResp.time.ToString());
}
}
}
catch(Exception ex)
{
Console.Write(ex.Message);
}
}
}
}
返回的结果是错误的,我想肯定是传入的参数有问题