function POS_Open(pszPortName: String; nComBaudrate: integer; nComDataBits: integer; nComStopBits: integer; nComParity: Integer; nComFlowControl: Integer): Integer; stdcall; external 'POSDLL.dll';
以上是他原本的定义
我的定义:Public Declare Function POS_Open Lib "POSDLL.dll" (pszPortName As String, nComBaudrate As Integer, nComDataBits As Integer, nComStopBits As Integer, nComParity As Integer, nComFlowControl As Integer) As IntegerHANDLE POS_Open(      LPCTSTR lpName,       int nComBaudrate,       int nComDataBits,       int nComStopBits,       int nComParity,       int nParam      ) 描述 打开端口。 参数 lpName[in]  指向以 null 结尾的打印机名称或端口名称。 当参数nParam的值为POS_COM_DTR_DSR、POS_COM_RTS_CTS、POS_COM_XON_XOFF或POS_COM_NO_HANDSHAKE 时, “COM1”,“COM2”,“COM3”,“COM4”等表示串口; 当参数nParam的值为POS_OPEN_PARALLEL_PORT时,“LPT1”,“LPT2”等表示并口; 当参数nParam的值为POS_OPEN_BYUSB_PORT时,“BYUSB-0”、“BYUSB-1”、“BYUSB-2”、“BYUSB-3”等表示USB端口。当参数nParam的值为POS_OPEN_PRINTNAME时,表示打开指定的打印机。    nComBaudrate    [in]  指定串口的波特率(bps)。 可以为以下值之一:1200,2400,4800,9600,14400,19200,38400,56000,57600等。        具体的值与打印机的端口参数有关,不过最大不应超过256000。    nComDataBits    [in]  指定串口通讯时的数据位数。    可以为 5 到 8。    具体的值与打印机的端口参数有关。 nComStopBits    [in]  指定串口通讯时的数据停止位数。可以为以下值之一: Flag
 Value
 Meaning
 
POS_COM_ONESTOPBIT
 0x00
 停止位为1
 
POS_COM_ONE5STOPBITS
 0x01
 停止位为1.5 
 
POS_COM_TWOSTOPBITS
 0x02
 停止位为2
  nComParity    [in]  指定串口的奇偶校验方法。 可以为以下值之一: Flag
 Value
 Meaning
 
POS_COM_NOPARITY
 0x00
 无校验
 
POS_COM_ODDPARITY
 0x01
 奇校验
 
POS_COM_EVENPARITY
 0x02
 偶校验
 
POS_COM_MARKPARITY
 0x03
 标记校验
 
POS_COM_SPACEPARITY
 0x04
 空格校验
  nParam    [in]  指定串口的流控制(握手)方式、或表示通讯方式。请参考参数lpName的说明。可以为以下值之一: Flag
 Value
 Meaning
 
POS_COM_DTR_DSR
 0x00
 流控制为DTR/DST 
 
POS_COM_RTS_CTS
 0x01
 流控制为RTS/CTS
 
POS_COM_XON_XOFF
 0x02
 流控制为XON/OFF
 
POS_COM_NO_HANDSHAKE
 0x03
 无握手
 
POS_OPEN_PARALLEL_PORT
 0x12
 打开并口通讯端口
 
POS_OPEN_BYUSB_PORT
 0x13
 打开USB通讯端口
 
POS_OPEN_PRINTNAME
 0X14
 打开打印机驱动程序
               其中前两项也统称为硬件流控制,一般选用 RTS/CTS 方式。 返回值 如果函数调用成功,返回一个已打开的端口句柄。 如果函数调用失败,返回值为 INVALID_HANDLE_VALUE (-1)。 以上为解释
我在程序里用:Dim t As Integert = POS_Open("BYUSB-1", 0, 8, &H0, &H0, &H13)MsgBox t
他一直返回-1
有谁知道挖是什么问题,是我定义的时候错了?还是写的内容不对,或者以上原因全不是?

解决方案 »

  1.   

    关键要搞清楚对方语言中:
    String 类型是否为 BStr,是否 Unicode 编码;
    Integer 类型什么长度,VB 中 2 字节是 Integer、4 字节用 Long;
    还有所有的参数是否都是 [In] 类型的,那么 VB 中全部要加 ByVal 限定。
      

  2.   

    咋一看,应该原本是Delphi的代码,如果这个DLL当中的第一个参数,确实使用的是Delphi的String类型的话,那么这整个DLL,VB是无法使用的。
      

  3.   

    如果这个DLL原来的声明应该是后面的C++的声明,那么这个Delphi的代码就是错的。
    根据
    HANDLE POS_Open(       LPCTSTR lpName,       int nComBaudrate,       int nComDataBits,       int nComStopBits,       int nComParity,       int nParam       ) 
    转为VB
    Public Declare Function POS_Open Lib "POSDLL.dll" (byval pszPortName As String,byval nComBaudrate As Long, nComDataBits As Long, nComStopBits As Long, nComParity As Long, nComFlowControl As Long) As Long
      

  4.   

    Public Declare Function POS_Open Lib "POSDLL.dll" (byval pszPortName As String,byval nComBaudrate As Long, byval nComDataBits As Long,byval nComStopBits As Long,byval nComParity As Long,byval nComFlowControl As Long) As Long 
      

  5.   

    function POS_Open(pszPortName: String; nComBaudrate: integer; nComDataBits: integer; nComStopBits: integer; nComParity: Integer; nComFlowControl: Integer): Integer; stdcall; external 'POSDLL.dll'; 
    这个是Delphi语言
    我现在找不到里面的string类型是否为 BStr,是否 Unicode 编码; 
    我现在写成
    Public Declare Function POS_Open Lib "POSDLL.dll" (ByVal pszPortName As String, ByVal nComBaudrate As Long, ByVal nComDataBits As Long, ByVal nComStopBits As Long, ByVal nComParity As Long, ByVal nComFlowControl As Long) As Long
    应VB调用后还是-1
    VB语句:Dim t As Long
        t = POS_Open("BYUSB-1", 1200, 8, &H0, &H0, &H13)
    是不是还是string的问题?
      

  6.   

    - -不是吧,第一个定义用Delphi定义的,我是用记事本打开PosdllFuncs.pas这个文件找到的定义。
    而 HANDLE POS_Open(       LPCTSTR lpName,       int nComBaudrate,       int nComDataBits,       int nComStopBits,       int nComParity,       int nParam       ) 以上是在他帮助文件里写的定义。
      

  7.   

     t = POS_Open(byval "BYUSB-1",byval 1200&byval 8&,byval &H0&,byval &H0&,byval &H13&
      

  8.   

    我在1楼已经讲了要加 ByVal。这应该在参数中声明比较保险。
    还有参数类型已经声明为 Long 型了,调用时不加类型符 & 是可以的。
    Public Declare Function POS_Open Lib "POSDLL.dll" _
           (ByVal pszPortName As String, _
            ByVal nComBaudrate As Long, _
            ByVal nComDataBits As Long, _
            ByVal nComStopBits As Long, _
            ByVal nComParity As Long, _
            ByVal nComFlowControl As Long _
           ) As Long Dim t As Long
    t = POS_Open("BYUSB-1", 1200, 8, &H0, &H0, &H13)
      

  9.   

    疏忽,应该没区别。
    那么可能是字符串编码的问题了。
    Public Declare Function POS_Open Lib "POSDLL.dll" _
           (ByVal pszPortName As Long, _
            ByVal nComBaudrate As Long, _
            ByVal nComDataBits As Long, _
            ByVal nComStopBits As Long, _
            ByVal nComParity As Long, _
            ByVal nComFlowControl As Long _
           ) As Long Dim t As Long, aChars() as byte'因为不知道编码方式,下面两种方式分别试一下
    aChars = "BYUSB-1" & Chr(0) 'Unicode
    aChars = StrConv("BYUSB-1" & Chr(0), vbFromUnicode) 'Ansit = POS_Open(VarPtr(aChars(0)), 1200, 8, &H0, &H0, &H13)
      

  10.   

    以上2种方式经过测试全是-1,
    难道真的如unsigned 所说
    “如果这个DLL当中的第一个参数,确实使用的是Delphi的String类型的话,那么这整个DLL,VB是无法使用的。”
      

  11.   

    用Delphi做导出的API,如果非要使用string,那么注意几点:
    1. 在dll及调用dll的应用程序的project主单元中,都要添加uses ShareMem;
    2. ShareMem必须是uses里面的第一个类。
      

  12.   

    我查了下说明书什么的,关于波特率,只有串口的时候才有效啊。。现在我用的是USB,因该是和波特率无关的。
      

  13.   

    - -关于DLL,我是不能改的吧!~,应用程序VB中有uses ShareMem么?
      

  14.   

    这是Dlthi与外部调用者约定接口的方式,不符合API格式就不能被调用。