C# 调 DLL 获取函数值的问题,请大神们帮个忙,谢了,两天了没有弄出来,在线等

xzweihe 2014-04-24 12:35:44
VC的接口
Bool GetKeyPadStatus( byte * addr, char * pstatus, char * pnumber,byte* ptocounter);

C# 调用
[DllImport("KeyPad.dll", CallingConvention = CallingConvention.StdCall)]
public static extern bool GetKeyPadstatus(ref byte addr, ref string pstatus, ref string pnumber, ref byte ptocounter); //获取终端状态函数

调用函数

byte address1 = 1;
//byte address2 = 2;
string pstatus = "0"; //////////这个值
string pnumber = "0000";
byte mtocounter = 0;//

bool temp = calldll.GetKeyPadstatus(ref address1, ref pstatus, ref pnumber, ref mtocounter);
if (temp)
{
switch (pstatus[0])
{
case '0':
label7.Text = "怎么是0 零 没有获得呼叫器的状态值";
break;
case 'L':
label7.Text = "空闲状态,按了清除键了";
break;
case 'E':
label7.Text = "暂停状态,按了暂停键";
break;
case 'W':
label7.Text = "等待" + pnumber;
break;
case 'P':
label7.Text = "呼叫" + pnumber; //重复呼叫
CallAndPrint();
break;
case 'M':
label7.Text = "转移" + pnumber + "到" + mtocounter.ToString() + "窗口";
break;
case 'S':
label7.Text = "正在受理" + pnumber; break;
case 'U':
label7.Text = "呼叫器要求派号";
byte waitnum = 100; //等待人数
calldll.setKeyPadnumber(ref address1, ref pnumber, waitnum);
Thread.Sleep(120);
CallAndPrint();
break;
}
}
最后获取的pstatus 值 是 0 也就是我开始刚给他的赋值

没有分了 只有25分了,昨天因为没分没能发帖。急死人!!

...全文
507 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
hartyu 2021-06-25
  • 打赏
  • 举报
回复

问题解决了吗? 

huihuipeng 2014-05-14
  • 打赏
  • 举报
回复
引用 22 楼 small21 的回复:
[quote=引用 21 楼 huihuipeng 的回复:] C# 调用 [DllImport("KeyPad.dll", EntryPoint = "GetKeyPadStatus" )] public static extern bool GetKeyPadstatus([MarshalAs(UnmanagedType.LPStr)] byte addr,[MarshalAs(UnmanagedType.LPStr)] string pstatus, [MarshalAs(UnmanagedType.LPStr)] string pnumber, [MarshalAs(UnmanagedType.LPStr)] byte ptocounter); 注意设置下程序入口
这个可以,用IntPtr的话就是先用Marshal类转化string为非托管的内存,可以得到一个IntPtr指针。[/quote] 如果解决了问题,请及时结贴。如果有没有,请继续询问和探讨
huihuipeng 2014-04-25
  • 打赏
  • 举报
回复
C# 调用 [DllImport("KeyPad.dll", EntryPoint = "GetKeyPadStatus" )] public static extern bool GetKeyPadstatus([MarshalAs(UnmanagedType.LPStr)] byte addr,[MarshalAs(UnmanagedType.LPStr)] string pstatus, [MarshalAs(UnmanagedType.LPStr)] string pnumber, [MarshalAs(UnmanagedType.LPStr)] byte ptocounter); 注意设置下程序入口
慧眼识狗熊 2014-04-25
  • 打赏
  • 举报
回复
引用 21 楼 huihuipeng 的回复:
C# 调用 [DllImport("KeyPad.dll", EntryPoint = "GetKeyPadStatus" )] public static extern bool GetKeyPadstatus([MarshalAs(UnmanagedType.LPStr)] byte addr,[MarshalAs(UnmanagedType.LPStr)] string pstatus, [MarshalAs(UnmanagedType.LPStr)] string pnumber, [MarshalAs(UnmanagedType.LPStr)] byte ptocounter); 注意设置下程序入口
这个可以,用IntPtr的话就是先用Marshal类转化string为非托管的内存,可以得到一个IntPtr指针。
xzweihe 2014-04-24
  • 打赏
  • 举报
回复
中午大家都吃饭啦,发帖发的真不是时候,自己顶
udng87 2014-04-24
  • 打赏
  • 举报
回复
学习,帮顶!
PiKaQiuPiKaPi 2014-04-24
  • 打赏
  • 举报
回复
看不出问题的,帮你顶下
xzweihe 2014-04-24
  • 打赏
  • 举报
回复
没有解决,自己再顶,不让沉下去啊
xzweihe 2014-04-24
  • 打赏
  • 举报
回复
引用 16 楼 tcmakebest 的回复:
全是带星号的,都用 IntPtr 吧,一定成功
用IntPtr 怎么写啊,没有写过,能帮忙写下吗?
tcmakebest 2014-04-24
  • 打赏
  • 举报
回复
全是带星号的,都用 IntPtr 吧,一定成功
F_bjtu_wxy 2014-04-24
  • 打赏
  • 举报
回复
哈哈,我也在弄这个,看你这几天挺纠结的,我也弄了几天了呢,我参考了一下这个空间,挺有帮助的,建议你看看,主要是后面那段的那个表格,祝你早日完成项目
xzweihe 2014-04-24
  • 打赏
  • 举报
回复
DELPHI 语言可以成功 获取返回值 C# 的 就是不能获得 应该不是 GetKeyPadstatus函数里面的问题
周美文 2014-04-24
  • 打赏
  • 举报
回复
应该不是你这边的问题吧。你去问问那个实现GetKeyPadstatus这个函数的人吧
iyomumx 2014-04-24
  • 打赏
  • 举报
回复
不要用ref string string对应char* ref string对应char**
zijiang001 2014-04-24
  • 打赏
  • 举报
回复
CallingConvention = CallingConvention.Cdecl 试试
xzweihe 2014-04-24
  • 打赏
  • 举报
回复
没有搞定啊,哪位大神还有方法吗?
xzweihe 2014-04-24
  • 打赏
  • 举报
回复
引用 6 楼 qin7wy5lp 的回复:
我觉得应该就是值传递和引用传递的问题..
能讲详细点吗?
xzweihe 2014-04-24
  • 打赏
  • 举报
回复
用 StringBuilder 返回的 值 是空
桃子先生 2014-04-24
  • 打赏
  • 举报
回复
我觉得应该就是值传递和引用传递的问题..
zijiang001 2014-04-24
  • 打赏
  • 举报
回复
嗯 把string都用StringBuilder替换试试,String的地址是会变的
加载更多回复(3)

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧