现在有个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#指针也不了解 ,不知道怎么使用
还望大侠们赐教
最好能给出比较详细的例子
ALength:读取长度:例:100
PReadData:返回数据指针 2、function WriteValue(AWriteStart:Integer;ALength:Integer;PWriteData:PInteger):Boolean;stdcall;
参数说明: AWriteStart:起始地址:例:00
ALength:写值字数长度:例:100
PWriteData:写入数据指针现在需要在C#里面使用,由于没用过Delphi,对C#指针也不了解 ,不知道怎么使用
还望大侠们赐教
最好能给出比较详细的例子
解决方案 »
- WPF与WinForm的抉择[转贴]
- 我想直接修改datagridview中的数据,但是不能实现功能代码如下
- 错误 1 “System.Windows.Forms.Application”并不包含“EnableVisualStyles”的定义
- 我应该如何开始学习C#(有DELPHI和SQL基础)?
- ★★★为什么绝大多数系统的用户名和密码不支持中文?★★★
- WebBrowser的使用问题
- 关于返回一个连接字符串,请高手帮解决一下,我是个新手,谢谢!!!
- C# 的设置特性的作用,列举举例 即给分!
- 谁用Head first c#的试验源码
- cookiecontainer 对象如何保存到文本?
- 如何调用自己封装的类库?
- C sharp 的 WebBroswer 里边载入的网页为什么比浏览器慢?
这个不搞清楚,咋都不行
[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);
{
[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);//只要这里显示出来一点东西,说明函数读取数据成功了
}
}
提示【外部组件发生异常】如果PReadData 是整型的 又如何修改谢谢
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()
{
[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;
写的时候 WriteValue() 返回True 不知道 算写进去没 因为在我PC测得 理论上是不让写的
WriteValue() ;PWriteData 用整型时 报错
尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
IntPtr 时
返回True 。
private static extern string ReadValue(AReadStart:Integer;ALength:Integer;PReadData:PInteger);
bool WriteValue(int AWriteStart;int ALength;IntPtr PWriteData);
这样不知道成不.
因为第三个参数是整形指针类型,你可以试着用ref试试public static extern void ReadValue(int AReadStart, int ALength, ref PReadData);
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;大侠们 帮帮忙
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);
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);
修改[加入函数说明以及调用方法][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);