PDA

Click to See Complete Forum and Search --> : API with return pointer


yiming
Nov 18th, 1999, 07:48 AM
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

Serge
Nov 18th, 1999, 05:56 PM
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:


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
Serge_Dymkov@vertexinc.com
Access8484@aol.com
ICQ#: 51055819 (http://www.icq.com/51055819)

erw
Nov 18th, 1999, 06:17 PM
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.

Yonatan
Nov 18th, 1999, 06:20 PM
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: RZvika@netvision.net.il
ICQ: 19552879 (http://www.icq.com/19552879)
AIM: RYoni69

Serge
Nov 18th, 1999, 07:43 PM
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
Serge_Dymkov@vertexinc.com
Access8484@aol.com
ICQ#: 51055819 (http://www.icq.com/51055819)

Frans C
Nov 20th, 1999, 01:09 AM
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