string in c++?

New to FreeBASIC? Post your questions here.
Post Reply
maurosebastian91
Posts: 30
Joined: Mar 21, 2021 18:22

string in c++?

Post by maurosebastian91 »

Hi mates, good morning.

Please, I'm trying to make my code written in FB work in c++, but everything was fine, until I got the following question.

If I have a function in a lib or dll that returns a string in FB, how can I execute it in my C++ or C code?

example:

function hello() as string
dim text as string = "hello world!"
return text
end function

I've read that strings are nothing more than character arrays, so I tried this in c++:

char* hello();

but when i want to print it, it shows me many weird characters.

what am I doing wrong?

Thank you :)
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: string in c++?

Post by dodicat »

hello.bas (compile -dll)

Code: Select all


#cmdline "-dll"
extern "c++"
function hello() as zstring ptr export
dim text as zstring ptr = strptr("hello world!")
return text
end function
end extern

 
test.cpp

Code: Select all

//test.cpp
#include<iostream>
extern char * hello() ;

int main()
{
	std::cout<<hello()<<std::endl;
	system("pause");
	return 0;
}
//Command: g++.exe "PATH\test.cpp" -o "PATH\test.exe" -l hello  -L"PATH TO LIBRARY FILES"  
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: string in c++?

Post by caseih »

Pretty sure you can CAST a dynamic FB string to a zstring ptr and it does the strptr stuff for you under the hood. For example, you can use printf() to print a FB string directly. So in your FB functions you wish to export to C++ land, return ZString Ptr casts from your dynamic strings.

Note that there is an issue with this---and it's a bug in my opinion (but the devs disagree with me. A zero-length dynamic string casts to a NULL pointer. Just something to be aware of. Pretty sure STRPTR() also returns NULL for zero-length dynamic strings. If C++ land wasn't expecting this, it could lead to seg faults.
maurosebastian91
Posts: 30
Joined: Mar 21, 2021 18:22

Re: string in c++?

Post by maurosebastian91 »

splendid, thank you very much for your answers.

I thought it would be simpler, but it wasn't.

I tell you why.

Yesterday I downloaded the code in this topic,
https://users.freebasic-portal.de/mod/d ... ecksum.zip

and all hashing functions work with string.
I am thinking of using those functions in my application, but I feel that using the zstring ptr, you have to change all the code.
Is there a way to convert a string so that it is taken by the zstring ptr and can be returned correctly.
I just tried it that way, and it keeps throwing me the weird characters.

Thanks and sorry for bothering.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: string in c++?

Post by dodicat »

You might have to shadow (wrap) all the functions returning string.

Code: Select all


#cmdline "-dll"
extern "c++"
function hello() as string 
   dim as string test="hello world!"
    return test
end function

function helloS() as zstring ptr export
    static as string s:s=hello()
    return @s[0]
end function

end extern


 
maurosebastian91
Posts: 30
Joined: Mar 21, 2021 18:22

Re: string in c++?

Post by maurosebastian91 »

Extraordinary, I think it's what I'm looking for.

just one more little query.
what does s:s mean on the line
static as string s:s=hello()

Thanks a lot :)
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: string in c++?

Post by dodicat »

Just a short way of saying
static as string s
s=hello()
using a colon to separate statements is allowed.

you cannot do
static as string s=hello()
"Var-len strings cannot be initialized " would be the error.
SARG
Posts: 1757
Joined: May 27, 2005 7:15
Location: FRANCE

Re: string in c++?

Post by SARG »

A precision : only shared or static var-len strings cannot be initialized but local var-len strings can be.

Code: Select all

static as string s1="ee"   'error
dim shared as string s2="ee"     'error
dim as string s3="ee"   'ok
Post Reply