This is a solution to the string pointer dilemma.
I have used overloaded functions (functions of the same name but different prototypes) to handle both Ascii and wide strings.
If the variable type is not a string (gstring) then the generic Read/Write functions are automatically used instead.
The string variables will automatically adjust in size if the count is set to 0
==========================================================
'THIS VERSION SUPPORTS ANSI AND WIDE DYNAMIC STRINGS
'Using the function overloading feature (and gstrings)
'=========================================================
IndexBase 0
Declare Function ReadFile Lib "kernel32.dll" (ByVal hFile As Long, ByRef lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, ByRef lpNumberOfBytesRead As Long, ByRef lpOverlapped As any) As Long
Declare Function WriteFile Lib "kernel32.dll" (ByVal hFile As Long, ByRef lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, ByRef lpNumberOfBytesWritten As Long, ByRef lpOverlapped As any) As Long
Declare Function GetFileSizeEx Lib "kernel32.dll" (ByVal hFile As Long, ByRef lpFileSize as quad) as long
Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Declare Function CreateFile Lib "kernel32.dll" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Declare Function DeleteFile Lib "kernel32.dll" Alias "DeleteFileA" (ByVal lpFileName As String) As Long
Declare Function MoveFile Lib "kernel32.dll" Alias "MoveFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String) As Long
Declare Function CopyFile Lib "kernel32.dll" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long
Declare Function CreateDirectory Lib "kernel32.dll" Alias "CreateDirectoryA" (ByVal lpPathName As String, ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long
% GENERIC_WRITE = &H40000000
% GENERIC_READ = &H80000000
% OPEN_EXISTING = 3
% OPEN_ALWAYS = 4
% FILE_SHARE_READ = 1
% FILE_SHARE_WRITE = 2
Dim byref SYS_SECUT as long
Dim byref NO as long
Dim dwcount as long
'----------------------------------------------------------------------------------------------------
Function ReadFileData(file as string,byref dest as any, byval count as long,byval scale as long) as long
Long vRet
vRet = CreateFile(file, GENERIC_WRITE or GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, SYS_SECUT, OPEN_ALWAYS, 0, 0)
ReadFile vRet,dest,count*scale,dwcount,NO
dwcount/=scale
CloseHandle vRet
Function = vRet
End Function
'--------------------------------------------------------------------------------------------------------
Function WriteFileData(file as string,byref dest as any, byval count as long,byval scale as long) as long
Long vRet
vRet = CreateFile(file, GENERIC_WRITE or GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, SYS_SECUT, OPEN_ALWAYS, 0, 0)
WriteFile vRet,dest,count*scale,dwcount,NO
dwcount/=scale
CloseHandle vRet
Function = vRet
End Function
'===========================================================================
'OVERLOADED FUNCTIONS FOR ASCII (scale=1) and wide (scale=2) DYNAMIC STRINGS
'The length of the string buffer is automatically adjusted
'===========================================================================
'-----------------------------------------------------------------------------------------------------------
Function ReadFileData(file as string,byref dest as gstring, byval count as long,byval scale as long) as long
Long vRet
Quad le
vRet = CreateFile(file, GENERIC_WRITE or GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, SYS_SECUT, OPEN_ALWAYS, 0, 0)
if count=0 then GetFileSizeEx vRet,le : count=le/scale
dest=nuls count*scale
ReadFile vRet,*dest,count*scale,dwcount,NO
dwcount/=scale
CloseHandle vRet
Function = vRet
End Function
'------------------------------------------------------------------------------------------------------------
Function WriteFileData(file as string,byref dest as gstring, byval count as long,byval scale as long) as long
Long vRet,le
le=len dest 'in bytes
if count=0 then count=le
if count>le then count=le
vRet = CreateFile(file, GENERIC_WRITE or GENERIC_READ, FILE_SHARE_READ Or FILE_SHARE_WRITE, SYS_SECUT, OPEN_ALWAYS, 0, 0)
WriteFile vRet,*dest,count,dwcount,NO
dwcount/=scale
CloseHandle vRet
Function = vRet
End Function
'=====
'TESTS
'=====
Long i
String Mess = "It seems to be night, I can't see my fingers"
String Yess = "ooooo!"
Long a[100]
Long b[100]
Quad q = 123456789123
Quad w
For i=0 To 9
a(i) =65536+i
Next
WriteFileData "Quad.txt",q,1,8
ReadFileData "Quad.txt",w,1,8
print w
Double d=12345.12345678
Double e
WriteFileData "Double.txt",d,1,sizeof double
ReadFileData "Double.txt",e,1,sizeof double
print e
WriteFileData "Hoppla.txt",mess,0,1
print yess
ReadFileData "Hoppla.txt",yess,0,1
print yess
WriteFileData "Test.zaa",a,100,4
ReadFileData "Test.zaa",b,100,4
For i=0 to 5
Print Str(b(i))
Next
CreateDirectory "MyFirstTest",0
CopyFile "Test.zaa","MyFirstTest/Test.zaa",0
DeleteFile "Test.zaa"
Charles