| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 509 人关注过本帖, 2 人收藏
标题:C#调用dll问题。原型说明都有。
只看楼主 加入收藏
gjm
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-3-27
收藏(2)
 问题点数:0 回复次数:2 
C#调用dll问题。原型说明都有。
调用test.dll:
long oa(unsigned long msgshow,unsigned long p,unsigned long c,char *str,LPTSTR com);
参数说明:
msgshow:业务类型;p:格式类型; c:长度;str:数据串;调用时,通过数据串传入参数;函数返回时,数据串中包含返回的数据; com:数据串口
使用注意:函数返回时向数据串中进行写操作。
 函数返回值:函数通过字符串和函数返回值来返回函数的执行结果
 当函数返回值等于0时,表示成功,字符串中包含了业务处理后返回的数据
 当函数返回值不等于0时,出现错误。

我的做法是:
[DllImport("test.dll", ExactSpelling = true, SetLastError = true)]
public static extern int oa(UInt32 msgshow, UInt32 p, UInt32 c, StringBuilder str, string com);
   private void button_dq_Click(object sender, EventArgs e)
   {
   StringBuilder strS=new StringBuilder();
   if (oa(1001, 101, 126, strS, "AUTO") == 0)
   {
   textBox_xx.Text = strS.ToString();
   MessageBox.Show(oa(1001, 101, 126, strS, "AUTO").ToString());
   }
   else
   {
   MessageBox.Show("错误:" + oa(1001, 101, 126, strS, "AUTO").ToString());
   }
   }

但是总是最后错误接口参数错误。。。
搜索更多相关主题的帖子: 字符串 
2011-07-12 09:53
yfl168648
Rank: 2
等 级:论坛游民
帖 子:4
专家分:16
注 册:2011-7-15
收藏
得分:0 
StringBuilder 我估计问题出在这里了
这是你自己弄的类型吗?
诚恳的说用Byte[]
2011-07-15 08:54
303770957
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:838
专家分:2125
注 册:2005-9-10
收藏
得分:0 
CSharp 应该这样申明dll中的oa函数。如下所示:
using System.Runtime.InteropServices;
public partial class NativeMethods {  
    /// Return Type: int
    ///msgshow: unsigned int
    ///p: unsigned int
    ///c: unsigned int
    ///str: char*
    ///com: LPTSTR->LPWSTR->WCHAR*
    [DllImportAttribute("test.dll", EntryPoint="oa")]
public static extern  int oa(
    uint msgshow,
    uint p,
    uint c,
    System.IntPtr str,
    [MarshalAsAttribute(UnmanagedType.LPWStr)] System.Text.StringBuilder com
    ) ;
}

♂ 死后定当长眠,生前何须久睡。♀
2013-06-25 17:50
快速回复:C#调用dll问题。原型说明都有。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017063 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved