Results 1 to 11 of 11

Thread: string from dll truncated

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    20

    string from dll truncated

    I'm using a c++ DLL, which takes as a parameter a structure from VB, like this:
    Public Type ODATA
    lReadError As Long
    szData As String * 128
    End Type

    I have declared the function as ByRef pOData As ODATA).

    The problem is that when c-function returns the filled structure, the string part is not as it should. The first bytes have been dissappeared. If that function is used from another c code it returns correctly everything but not if called from VB. For example calling from c it returns a string "abcdefg", but called from VB it gets only the value "efg". I think it has something to do with VB way of handling strings with two bytes for pointer and length in the beginning.

    How should I use/declare variables to receive the whole string?
    The C DLL is not made specially for VB. I use VB 5.

    Thanks in advance.
    /petri

  2. #2
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: string from dll truncated

    check out this thread, parts of the replies may have the answer to your problem.

    http://www.vbforums.com/showthread.php?t=354844

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    20

    Re: string from dll truncated

    Thanks benmartin101
    I see what you mean, but unfortunately that did not solve the problem.
    I tried to give a pointer in long instead of a string:

    VB Code:
    1. Public Type OIDATA
    2.   lReadError As Long
    3.   szData As [COLOR=Red]long [/COLOR]
    4. End Type

    and then use it like this:
    VB Code:
    1. Temp = Space(255)
    2.   pOIData.szData = StrPtr(Temp)
    3.   retdata = OCIRead(lObject, lDevice, lIndex, pOIData)

    But then I get an exception error and program terminates.
    I think it has something to do with declare. The function is declared as
    VB Code:
    1. Declare Function OCIRead Lib "oci200.dll" (ByVal lOIObject As Long, lDevice As Long, ByVal lIndex As Long, [COLOR=Red]ByRef pOIData As OIDATA[/COLOR]) As Long

    and now I should pass a value inside a structure which must be declared Byref.

    Any ideas?
    /petri

  4. #4
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: string from dll truncated

    try this
    VB Code:
    1. Public Type ODATA
    2.   lReadError As Long
    3.   szData(0 to 127) As Byte
    4. End Type
    Now the byte array should contain the string you are looking for.

  5. #5
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: string from dll truncated

    did you make this dll? Because I think its giving an error because perhaps it expects a string member in the OIDATA type.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    20

    Re: string from dll truncated

    moeur, I tried that, but got Type mismatch. Maybe I'm not handling right the Byte pointer now, as I need to convert that back to string again. Need to look at it closer tomorrow.

    benmartin101, no that dll is not made by me (I wouldn't have this problem if I've done that ). It is part of the house/heat controlling system that has PC interface through a serial port.

    Thanks for your help!

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    20

    Re: string from dll truncated

    OK. I had a stupid mistake but could now process it.
    Unfortunately the result was the same as before: the first four characters are missing, even if declared as byte (1 to 127).

    When I use this same DLL with executable made with C the result was
    "-1323827200,11,1", but call from VB gives still "3827200,11,1".

    The big number with minus sign is in big-endian form and I must convert it to small endian. That is why I need all the characters (to get the energy consumption).

    /petri

  8. #8
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: string from dll truncated

    show me how it is declared in C and how you are processing the string after you get it.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    20

    Re: string from dll truncated

    This is relevant (I think) code from C

    Code:
    #define _EXPORT(return_value) extern "C" return_value __stdcall __export
    #pragma pack(1)
    
    struct OIDATA
    {
      long lData[32];    
      long lCheckAlarm;  
      long lCategory;    
      long lReadError;   
      char szData[128];  // raw index read data as comma separated
    };
    
    _EXPORT(long) OCIRead(long lOIObject,long lDevice,long lIndex,struct OIDATA *pOIData);
    and an example how I use data in VB

    VB Code:
    1. CopyMemory Temp, StrPtr(pOIData.szData), 15
    2. Temp = Left(Temp, InStr(Temp, ",") - 1)
    3. MsgBox Temp    'this is where the four leading characters are missing!
    4. MYREC.Energy = ntohl(Val(Temp))

    As said earlier, the result was the same even if szData was a string, not a Byte array. Then of course I didn't use CopyMemory() and StrPtr() functions.

    Does this help you, moeur?
    /petri

  10. #10
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: string from dll truncated

    That C code should be declared like this
    VB Code:
    1. Type OIDATA
    2.   lData(0 To 31) As Long
    3.   lCheckAlarm As Long
    4.   lCategory As Long
    5.   lReadError As Long
    6.   szData(0 To 137) As Byte  ' raw index read data as comma separated
    7. End Type
    8.  
    9.  
    10. Declare Function OCIRead Lib "oci200.dll" (ByVal lOIObject As Long, ByVal lDevice As Long, ByVal lIndex As Long, pOIData As OIDATA) As Long

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    20

    Re: string from dll truncated

    GREAT! It works now.
    Thanks a lot, moeur

    /petri

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