现在有个Delphi 写的Dll 文件   里面有两个函数:  1、procedure ReadValue(AReadStart:Integer;ALength:Integer;PReadData:PInteger);stdcall;    参数说明:AReadStart:起始地址:例:00 
              ALength:读取长度:例:100
              PReadData:返回数据指针  2、function WriteValue(AWriteStart:Integer;ALength:Integer;PWriteData:PInteger):Boolean;stdcall;
     参数说明:       AWriteStart:起始地址:例:00
                      ALength:写值字数长度:例:100
                      PWriteData:写入数据指针现在需要在C#里面使用,由于没用过Delphi,对C#指针也不了解 ,不知道怎么使用
还望大侠们赐教
最好能给出比较详细的例子

解决方案 »

  1.   

    PReadData是什么数据结构的指针?
    这个不搞清楚,咋都不行
      

  2.   

    PReadData是什么数据结构 我也不太清楚只知道这个功能是用来读取硬件指定区域的数据没玩过硬件打交道的东西
      

  3.   

    我试着这样在C#里定义 但是 报返回类型有问题 
    [DllImport("Omron_R.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
            /// <summary>
            /// 从指定位置读值
            /// </summary>
            /// <param name="AReadStart">起始地址:例:00</param>
            /// <param name="ALength">读取长度:例:100</param>
            /// <param name="PReadData">返回数据指针</param>
            public static extern byte[] ReadValue(int AReadStart, int ALength, byte[] PReadData);
      

  4.   

    PReadData 为字符串的时候怎么处理呢
      

  5.   

    假定是字符串(char*),按下面的办法试试:class Program
        {       
            [DllImport("Omron_R.dll")]
            public static extern void ReadValue(int AReadStart, int ALength, IntPtr PReadData);
            static void Main(string[] args)
            {
                const int LEN = 256;
                IntPtr p = Marshal.AllocHGlobal(LEN);//分配256个字节内存
                for (int i = 0; i < LEN / 64; i++) 
                {
                    Marshal.WriteInt64(p, 0);//256个字节全部清零
                }            ReadValue(0, LEN, p);//调用非托管函数获取数据            byte[] buff = new byte[LEN];
                Marshal.Copy(p, buff, 0, LEN);            string result = Encoding.ASCII.GetString(buff);//如果出来的字符串不对,用双字节字符集试试            Console.WriteLine(result);//只要这里显示出来一点东西,说明函数读取数据成功了
            }
        }
      

  6.   

    不行啊
    提示【外部组件发生异常】如果PReadData 是整型的 又如何修改谢谢
      

  7.   

    ReadValue(0, LEN, p);//调用非托管函数获取数据  时出错
      

  8.   

    没有具体提示,写的时候倒提示 内存受保护未处理 System.Runtime.InteropServices.SEHException
      Message="外部组件发生异常。"
      Source="TEST"
      ErrorCode=-2147467259
      StackTrace:
           在 TEST.PLC.ReadValue(Int32 AReadStart, Int32 ALength, IntPtr PReadData)
           在 TEST.Form1.a() 位置 d:\我的文档\Visual Studio 2005\Projects\TEST\TEST\Form1.cs:行号 49
           在 TEST.Form1.Form1_Load(Object sender, EventArgs e) 位置 d:\我的文档\Visual Studio 2005\Projects\TEST\TEST\Form1.cs:行号 36
           在 System.Windows.Forms.Form.OnLoad(EventArgs e)
           在 System.Windows.Forms.Form.OnCreateControl()
           在 System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
           在 System.Windows.Forms.Control.CreateControl()
           在 System.Windows.Forms.Control.WmShowWindow(Message& m)
           在 System.Windows.Forms.Control.WndProc(Message& m)
           在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
           在 System.Windows.Forms.ContainerControl.WndProc(Message& m)
           在 System.Windows.Forms.Form.WmShowWindow(Message& m)
           在 System.Windows.Forms.Form.WndProc(Message& m)
           在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           在 System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
           在 System.Windows.Forms.Control.SetVisibleCore(Boolean value)
           在 System.Windows.Forms.Form.SetVisibleCore(Boolean value)
           在 System.Windows.Forms.Control.set_Visible(Boolean value)
           在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           在 System.Windows.Forms.Application.Run(Form mainForm)
           在 TEST.Program.Main() 位置 d:\我的文档\Visual Studio 2005\Projects\TEST\TEST\Program.cs:行号 17
           在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
           在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           在 System.Threading.ThreadHelper.ThreadStart()
      

  9.   

    class Program
        {       
            [DllImport("Omron_R.dll")]
            public static extern void ReadValue(int AReadStart, int ALength, IntPtr PReadData);
            static void Main(string[] args)
            {
                const int LEN = 64;
                IntPtr p = Marshal.AllocHGlobal(LEN*4);//分配256个字节内存            ReadValue(0, LEN, p);//调用非托管函数获取数据            int[] buff = new int[LEN];
                Marshal.Copy(p, buff, 0, LEN);
            }
        }http://hi.baidu.com/wyevon/blog/item/3dbf1d122002cb095baf5397.htmlDelphi 已经为很多类型预定义了指针, 譬如数据类型: 
    Integer 有对应的 PInteger;
      

  10.   

    还是不行。
     写的时候 WriteValue() 返回True 不知道 算写进去没 因为在我PC测得 理论上是不让写的
      

  11.   

    本地测试
    WriteValue() ;PWriteData 用整型时 报错
    尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
    IntPtr 时
    返回True 。
      

  12.   

    [DllImport("delphi.dll", EntryPoint = "ReadValue")]
    private static extern string ReadValue(AReadStart:Integer;ALength:Integer;PReadData:PInteger);
      

  13.   

    PReadData和PWriteData是结构体ReadData和WriteData的指针,你必须知道这两个结构体的定义才行,不知道是不是dephi里预定义的结构,你查下帮助
      

  14.   

    void ReadValue(int AReadStart;int ALength;IntPtr PReadData);
    bool WriteValue(int AWriteStart;int ALength;IntPtr PWriteData);
    这样不知道成不.
      

  15.   

    你这是托管调用非托管,用[DllImport]加载dll,
    因为第三个参数是整形指针类型,你可以试着用ref试试public static extern void ReadValue(int AReadStart, int ALength, ref PReadData);
      

  16.   

    搞不定啊 在Delphi 是这样用的  TReadValue=procedure(AReadStart:Integer;ALength:Integer;PReadData:PInteger);stdcall; var
      FSendValue:TReadValue;
      i:Integer;
      FAryRead:array of Integer;
    begin
      SetLength(FAryRead,StrToInt(Trim(edtlength.Text))*2+14);
      if Trim(mmo1.Text)<>'' then
      mmo1.Text:=mmo1.Text+#13#10;
      FDes:=Copy(edtdes.Text,LastDelimiter('.',edtdes.Text)+1,Length(edtdes.Text)-LastDelimiter('.',edtdes.Text));
      FSendValue:=GetProcAddress(MStatusM,'ReadValue');
      FSendValue(StrToInt(Trim(edtstart.Text)),StrToInt(Trim(edtlength.Text)),Pinteger(FAryRead));
     
      for i:=0 to StrToInt(Trim(edtlength.Text))+13 do
      begin
        if i<14 then
        Continue;
        if i=14 then
        mmo1.Text:=mmo1.Text+inttostr(faryread[i])
        else
        mmo1.Text:=mmo1.Text+'  '+inttostr(faryread[i]);
      end;
     
    end;大侠们 帮帮忙
      

  17.   

    第三个参数应该就是个整形数组的指针,
    FAryRead:array of Integer;
    SetLength(FAryRead,StrToInt(Trim(edtlength.Text))*2+14);
    FSendValue(StrToInt(Trim(edtstart.Text)),StrToInt(Trim(edtlength.Text)),Pinteger(FAryRead));试试这个
    [DllImport("Omron_R.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
      /// <summary>
      /// 从指定位置读值
      /// </summary>
      /// <param name="AReadStart">起始地址:例:00</param>
      /// <param name="ALength">读取长度:例:100</param>
      /// <param name="PReadData">返回数据指针</param>
      public static extern void ReadValue(int AReadStart, int ALength, out System.IntPtr PReadData);调用时
    int readStart = xxx;
    int length = xxx;
    System.IntPtr buffer = IntPtr.Zero;
    ReadValue( readStart , length , out buffer);
      

  18.   

    FSendValue:=GetProcAddress(MStatusM,'ReadValue'); //这句是得到动态库中的ReadValue函数的地址
      FSendValue(StrToInt(Trim(edtstart.Text)),StrToInt(Trim(edtlength.Text)),Pinteger(FAryRead));//这句是取值,最后一个参数应该是int [] arrRead;试一下这个呢:[DllImport("Omron_R.dll", EntryPoint = "ReadValue")]
    static extern void ReadValue(int AReadStart, int ALength, byte[] ReadData);
      

  19.   

    不能修改回帖,唉!
    修改[加入函数说明以及调用方法][DllImport("Omron_R.dll", EntryPoint = "ReadValue")]  /// <summary>
      /// 从指定位置读值
      /// </summary>
      /// <param name="readStart">开始地址: 例:0x00</param>
      /// <param name="length">读取长度:例:100</param>
      /// <param name="readData">返回数据</param>
    static extern void ReadValue(int readStart, int length, byte[] readData);调用:
        int readStart = 0x00;  //读取的开始开始
        int length = 16;       //读取的长度
        int [] readData = new int [32];    ReadValue(readStart, length, readData);
      

  20.   

    static extern void ReadValue(int readStart, int length, byte[] readData);byte错了,应该为int