Click to See Complete Forum and Search --> : Passing a string arg to a DLL function


Leslie Chong
August 9th, 2001, 05:38 AM
Hello,

I've been trying to pass a string reference to a DLL function (created by myself). I always get the only 4 bytes in the return string 'DataOut':


private Sub CommandRead_Click()

Dim DataOut as string
Dim BlkNo as Integer
Dim BlkLen as Integer

DataOut = 0

'Read Data
BlkNo = 0
BlkLen = 32
Status = ReadBlock(BlkNo, BlkLen, DataOut)
Text1.Text = DataOut

End Sub

public Declare Function ReadBlock Lib "TestMemCard.dll" (byval BlkNo as Integer, byval BlkLen as Integer, byval DataOut as string) as Long




During creation of "TestMemCard.dll" , the function 'ReadBlock' was defined as

extern "C" long CALLDLL WINAPI ReadBlock(BYTE BlkNo,BYTE BlkLen,char *DataOut)

...where 'BYTE' is unsigned char.

I tried both 'ByVal' and 'ByRef' for 'DataOut', and get always 4 bytes for 'ByVal' while 'RyRef' gives illegal operation.

I appreciate if anyone could help. Thanks.

Clearcode
August 9th, 2001, 05:51 AM
You are passing an empty string - you should preinitialise it if you are going to write to it i.e.:


Dim DataOut as string

DataOut = string$(1024,0)





Hope this helps,
Duncan

-------------------------------------------------
Ex. Datis: Duncan Jones
Merrion Computing Ltd
http://www.merrioncomputing.com
Check out the new downloads - ImageMap.ocx is the VB control that emulates an HTML image map, EventVB.OCX for adding new events to your VB form and adding System Tray support simply, MCL Hotkey for implemenmting system-wide hotkeys in your application...all with source code included.