现在的位置: 首页 > 综合 > 正文

关于用.Net平台开发SPMS_API开发接口里的PChar说明——转载

2012年06月10日 ⁄ 综合 ⁄ 共 1861字 ⁄ 字号 评论关闭

SPInterface.dll

上行接收函数: GetCNGPDeliverSM

问题说明: 此开发包为Delphi6开发,其中用了PChar这种Vb和.Net不直接支持的数据类型,需要一定的转换.

下面为VB.Net 2005开发代码示例:

定义部份:

Imports System.Runtime.InteropServices

Private Declare Function GetCNGPDeliverSM Lib "SP_Interface.dll" ( _
   <MarshalAs(UnmanagedType.VBByRefStr)> ByRef sMsgID As String, _
   ByRef byMsgFormat As Byte, _
   <MarshalAs(UnmanagedType.VBByRefStr)> ByRef sRecvTime As String, _
   <MarshalAs(UnmanagedType.VBByRefStr)> ByRef sOrgAddr As String, _
   <MarshalAs(UnmanagedType.VBByRefStr)> ByRef sDestAddr As String, _
   ByRef byMsgLen As Byte, _
   <MarshalAs(UnmanagedType.VBByRefStr)> ByRef sMsgContent As String, _
   ByRef byProValue As Byte, _
   ByRef byIsReport As Byte, _
   <MarshalAs(UnmanagedType.VBByRefStr)> ByRef sSrcMsgID As String, _
   <MarshalAs(UnmanagedType.VBByRefStr)> ByRef sSubmitdate As String, _
   <MarshalAs(UnmanagedType.VBByRefStr)> ByRef sDonedate As String, _
   <MarshalAs(UnmanagedType.VBByRefStr)> ByRef sStatus As String, _
   ByRef iDeliverAckResult As Int32, _
   <MarshalAs(UnmanagedType.VBByRefStr)> ByRef Link_ID As String, _
   <MarshalAs(UnmanagedType.VBByRefStr)> ByRef BeneficiaryID As String) As Int32

初台化各指针

Public Sub XX()

Dim Result As Int32
Dim sMsgID As New String(Chr(0), 10)
Dim byMsgFormat As Byte = 0
Dim sRecvTime As New String(Chr(0), 14)
Dim sOrgAddr As New String(Chr(0), 20)
Dim sDestAddr As New String(Chr(0), 20)
Dim byMsgLen As Byte = 0
Dim sMsgContent As New String(Chr(0), 254)
Dim byProValue As Byte = 0
Dim byIsReport As Byte = 0
Dim sSrcMsgID As New String(Chr(0), 20)
Dim sSubmitdate As New String(Chr(0), 10)
Dim sDonedate As New String(Chr(0), 10)
Dim sStatus As New String(Chr(0), 7)
Dim iDeliverAckResult As Byte = 0
Dim Link_ID As New String(Chr(0), 20)
Dim BeneficiaryID As New String(Chr(0), 21)

End Sub

详细说明:

在下行的时候虽说传入参数也有PChar类型,不过用String类型可以表示并系统可以识别,不需要我们自己去转换,其它类型的可按开发文档上的Byte和Int32类型表示

上行因为PChar是指针引用,象ByRef或Output这种按引用传值方式,所以需要托管代码到非托管代码的一个指针转换,那么可以使用UnmanagedType.VBByRefStr 进行"特性"说明这一转换,C#也一样,好象.Net1.1不支持这一项,我也没研究过

除了这一项之外还有就是必须分配内存空间,这里我用New String(Chr(0),100)表示,后面的100是字符长度,这个是定长.

 

抱歉!评论已关闭.