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
string in c++?
-
- Posts: 30
- Joined: Mar 21, 2021 18:22
Re: string in c++?
hello.bas (compile -dll)
test.cpp
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
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"
Re: string in c++?
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.
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.
-
- Posts: 30
- Joined: Mar 21, 2021 18:22
Re: string in c++?
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.
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.
Re: string in c++?
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
-
- Posts: 30
- Joined: Mar 21, 2021 18:22
Re: string in c++?
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
just one more little query.
what does s:s mean on the line
static as string s:s=hello()
Thanks a lot
Re: string in c++?
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.
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.
Re: string in c++?
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