Results 1 to 5 of 5

Thread: Convert C++ dll function declaration to VB *RESOLVED*

  1. #1

    Thread Starter
    Hyperactive Member Simon Caiger's Avatar
    Join Date
    Aug 2000
    Location
    Rugby, England
    Posts
    377

    Convert C++ dll function declaration to VB *RESOLVED*

    Hi,
    I have a C++ dll which has the following function prototype declared in it.

    VB Code:
    1. Short Int Send_RS232_Command(unsigned char, unsigned char *const, unsigned char, unsigned char *const, long)

    I am trying to use this dll from VB.
    Code I have so far is.

    VB Code:
    1. Option Explicit
    2. Option Base 0
    3.  
    4. Private Declare Function Send_RS232_Command Lib "RS232V2.dll" Alias "?Send_RS232_Command@@YAFEQAEE0J@Z" _
    5.                         (ByVal bBoardAddress As Byte, ByRef bTxDataArray As Byte, _
    6.                         ByVal bTxDataLength As Byte, ByRef bRxDataArray As Byte, ByVal lRxDataLength As Long) As Integer
    7.  
    8. Private Sub cmdSend_Click()
    9.  
    10.     Dim iRet As Integer
    11.     Dim bBoardAddress As Byte
    12.     Dim bTxDataArray(10) As Byte
    13.     Dim bTxDataLength As Byte
    14.     Dim bRxDataArray(10) As Byte
    15.     Dim lRxDataLength As Long
    16.    
    17.     bBoardAddress = 1
    18.     bTxDataArray(0) = 0
    19.     bTxDataArray(1) = 1
    20.     bTxDataArray(2) = 2
    21.     bTxDataArray(3) = 3
    22.     bTxDataArray(4) = 4
    23.     bTxDataLength = 5
    24.    
    25.     iRet = Send_RS232_Command(bBoardAddress, bTxDataArray(0), bTxDataLength, bRxDataArray(0), lRxDataLength)
    26.  
    27. End Sub

    I get the error "Bad DLL Calling Convention".
    Is this because I have incorrectly declared the function?
    Last edited by Simon Caiger; May 4th, 2004 at 07:59 AM.
    Simon Caiger

    Documentation is like sex; when it's good, it's very, very good, and when it's bad, it's better than nothing.

  2. #2

    Thread Starter
    Hyperactive Member Simon Caiger's Avatar
    Join Date
    Aug 2000
    Location
    Rugby, England
    Posts
    377
    So I did the usual self doubting, am I going mad bit for a couple of hours and then I discovered that I have been wrestling with another Microsoft bug.

    http://support.microsoft.com/default...;en-us;Q153586

    You can not call a C function in a dll from the VB IDE if the function uses the _cdecl calling convention.

    If you compile and run the VB .exe, it works fine.

    Not to good for debugging then
    Simon Caiger

    Documentation is like sex; when it's good, it's very, very good, and when it's bad, it's better than nothing.

  3. #3
    Fanatic Member
    Join Date
    Dec 2003
    Posts
    703
    Do you have the DLL source? If so change the calling convention to __stdcall and it will work through the IDE.
    an ending

  4. #4

    Thread Starter
    Hyperactive Member Simon Caiger's Avatar
    Join Date
    Aug 2000
    Location
    Rugby, England
    Posts
    377
    Thanks for the reply.

    Thats what the Knowledge Base article I linked to suggests.

    Unfortunately, I do not have the source code
    Simon Caiger

    Documentation is like sex; when it's good, it's very, very good, and when it's bad, it's better than nothing.

  5. #5
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    You can write a dll in c++ that calls your RS232 dll, and have your vb program call your dll. This way, you could make your dll have the stdcall.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width