# A little help with importing c library

**URL:** <https://www.ruby-forum.com/t/a-little-help-with-importing-c-library/87077>\
**Category:** Ruby\
**Created:** [February 28, 2007, 12:24am UTC](https://www.ruby-forum.com/t/a-little-help-with-importing-c-library/87077 "2007-02-28T00:24:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ea924/32.png) [@tea](https://www.ruby-forum.com/u/tea)\
**Post date:** [February 28, 2007, 12:24am UTC](https://www.ruby-forum.com/t/a-little-help-with-importing-c-library/87077/1 "2007-02-28T00:24:48Z")

</div>

Hello,

I am very new to Ruby, but I am giving it a try. If I am able to load  
the dll and call its functions, I will go into more detail. The .dll is  
created in VS2005 on XP os

First of all, I used the search function of the forum, but I didn’t find  
what I was looking for.  
After initial problems, my ruby script recognizes the library (if I  
remove the file, it complains).

It’s a C wrapper library for a C++ library that has just a few functions  
for now.

I am stuck at the beginning 🙂  
My library is called qfinterface.dll and I would like to call the  
function CreateSession(const char \*) that returns an int, and pass it a  
string.

I mostly get the error “uninitialized constant” when trying to call the  
function, and I’ve tried so many different things, that I am losing  
track of what I tried and what not. right now I get “undefined local  
variable or method”

## this is my script sofar(in one of its versions):

require ‘qfinterface’

puts “hello”

Qf = qfinterface.CreateSession(“hd”)

def main()

puts “hello”

```
 #QfInterface.CreateSession("sf")

```

end

main()

* * *

would you please point me into right direction in how to call these  
classes.

please let me know if you need further information.

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![tea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ea924/32.png) [@tea](https://www.ruby-forum.com/u/tea)\
**Post date:** [February 28, 2007, 1:37am UTC](https://www.ruby-forum.com/t/a-little-help-with-importing-c-library/87077/2 "2007-02-28T01:37:13Z")

</div>

I got it!!

in case another lost soul needs it, here is how:

require ‘qfinterface’  
require ‘Win32API’

puts “hello”

CreateSession = Win32API.new(“qfinterface”,“CreateSession”,[“P”],“I”)

def main()

```
 puts "hello"
 check = CreateSession.call("mystring")

```

end

main()

---

<div class="post-metadata">

**Author:** ![tea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ea924/32.png) [@tea](https://www.ruby-forum.com/u/tea)\
**Post date:** [February 28, 2007, 4:50am UTC](https://www.ruby-forum.com/t/a-little-help-with-importing-c-library/87077/3 "2007-02-28T04:50:16Z")

</div>

On Feb 27, 2007, at 6:24 PM, Tea Figuric wrote:

> I mostly get the error “uninitialized constant” when trying to call  
> the  
> function, and I’ve tried so many different things, that I am losing  
> track of what I tried and what not. right now I get “undefined local  
> variable or method”

I’m assuming you’ve read README.ext already?  
[http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/RUBY/README.EXT](http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/RUBY/README.EXT)?  
view=markup

Have you investigated using the inline library? It might make more  
sense if you just need a few functions.

> **[rubyinline | software projects | by ryan davis](https://www.zenspider.com/projects/rubyinline.html)**
>
> Inlined foreign code for ultra-fast and flexible ruby.

Using C from Ruby is a bit more complex than just regular ruby  
programming, but hopefully those links can get you started.  
-Mat

---

<div class="post-metadata">

**Author:** ![tea](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7ea924/32.png) [@tea](https://www.ruby-forum.com/u/tea)\
**Post date:** [March 2, 2007, 6:40pm UTC](https://www.ruby-forum.com/t/a-little-help-with-importing-c-library/87077/4 "2007-03-02T18:40:29Z")

</div>

Mat S. wrote:

> On Feb 27, 2007, at 6:24 PM, Tea Figuric wrote:
> 
> I’m assuming you’ve read README.ext already?  
> [http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/RUBY/README.EXT](http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/branches/RUBY/README.EXT)?  
> view=markup
> 
> Have you investigated using the inline library? It might make more  
> sense if you just need a few functions.  
> [rubyinline | software projects | by ryan davis](http://www.zenspider.com/ZSS/Products/RubyInline/)
> 
> Using C from Ruby is a bit more complex than just regular ruby  
> programming, but hopefully those links can get you started.  
> -Mat

Thank you for your response Mat, I will look into the links.

The library should be used by different scripting languages and  
languages, so it should be as generic as possible.

In the meantime, I ran into a different problem, and I am having  
problems finding an answer to it.

Since my C/C++ functions return only int, I have a function to which I  
pass a _buffer (as char_), and befferLength (as int), where the buffer  
should be populated inside of the function, and I have access to the  
value from the outside. something like int (or void) GetValue(char  
\*buffer, int bufferLength);

How do I pass a char\* from ruby? is there something to convert from  
string to char \* without having to change my C lib?

Thanks again for your help!
