PDA

View Full Version : Freebasic Strings again



Michael Clease
09-09-2009, 08:57
This is a follow on from this thread http://community.thinbasic.com/index.php?topic=2899.msg22044;topicseen#new

This is a question for Charles really.

you said something about copying the string to/from bstr but couldnt that take a lot of time, the string lengths I am planning on working with could be very large or very small.

Does FB have the ability to overlay arrays on top of my imported data.

So if I had


DIM input AS STRING

TYPE mydata FIELD = 1
buff( 0 to Len(input)) as ubyte
END TYPE

DIM mystring AS mydata AT STRPTR(input)


I know i used cross between TB and FB but you get the idea.

Any help would be useful.

Charles Pegge
09-09-2009, 11:53
Hi Mike,

FB does not appear to have an overlay mechanism like PB absolute arrays. But I did some tests and found FB does not mind passing anonymous pointers. So you can pass the pointer of one kind of data and receive it as the pointer to another kind of data. - which is almost as good as a simple overlay.

Of course passing data between TB and FB gives you greater opportunities to fudge the header declarations and pass the parameters byref instead of pointers byval.





'FreeBasic


'FB SUPPORTS ANONYMOUS POINTERS

dim as long a(100)
dim as any ptr p
p=varptr(a(0))

a(0)=65

sub fz(byval z as zstring ptr)
print *z
end sub


'These work:

asm
push [p]
call fz
end asm


fz p

Michael Clease
09-09-2009, 16:37
Thanks for your help Charles very useful information.




Of course passing data between TB and FB gives you greater opportunities to fudge the header declarations and pass the parameters byref instead of pointers byval.


I dont quite understand what you mean (it could be me, I was awake for about 20 hours yesterday and my brain is off planet at the moment) so if you could talk very slowly I might understand.

Charles Pegge
09-09-2009, 18:52
Mike,

2 Functions for sharing / passing / returning text string data between TB and an FB module. These are only suitable for strings that do not contain null bytes, because the passed data is assumed by FB to be a zstring ( though it is actually an ole BSTR).

thinBasic


'
'----------------------------------------------
'Module with string functions suitable for text
'==============================================

' sensitive to null byte as termintor character


uses "testm"

dim s as string
s="hello module!"
shareStringBuffer strptr(s)
msgbox 0,s



msgbox 0, returnString "Hello!"

FreeBasic


'
'
' fbc -dylib thinBasic_testm.bas
'


#include Once "thinCore.bi"


sub shareStringBuffer()
dim as long bz
dim as zstring ptr pz
thinBasic_ParseLong(bz) : pz=cast(zstring ptr,bz)
mid$(*pZ,1)="done: "
end sub

function returnString() as bstr
dim as double d
dim as bstr bz
dim as zstring ptr pz
dim s as string
d=thinBasic_ParseString(bz) : pz=cast(zstring ptr,bz)
s="Return: "+*pz
function=SysAllocStringByteLen ( strptr(s), len(s))
end function

FUNCTION LoadLocalSymbols Cdecl ALIAS "LoadLocalSymbols" (BYVAL sPath AS STRING) AS Long EXPORT
thinBasic_LoadSymbol ("shareStringBuffer", thinBasic_ReturnNone, @shareStringBuffer, thinBasic_ForceOverWrite)
thinBasic_LoadSymbol ("returnString", thinBasic_ReturnString, @returnString, thinBasic_ForceOverWrite)
FUNCTION = 0
END FUNCTION



Function UnLoadLocalSymbols Cdecl ALIAS "UnLoadLocalSymbols" (BYVAL sPath AS STRING) AS Long EXPORT
FUNCTION = 0
END FUNCTION



Kit:

Michael Clease
09-09-2009, 21:20
Thanks again Charles but I will be passing data which will probably include null bytes so I think FB is too much hard work, Ive spent longer trying to get the data in FB and not done any work on actually processing the data.

I will use another option.

Thanks for your help.

Charles Pegge
09-09-2009, 21:48
If you change your mind Mike, I can show you how to transfer string data efficiently. Byte transfers in Asm only take a few nanoseconds - say 100Mb per Second depending on RAM speed. Have you had any other obstacles with FB ?

Charles

jack
09-09-2009, 23:01
Mike, why couldn't you use an array of uBytes?
sounds to me you are using strings for non-string data.

D.J.Peters
20-05-2013, 03:27
I lost all of my own FreeBASIC thinCore stuff.
However after some research i found out the BSTR / OLESTRING is in real only a pointer on UCHAR not WCHAR.
#ifndef OLE2ANSI
type OLECHAR as WCHAR
#define OLESTR(s) wstr(s)
#else
type OLECHAR as byte
#define OLESTR(s) s
#endif

type BSTR as OLECHAR ptrThat means I can receive and transmit it as simple zstring ptr.
DECLARE FUNCTION thinBasic_ParseStringPtr ALIAS "thinBasic_ParseString" (byval pp as zstring ptr ptr) AS longNote: SysAllocStringByteLen()
If you allocate and returns an BSTR from FreeBASIC DLL the memory will never be freed (exept the OS unload the DLL)

Sorry if i'm wrong but all my current tests with zstring ptr as BSTR replacement works without any MPF. (Memory Protection Fault)

Joshy