Hello,
i trying to use getParam function from IUP.
There is original function in C:
int IupGetParam(const char* title, Iparamcb action, void* user_data, const char* format,...);
This is how this function is wrapped in module:
proc getParam*(title: cstring; action: Iparamcb; user_data: pointer; format: cstring): cint
{.varargs, cdecl, importc: "IupGetParam", dynlib: libname.}
And this is, how i using this function in my code:
var first: string = ""
var second: string = ""
var third: string = ""
let status = getParam("Title", nil, nil,
"Input 1: %s\nInput 2: %s\nBool 1: %b\n", first, second, third, nil)
But i dont know hot to work with result variables...
If i do:
echo first ... then output is properly echoed, but:
echo len(first) ... shows always 0
var test = first
echo test ... shows only first character from "first" string
trim(first) ... returns empty string, etc...
What im doing wrong?
Thanks for help.
EDIT: Added original C function.
Are you familiar with IUP -- have you ever used it before?
I really think first, second, third as RESULT variables can not work this way. You have defined them as Nim string variables, C lib can not know how to handle it. Have not really an idea what IupGetParam() does, a short Google search gave me some docs, but I am not sure about the ... parameters. Is that really output parameters? And really string? Who does memory allocation? Maybe pointers to value?
Sorry can not really help -- maybe someone other knows what IUP is.
Yes i used IUP before. But only from C. But im trying using it from Nim for the first time...
IupGetParam() is method which popup modal Dialog with input fields. And text from this input fields is then returned in respective variables.
There is exmaple how it works in C:
http://webserver2.tecgraf.puc-rio.br/iup/examples/C/getparam.c
In C you will define string, for example: char first[100] = ""; And it will populate this string with result.
I tried it already also with cstrings, but it segafulted then... so i dont know.
var log: string = newString(bytesize)
Of course len() may be wrong when your C lib changes the string -- I think Nim's len() stores length separately as Delphi did, it may not search for terminating 0X.
YES! Allocation..that was the problem!
I dont know how was i able to miss it... Its logical... i working with C-string, so i need to allocate him :)
newString works also for cstrings.
So, now i doing it this way:
1) allocating new C-string
first: cstring = newString(100)
3) and then converting result to Nim-string
final = $first
Done. Now it works out the way it's supposed to..
Thank you Stefan for help!