Pointer stuff

BlitzMax Forums/BlitzMax Programming/Pointer stuff

TeraBit(Posted 2005) [#1]
I'm trying to convert a Blitz String into an int so that I can pass it to a DLL.

e.g.

t:String = "Hello"

A:int = 0

A = t.ToCString

I used to do it this way:
Int(StringToCString(t$))

and used to convert back via

CStringToString$(retval)

but this no longer works.

No matter how I slice it, I can't get a simple int with the memory address I want to pass. Just convertion errors. Any Idea's what I'm doing wrong.


Michael Reitzenstein(Posted 2005) [#2]
Shouldn't it be Byte Ptr not Int?


TeraBit(Posted 2005) [#3]
I'm just passing straight ints:

Extern
	Function StdCall0(bbfunction)
	Function StdCall1(bbfunction,a0)
	Function StdCall2(bbfunction,a0,a1)
	Function StdCall3(bbfunction,a0,a1,a2)
	Function StdCall4(bbfunction,a0,a1,a2,a3)
	Function StdCall5(bbfunction,a0,a1,a2,a3,a4)
	Function StdCall6(bbfunction,a0,a1,a2,a3,a4,a5)
	Function StdCall7(bbfunction,a0,a1,a2,a3,a4,a5,a6)
End Extern


These can either be interpreted as values or memory pointers depending on the DLL function.

It is used by functions like so:
Function AddLabel%(t$,x%,y%,w%,h%,P%)
	Return StdCall6(DllFunction(lib%,"AddLabel"),Int Varptr(t$),x,y,w,h,p)
End Function


Int(ConvertToCString()) used to do it, but was removed when blitz strings went to objects. int(MyString.ToCString) gives an error.

It's a bit frustrating to have an entire working system that have the rug move under you and then not being able to figure out how to make it work again.


Azathoth(Posted 2005) [#4]
ToCString is a method, it should be ToCString()


skidracer(Posted 2005) [#5]
this looks as though it's working (oops 3 secs to slow, beaten by the vowel man...):
Strict
Local t:String = "Hello"
Local a:Int = 0
a=Int(t.ToCString()) 
Print a
End



TeraBit(Posted 2005) [#6]
Great. Most of the Way there. So how do you convert a CString Back to a Blitz one when you get an int returned as the pointer?


skidracer(Posted 2005) [#7]
Strict
Local t:String = "Hello"
Local a:Int = 0
a=Int(t.ToCString())
Print a
Local b$=String.FromCString(Byte Ptr(a))
Print b
End


TeraBit(Posted 2005) [#8]
Right it's all working again now. Thanks for all your help!

I'll make another thread for the demo!


skidracer(Posted 2005) [#9]
if you want to tidy up your dll calls, there's some discussion here that might be of interest including declaring stdcall function pointer prototypes:

http://www.blitzbasic.com/Community/posts.php?topic=41803


TeraBit(Posted 2005) [#10]
Thanks I'll take a look. The demo has been posted if you're interested to see what it's used for...