
Mais bon, je ne peux pas vraiment t'aider, mes connaissance en la matière sont un peu limité, bien que je me considère comme débrouillard dans le domaine.

Je n'ai pas l'impresison que ce soit spécifique à LCC... Il faut utiliser SYS_GetOutputBuffer() pour demander à PB de réserver un espace pour nous et pouvoir l'utiliser à notre guise. C'est encore plus simple que ton code...VI. Managing strings
--------------------
Managing strings in PureBasic 4.0 and above isn't obvious anymore. If a function use strings but doesn't
returns string, there is not problem at all. Now when it returns string, there is different case:
1) The function doesn't has string parameter
It's the easiest one, and you can use the SYS_GetOutputBuffer() or SYS_GetOutputBufferESP() macro
in LCC.
SYS_GetOutputBuffer(char *Output, int NeededLength, char *NbParameters)
With this command you ask PureBasic to returns a string buffer of 'NeededLength' (in characters, which means
than it's the same value in unicode or not) and store the pointer value in 'Output'. 'NbParameters' is a
string number which indicates how many parameters the function has.
ex: SYS_GetOutputBuffer(Output, 100, "2")
Once you get this buffer, just write the string result in it and you're done.
Note: the 'NeededLength' should be the exact returned string length. It doesn't count the null terminating character
WARNING: When using LCC, there is sometimes an issue when the function doesn't have any parameters, and is very short.
LCC then optimize it by using only registers and then SYS_GetOutputBuffer() will fails. In this case, you can generate
the .asm code and check the procedure start, if ebp is used or not. If not, you will have to use SYS_GetOutputBufferESP()
and put the offset number. It very rare case, but can happen.
2) If the function has some string parameters
You still have to use the above functions, but before you may need to to call SYS_GetParameterIndex() and SYS_ResolveParameter()
if you need to use the string parameters after having called SYS_GetOutputBuffer(). Why that ? Because if one of your
parameter is a composed string (like a$+b$), it will reside on the internal buffer. When you request some length (let's say
a big length), it can be reallocated, which means than your string pointers won't be correct anymore. SYS_GetParameterIndex() will
get the index in the buffer, if the string is in the buffer (or return 0 else). SYS_ResolveParameter() will rebuild the
string pointer once SYS_GetOutputBuffer() will be called.
A typical C code will look like that:
M_PBFUNCTION void PB_LSet(const TCHAR *String, int Length)
{
[...]
ParameterIndex = SYS_GetParameterIndex(String);
SYS_GetOutputBuffer(Cursor, StringLength, "2");
if (ParameterIndex)
String = SYS_ResolveParameter(ParameterIndex);
[...]
}
Now, sometimes you can't know the size of the result string (or it will be too long to compute it. In this can, you
can request a fixed size and adjust it at the end with the SYS_ReduceStringSize(Length) function. 'Length' is the
number of byte to reduce the buffer. If you have requested a buffer of 4096 with SYS_GetOutputBuffer() and your
string do only 4000 bytes, you will have to reduce it from 96 bytes ie: SYS_ReduceStringSize(96).
Bah siIt's the easiest one, and you can use the SYS_GetOutputBuffer() or SYS_GetOutputBufferESP() macro
in LCC.
Code : Tout sélectionner
M_SYSFUNCTION(TCHAR *) SYS_GetOutputBuffer(int Length, int PreviousPosition);
Code : Tout sélectionner
#if defined(WINDOWS)
extern "C" __stdcall char * SYS_GetOutputBuffer(int, int);
#define EXPORT extern "C" _stdcall
#endif
#if defined(LINUX)
extern "C" char * SYS_GetOutputBuffer(int, int);
#define EXPORT extern "C"
#endif
EXPORT char * PB_RetourString(int pos)
{
std::string Test("This is a test for purebasic !");
int size = Test.size();
char * out = SYS_GetOutputBuffer(size, pos);
memcpy(out,Test.data(),size);
return out;
}
RetourString()
String
C'est déjà bien, ça m'a aussi permis de chercher pour KCC dans la foulée. Sinon, dans le Purelibrary.h on voit qu'il y a plein d'autres fonctions liées à la gestion des chaines.G-Rom a écrit :Merci Djes de m'avoir aiguillé , sa marche ici.
Pour renvoyer un string d'un code c++ vers pb :
Code : Tout sélectionner
#if defined(WINDOWS) extern "C" __stdcall char * SYS_GetOutputBuffer(int, int); #define EXPORT extern "C" _stdcall #endif #if defined(LINUX) extern "C" char * SYS_GetOutputBuffer(int, int); #define EXPORT extern "C" #endif EXPORT char * PB_RetourString(int pos) { std::string Test("This is a test for purebasic !"); int size = Test.size(); char * out = SYS_GetOutputBuffer(size, pos); memcpy(out,Test.data(),size); return out; }
Le fichier .descRetourString()
String
le point clé c'est le paramètre caché "int pos" de la fct° perso, en fouillant dans la mémoire à coté de l'adresse du string je ne pouvais pas trouvé , seul un gars calé en asm aurais pu voir ca, pas moi.
le principal c'est que c'est la fête du string maintenant !
EDIT : Marche pas en unicode , faut que je trouve une macro qui me detecte ca dans ce bordel.
@+