Results 1 to 6 of 6

Thread: API with return pointer

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 1999
    Location
    Stafford, England
    Posts
    6

    Post

    Hi,

    I have this really big problem, how can I call this API function that return a pointer? Any help will be grateful...

    the dll function is like this:
    ---------------------------------------------
    ReadCode(HWND hwnd,
    SHORT MaxLength,
    LPSTR Codeline,
    SHORT *Length);

    hwnd
    Handle of the application windows which will receive the notification messages.

    MaxLength
    Specify the size of the user buffer pointed by Codeline parameter.

    Codeline
    Pointer to the user buffer where codeline will be transferred by the service.
    This value is return by function.

    Length
    Pointer to a variable that will contain the effective size of the codeline transferred by the service.
    This value is return by function.
    ---------------------------------------------

    SO, in my VB coding, I declare like this:

    Public Declare Function ReadCode Lib "ReadCode.dll" _
    (ByVal hwnd As Long, _
    ByVal MaxLength As Integer, _
    ByRef Codeline As String, _
    ByRef Length As Integer) As Integer

    THEN, I call like this:

    dim FullCode as string
    dim ActualLen as integer

    rc = ReadCode(frmread.hwnd, 60, FullCode, ActualLen)

    ---------------------------------------------

    RESULT:
    I get a error message popup saying...

    The instruction at "0x65343221" referenced memory at "0x30303038". The memory could not be "read".
    --------------------------------------
    Anyone who can help will be most grateful, thanks.

    ming


  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    Look at this lne:


    Codeline -
    Pointer to the user buffer where codeline will be transferred by the service.
    This value is return by function.


    This means that your variable would recieve the result. But first you must prepare this variable. In your second parameter you specified that you the size of the String buffer is 60, but you never resized your variable to except this number:

    Code:
    Public Declare Function ReadCode Lib "ReadCode.dll" _
    (ByVal hwnd As Long, _
    ByVal MaxLength As Integer, _
    ByRef Codeline As String, _
    ByRef Length As Integer) As Integer
    
    
    '---------Here is your code
    
    Dim strFullCode As String
    Dim intActualLen as Integer
    Dim lRet As Long
    
    strFullCode = 60
    lRet = ReadCode(frmread.hwnd, 60, strFullCode, intActualLen)

    Unfortunately, I can't test it, because I don't have this ReadCode.dll

    ------------------

    Serge

    Software Developer
    [email protected]
    [email protected]
    ICQ#: 51055819


  3. #3
    New Member
    Join Date
    Nov 1999
    Location
    l
    Posts
    13

    Post

    Watch out with LPSTR, this is not a pointer
    to string variable. This is a long pointer to the string. Visual basic stores strings in unicode format. So if you give a variable of type string to the function then the pointer would refer to the first element of the variable string (this is the first of four bytes wich gives info on the length of the string).
    Instead of that you'll have to use the undocumented vb function StrPtr(string) to pass the pointer of the vb string.

    Almost all api declares want the pointer to the string except for the Ole subsystem wich can handle unicode string instead of Ansi.

  4. #4
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892

    Post

    No, not really, Serge's way works except:

    strFullCode = 60 ' Sets the strFullCode to "60"

    Use this instead:
    strFullCode = String(60, vbNullChar) ' Sets the size of strFullCode to 60

    ------------------
    Yonatan
    Teenage Programmer
    E-Mail: [email protected]
    ICQ: 19552879
    AIM: RYoni69

  5. #5
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    Oops, yep, my mistake.....I meant that it sould have been:

    strCodeline = Space(60)


    Sorry, for that. That what happens when you type directly in the post.



    ------------------

    Serge

    Software Developer
    [email protected]
    [email protected]
    ICQ#: 51055819


  6. #6
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Post

    Because of the way vb stores strings, it is necessary to pass them ByVal. So the declaration would be:
    Public Declare Function ReadCode Lib "ReadCode.dll" _
    (ByVal hwnd As Long, _
    ByVal MaxLength As Integer, _
    ByVal Codeline As String, _
    ByRef Length As Integer) As Integer

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