crystal-lang


Crystal C bindings: return type char*


I have a function with this signature:
const char* get_version();
My declaration is:
fun get_version(): LibC::Char*
And to use it:
version = MyLib.get_version()
puts version # how to convert to String?
How can I manage the return string? Do I have to import also strlen to measure the length of the C string and covert it to Crystal string manually?
You wrap the Char* pointer with String.new(MyLib.version). If you know the string length, you may call String.new(ptr, size) too.
Note that this will copy the data from the pointer, so the C binding may release its pointer without affecting the Crystal String.

Related Links

JSON.mapping if “root” attribute has inherit keys
Crystal C bindings: using fopen
Define Class object validations
Set attribute of type “Class” to define it as Int32, String, Float64
Set both default attribute and type of attribute
How can I test private methods?
Write binary data to stdout in Crystal
Unable to compile after upgrade from crystal 0.19.4 to 0.20
Application server for Crystal web app / Kemal
Crystal Fiber error - “Unhandled exception in spawn: SSL_shutdown: Operation now in progress (Errno)”
Convert hex string to int in Crystal
Crystal convert string to hash from response.body
Crystal no overload matches 'Array(Type)#[]' with type (Int32 | Nil)
Crystal method return multiple values
Reopening class or struct inside macro
define method with UInt8 variable

Categories

HOME
httparty
azure-stream-analytics
design-patterns
ggplot2
apache-flink
jmeter
nvd3.js
formal-languages
components
hessian
devise
grafana
bitbucket-api
nstableheaderview
augeas
fhir
pega
emoji
gource
package-managers
searchview
red5
xmonad
oledb
charles
microsoft-ocr
joptionpane
event-store
short-url
direct3d11
moses
google-cloud-print
gpio
comparable
realm-mobile-platform
testcomplete
volatile
zend-debugger
android-maps-extensions
getpixel
reverse
jwplayer6
slacktextviewcontroller
lubridate
searchkit
release
connection-pool
stringtokenizer
file-sharing
insight
mongotemplate
avx
otp
flyout
py2neo
android-instrumentation
cifilter
appscale
refinerycms
nservicebus5
nss
custom-errors
dllimport
roadkill-wiki
typeconverter
phpldapadmin
webmatrix-3
rickshaw
genymotion-call
rmysql
cancellation
twitter-bootstrap-rails
websphere-esb
multiautocompletetextview
scriptaculous
embedded-fonts
cmmi
nebula
twisted.web
resource-management
extreme-programming
pstack
nokia-s40
listitem
hibernateexception
relative
node-redis
creole
collectionviewsource
undefined-index
isapi-extension
blender-2.49
silverlight-oob
interop-domino
flvplayback

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App