首页新闻找找看学习计划

C#调用C的dll

0
悬赏园豆:30 [已解决问题] 解决于 2017-05-16 08:37

代码如下:

[DllImport("Sign64.dll", CharSet = CharSet.Auto)]
public static extern int Sign(byte[] src, int srcLen, out byte[] sign, out int signLen, byte[] pwd);

 

//加签
string teststr = "yq";

//输入数据
byte[] inputMsg=Encoding.UTF8.GetBytes(teststr);
int inputMsgLength = inputMsg.Length;
//输出数据
byte[] outputMsg=new byte[128];
int outputMsgLength = outputMsg.Length;

//var ptr = Marshal.AllocHGlobal(4);
//Marshal.WriteInt32(ptr, outputMsgLength);
//密码
byte[] pwd = Encoding.Default.GetBytes("88888888");
int IsOkWithSign = ICHepler.Sign(inputMsg, inputMsgLength, out outputMsg, out outputMsgLength, pwd); 

 

 

c函数原型   UINT Sign(BYTE *src, UINT srcLen, BYTE *sign, UINT* signLen, const char * pwd)

但是就是没有办法得到正确的返回值

这下我是一只大胖墩的主页 这下我是一只大胖墩 | 初学一级 | 园豆:6
提问于:2017-05-12 17:00
< >
分享
最佳答案
0

可能是你PINVOKE的方式不对,把你原始的C++的dll声明贴出来看看。实在不行可以使用C++/CLI调用。

收获园豆:30
天方 | 大侠五级 |园豆:5262 | 2017-05-14 20:05

UINT Sign(BYTE *src, UINT srcLen, BYTE *sign, UINT* signLen, const char * pwd)

这下我是一只大胖墩 | 园豆:6 (初学一级) | 2017-05-14 20:07

@不会代码的椰子: 

这种方式一般是将byte* 封装为IntPtr的,试试这种方式:

    [DllImport("Sign64.dll", CharSet = CharSet.Auto)]
    public static extern uint Sign(IntPtr src, int srcLen, IntPtr sign, ref int signLen,
                                    [In] [MarshalAs(UnmanagedType.LPStr)] string pwd);


    var inputMsgPtr = Marshal.AllocHGlobal(inputMsg.Length);
    Marshal.Copy(inputMsg, 0, inputMsgPtr, inputMsg.Length);

    var outputMsgPtr = Marshal.AllocHGlobal(outputMsg.Length);
    Marshal.Copy(outputMsg, 0, outputMsgPtr, outputMsg.Length);
            
    int outputLen = 0;
    Sign(inputMsgPtr, inputMsg.Length, outputMsgPtr, ref outputLen, "88888888");


    Marshal.Copy(outputMsgPtr, outputMsg, 0, outputLen);

    Marshal.FreeHGlobal(inputMsgPtr);
    Marshal.FreeHGlobal(outputMsgPtr);
天方 | 园豆:5262 (大侠五级) | 2017-05-14 21:23

@天方: 先谢过。。。

这下我是一只大胖墩 | 园豆:6 (初学一级) | 2017-05-14 21:26

@天方: 恩恩····确实这样写就没有问题了·····还有一个问题想要请教·用SHA1对报文进行摘要时,多一个空格和少一个空格都是不同的,那么这个标准有没有规定的····

这下我是一只大胖墩 | 园豆:6 (初学一级) | 2017-05-15 16:11

@天方: 规定比如:只要报文中有空格就去掉之类的?

这下我是一只大胖墩 | 园豆:6 (初学一级) | 2017-05-15 16:41

@不会代码的椰子: 

空格本来就是内容啊,多一个空格内容都变了,sha后的结果不一样不很正常嘛,这个有什么问题呢。

天方 | 园豆:5262 (大侠五级) | 2017-05-15 21:24

@天方: 我拿到别人发来的加签之后的报文自己再把签名部分去掉怎么都得不到一样的结果······比如啊<?xml   ....?>这个头要不要算入价签的里面?还有就是···········然后···········反正·····就是不一样

我贴个加签之后的你给看看······跪谢!

<ceb:CEB625Message xmlns:ceb="http://www.chinaport.gov.cn/ceb" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" guid="FT23DGGB-9940-415B-B666-C09BOO7F4033" version="1.0">
<ceb:InvtRefund>
<ceb:InvtRefundHead>
<ceb:guid>5923DGGB-9940-415B-B666-C09BOO7F40B2</ceb:guid>
<ceb:appType>1</ceb:appType>
<ceb:appTime>20160310112703</ceb:appTime>
<ceb:appStatus>2</ceb:appStatus>
<ceb:customsCode>8030</ceb:customsCode>
<ceb:orderNo>order20160317119400055</ceb:orderNo>
<ceb:ebpCode>1105910159</ceb:ebpCode>
<ceb:ebpName>娴嬭瘯浼佷笟</ceb:ebpName>
<ceb:ebcCode>1105910158</ceb:ebcCode>
<ceb:ebcName>娴嬭瘯浼佷笟</ceb:ebcName>
<ceb:logisticsNo>L2016031000001</ceb:logisticsNo>
<ceb:logisticsCode>1101081122</ceb:logisticsCode>
<ceb:logisticsName>娴嬭瘯鐗╂祦浼佷笟</ceb:logisticsName>
<ceb:copNo>cop201604210001</ceb:copNo>
<ceb:invtNo>15003213300400111</ceb:invtNo>
<ceb:buyerIdType>1</ceb:buyerIdType>
<ceb:buyerIdNumber>130681196203023331</ceb:buyerIdNumber>
<ceb:buyerName>鐜嬫柟</ceb:buyerName>
<ceb:buyerTelephone>13936921298</ceb:buyerTelephone>
<ceb:agentCode>1105910158</ceb:agentCode>
<ceb:agentName>娴嬭瘯浼佷笟</ceb:agentName>
<ceb:reason>1105910158</ceb:reason>
</ceb:InvtRefundHead>
<ceb:InvtRefundList>
<ceb:gnum>1</ceb:gnum>
<ceb:gcode>2412342134</ceb:gcode>
<ceb:gname>鍙箰</ceb:gname>
<ceb:qty>12</ceb:qty>
<ceb:unit>007</ceb:unit>
</ceb:InvtRefundList>
</ceb:InvtRefund>
<ceb:BaseTransfer>
<ceb:copCode>JDGW20160310001</ceb:copCode>
<ceb:copName>浜笢璐墿</ceb:copName>
<ceb:dxpMode>DXP</ceb:dxpMode>
<ceb:dxpId>EXP2016031000000001</ceb:dxpId>
<ceb:note></ceb:note>
</ceb:BaseTransfer>
<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"></ds:CanonicalizationMethod>
<ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"></ds:SignatureMethod>
<ds:Reference URI="">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"></ds:Transform>
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"></ds:DigestMethod>
<ds:DigestValue>Rb4mpnhn9t0TL0J/0sr/VeWTQ9Y=</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>
LxybvD+3eAIaa191cF2lX+rKu1hMDY2QPK75mbR8/vxKE3URtstMYDSxAAmlMM5Ia7sB1ptPuqTv
1kYwlZ4W5AIVwAFgwcM3ZjHv5KfQy/D9yV63Y77XoJ5f0qxceHlN1bJecbD8yYIGUmL0RHllp3Ts
yBVzrBZhR/sVnCuLK+c=
</ds:SignatureValue>
<ds:KeyInfo>
<ds:KeyName>0001</ds:KeyName>
</ds:KeyInfo>
</ds:Signature></ceb:CEB625Message>

 

乱码哈····不过不重要哈···重要的是我根据这个加签之后的报文自己用SHA1算出来的报文摘要跟他的不一样。。。我就把格式换过去换过来的还是不一样。

这下我是一只大胖墩 | 园豆:6 (初学一级) | 2017-05-15 21:39

@这下我是一只大胖墩: 这个我知道  hg业务吧

lchevalier | 园豆:209 (菜鸟二级) | 2018-06-16 20:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册