A Win32API question

B

Ben

Hi, I copied a dll from a matlab sample. (matlab uses this DLL to demo
its dll loading function). one of the functions in this DLL is
stringToUpper which has the prototype

EXPORTED_FUNCTION char* stringToUpper(char *);

For the arguments in Win32API.new, I assumed the 3rd arg is the input
type, so tried 'S' as string. 'L' is the return type as long. Not sure
if I need to use 'S' instead.

If a function has 3 parameters, do I need to write ['S', 'L', 'F'] for
string, integer, float inputs?

=========================================
irb(main):001:0> require 'Win32API'
=> true
irb(main):002:0> aa =
Win32API.new("shrlibsample.dll","stringToUpper",['S'],'L' )
RuntimeError: LoadLibrary: shrlibsample.dll

from (irb):2:in `initialize'
from (irb):2
irb(main):003:0>
==========================================

Any comments are welcome.
 
D

Daniel Berger

Ben said:
Hi, I copied a dll from a matlab sample. (matlab uses this DLL to demo
its dll loading function). one of the functions in this DLL is
stringToUpper which has the prototype

EXPORTED_FUNCTION char* stringToUpper(char *);

What does this do that String#upcase doesn't? Just curious.
For the arguments in Win32API.new, I assumed the 3rd arg is the input
type, so tried 'S' as string. 'L' is the return type as long. Not sure
if I need to use 'S' instead.

Eh? What 3rd argument? Looks like it has one argument to me, a char
pointer, which is represented by a 'P'.
If a function has 3 parameters, do I need to write ['S', 'L', 'F'] for
string, integer, float inputs?

A string is a char pointer, thus it's represented by a 'P'. For a
float just use 'L'.
=========================================
irb(main):001:0> require 'Win32API'
=> true
irb(main):002:0> aa =
Win32API.new("shrlibsample.dll","stringToUpper",['S'],'L' )
RuntimeError: LoadLibrary: shrlibsample.dll

from (irb):2:in `initialize'
from (irb):2
irb(main):003:0>
==========================================

There are a few possibilities here. The first thing to check is that
your dll file is named properly and is in your dll load path. The next
thing to check is that your function is, in fact, exported and that
you've matched the case exactly. The last thing you'll want to do is
correct the prototype to 'P' (although that won't cause the
RuntimeError at this point).

Hope that helps.

Dan
 
B

Ben

Thank you very much for your help.

This is a toy function which converts 'abcde' to 'ABCDE'.

Checked the export name with dumpbin. I have the C source and header
files also. Thus, DLL filename and function name are correct.

Modified 'S' to 'P'. But the same error appears:

====================================================
irb(main):004:0> aa =
Win32API.new("shrlibsample.dll","stringToUpper",['P'],'L')
RuntimeError: LoadLibrary: shrlibsample.dll

from (irb):4:in `initialize'
from (irb):4
from :0
====================================================


I am missing C and Fortran now. I hate to sit there and have no clue to
modify even a single letter.
 
D

Daniel Berger

Ben said:
Thank you very much for your help.

This is a toy function which converts 'abcde' to 'ABCDE'.

Checked the export name with dumpbin. I have the C source and header
files also. Thus, DLL filename and function name are correct.

Modified 'S' to 'P'. But the same error appears:

====================================================
irb(main):004:0> aa =
Win32API.new("shrlibsample.dll","stringToUpper",['P'],'L')
RuntimeError: LoadLibrary: shrlibsample.dll

from (irb):4:in `initialize'
from (irb):4
from :0
====================================================


I am missing C and Fortran now. I hate to sit there and have no clue to
modify even a single letter.

This is your clue:

RuntimeError: LoadLibrary: shrlibsample.dll

Behind the scenes Win32API.new is just calling LoadLibrary() +
GetProcAddress(). What you can see from this error message is that
LoadLibrary() is failing. That tells me the most likely culprit is
your load path.

In any case, you can specify a full path the to .dll file in the first
argument:

Win32API.new("C:\\path\\to\\shrlibsample.dll", "stringToUpper", 'P',
'P')

The 3rd argument can be either an array of characters or a simple
string - either way is fine. The final argument should be a 'P' as
well since the function returns a char pointer.

Regards,

Dan
 
B

Ben

Many thanks for your patience and help. I've learnt a lot from your
replies. The Win32API docs are too skinnie.

This becomes a two-line puzzle to me. Do I need to pick another DLL? I
can load this DLL under Matlab.
=====================================
D:\shrlib>irb
irb(main):001:0> require 'Win32API'
=> true
irb(main):002:0> aa =
Win32API.new("D:\\shrlib\\shrlibsample.dll","stringToUpper",'P','P')
RuntimeError: LoadLibrary: D:\shrlib\shrlibsample.dll

from (irb):2:in `initialize'
from (irb):2
irb(main):003:0>
==============================================

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top