日期:2014-05-18  浏览次数:20911 次

C#调用新农医系统第三方接口VC动态库,参数返回值为空,求解,JFNetLib,SetRemoteServerAddr,SendRequestPack
代码如下:
Int64 dk = 8800;
StringBuilder sbIP = new StringBuilder("192.168.0.80");
StringBuilder sbCS = new StringBuilder("EXCHCODE=GY01&HOSCODE=0C01");
StringBuilder sbJG = new StringBuilder(1024);
SetRemoteServerAddr(dk, sbIP);
int cnt = (int)SendRequestPack(sbCS, sbCS.Length, sbJG, sbJG.Length, 200);
MessageBox.Show(cnt.ToString());
MessageBox.Show(sbJG.ToString());

cnt.ToString()结果为1,表示“连接医院前置机服务器失败”,
sbJG.ToString()结果为空,求各位高手赐教,

引用代码:
public class Sxnb
{
[DllImport("JFNetLib.dll", EntryPoint = "SetRemoteServerAddr")]
public static extern void SetRemoteServerAddr(Int64 nSvrPort, StringBuilder szSvrAddr);

[DllImport("JFNetLib.dll", EntryPoint = "SendRequestPack")]
public static extern Int64 SendRequestPack(StringBuilder sendbuf, Int64 sendlen, StringBuilder recvbuf, Int64 recvlen, Int64 waitsecs);
}

------解决方案--------------------
C# code
[DllImport("JFNetLib.dll", EntryPoint = "SetRemoteServerAddr")]
public static extern void SetRemoteServerAddr(Int64 nSvrPort, string szSvrAddr);

[DllImport("JFNetLib.dll", EntryPoint = "SendRequestPack")]
public static extern Int64 SendRequestPack(string sendbuf, Int64 sendlen, StringBuilder recvbuf, out Int64 recvlen, Int64 waitsecs);

Int64 dk = 8800;
string sbIP = "192.168.0.80";
string sbCS = "EXCHCODE=GY01&HOSCODE=0C01";
StringBuilder sbJG = new StringBuilder(65535);
long length;
SetRemoteServerAddr(dk, sbIP);
int cnt = (int)SendRequestPack(sbCS, sbCS.Length, sbJG, out length, 200);
MessageBox.Show(cnt.ToString());
MessageBox.Show(sbJG.ToString());

------解决方案--------------------
1、把所有Int64改为Int32。因为在C++中long类型其实和int一样只是32位的
2、int cnt = (int)SendRequestPack(sbCS, sbCS.Length, sbJG, sbJG.Length, 200);
传入sbJG.Length是不对的。因为sbJG创建后只是提供了1024字节的容量,但其实际长度Length = 0。可以传入sbJG.Capacity或者小于sbJG.Capacity的实际希望读取的字节数目