passing strings to external functions

General FreeBASIC programming questions.
DamageX
Posts: 130
Joined: Nov 21, 2009 8:42

passing strings to external functions

Post by DamageX »

what is the difference between these three code snippets? I have found that only one of them works, but I don't know why.

Code: Select all

declare function SomeThing (byval as any ptr) as integer
dim stuff as string
stuff="cheerios"
? SomeThing(@stuff)

Code: Select all

declare function SomeThing (byref as string) as integer
dim stuff as string
stuff="cheerios"
? SomeThing(stuff)

Code: Select all

declare function SomeThing (byref as zstring) as integer
dim stuff as string
stuff="cheerios"
? SomeThing(stuff)
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: passing strings to external functions

Post by MrSwiss »

And, where is the external code, being called?
Just a guess, the "Any Ptr", probably works ...
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: passing strings to external functions

Post by Munair »

It would be helpful if you provided a function body. The function declaration with ANY PTR may be the one that works. Try each declare independently and see what works. You are also vague about "one works". Do you get an error message from the compiler, or just no output result?

EDIT: crossed reply with MrSwiss
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: passing strings to external functions

Post by MrSwiss »

In case you want to make it more, String-like use:
ByVal As ZString Ptr -- or --
ByVal As Const ZString Ptr (aka: Ptr is: "read only")

@Munair,
external code = function body (implementation)
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: passing strings to external functions

Post by fxm »

MrSwiss wrote:ByVal As Const ZString Ptr (aka: Ptr is: "read only")
We can modify Ptr but not *Ptr.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: passing strings to external functions

Post by MrSwiss »

That's exactly, what I ment to say, the ZString is: "read only" ...
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: passing strings to external functions

Post by fxm »

Yes:

Code: Select all

as const Zstring ptr       '' zstring is constant
as zstring const ptr       '' ptr is constant
as const zstring const ptr '' zstring and ptr are constant
DamageX
Posts: 130
Joined: Nov 21, 2009 8:42

Re: passing strings to external functions

Post by DamageX »

Well, I posted generic example code to get right to the point.

But after thinking about it I realized what I should have done. I compiled all three and compared assembly listings. So now I can answer my own question.

The first and second pieces of code generate exactly the same output. Third one is different.

Code: Select all

lea eax, [ebp-12]
push eax

Code: Select all

push dword ptr [ebp-12]
Passing pointer to descriptor or passing pointer to string.

I can pass a pointer to a string if I want to by using strptr(stuff) instead of @stuff

Learn something every day (hopefully not forgetting it the next day).
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: passing strings to external functions

Post by MrSwiss »

DamageX wrote:Well, I posted generic example code to get right to the point.
As can be seen, from the comments, you didn't really do that.
DamageX wrote:I compiled all three and compared assembly listings.
That is IMO, not the way to go, since the generated ASM might differ,
from a straight ASM implementation.
(generated ASM vs. hand coded ASM)
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: passing strings to external functions

Post by Munair »

Indeed, no need to consult assembler code. It's simple: if you have the function source code you can adjust it to your needs. Otherwise you should know what parameter(s) to pass and how. So far you have been vague about your "external function".
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: passing strings to external functions

Post by jj2007 »

What about a straightforward solution?

Code: Select all

Function SomeThing (mystring As string) As integer
  print mystring;
  return 1
End Function

dim stuff as string
stuff="cheerios"
? SomeThing(stuff)
Sleep
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: passing strings to external functions

Post by MrSwiss »

jj2007 wrote:What about a straightforward solution?
Problem is: it's internal and, not as stated by OP, external (library).
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: passing strings to external functions

Post by caseih »

At the very least he could have provided the C function prototype he's trying to pass a string to, rather than just making us guess. Until such time as DamageX provides additional information, there's not much we can do or say here other than make wild guesses and make general statements.

With that in mind, here goes a general statement. If the function he was trying to call is a C-based function, const ZString Ptr should be used to declare the function's parameter, and a FB string can be passed directly to it and FB will pass a ptr to the string's contents. I'm not sure why DamageX would think that declaring it as STRING would work... other languages usually don't know how to deal with an FB string descriptor.
DamageX
Posts: 130
Joined: Nov 21, 2009 8:42

Re: passing strings to external functions

Post by DamageX »

At the very least he could have provided the C function prototype he's trying to pass a string to
I was trying to call wsock32.dll gethostbyname. From Windows Sockets Network Programming:

Code: Select all

struct hostent FAR * PASCAL FAR gethostbyname  /* Null on failure */
 (char FAR *name);  /* pointer to name of the host */
If that means something to you, great. I am not a C programmer.
and FB will pass a ptr to the string's contents. I'm not sure why DamageX would think that declaring it as STRING would work... other languages usually don't know how to deal with an FB string descriptor
I was not aware of how to tell whether FB passes a pointer to a string or a pointer to a descriptor, at first I didn't even know it passed a pointer to a descriptor. That's what my question was about. It seems to depend on several details. For instance, in addition to what has already been mentioned, it also seems that @"www.freebasic.net" is a pointer to the string, and @stuff can be a pointer to the string if it is DIMed as STRING*20 instead of just STRING.

I wanted to pass a pointer because I wanted the function to read from a location other than the beginning. Specifically, if I have stuff="http://www.freebasic.net" I wanted to pass a pointer to "www.freebasic.net" and skip the "http://"

Needless to say, when I tried gethostbyname(@stuff+7) it ALWAYS failed.

Hopefully, this discussion thread is now a little more clear :)
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: passing strings to external functions

Post by jj2007 »

DamageX wrote:Needless to say, when I tried gethostbyname(@stuff+7) it ALWAYS failed.
It failed when compiling, or when running the exe?

Code: Select all

#include "win\winsock2.bi"

Declare Function MyGetHostName Alias "gethostbyname" ( Byval host_name As zString ptr) As HOSTENT Ptr

Dim stuff as string
stuff="http://www.freebasic.net"

print gethostbyname(stuff)	' built-in
print MyGetHostName(stuff)	' declared
Sleep
btw here is the original declaration:

Code: Select all

declare function gethostbyname(byval name as const zstring ptr) as HOSTENT ptr
Post Reply