How to retrieve a BOOL type value returned from a .dll function

Hi! I'm new to the external interface tools of MATLAB and I'm not very familiar with C/C++ so I was wondering if anyone here has experience.
I'm writing a control script for a near-infrared camera, which came with a driver .dll file and a brief description of the functions within it.
For example, I have a function described as:
BOOL DLL_Get_Spectra(double* pdblData, double* pdblCaseTemperature);
Description: Acquire one snapshot spectrum.
Parameter: double* pdblData: Pointer of spectrum power array allocated by the user (Buffer size >= pixel count)
double* pdblCaseTemperature: Pionter of case temperature.
Return value: Boolean TRUE: Operation completed FALSE: Operation failed
After loading dll and using libfunction, we see that the library is loaded without any problems:
[lib.pointer, doublePtr] DLL_Get_Wavelenth(double, doublePtr)
I have read how to pass pointers into MATLAB, but not much on how to read pointers from MATLAB, do I just use doublePtr.Value?
Also, MATLAB does not have a BOOL type, so how am I suppose to read the returned lib.pointer as TRUE or FALSE?
Thanks!

 Réponse acceptée

Philip Borghesani
Philip Borghesani le 10 Jan 2012
That function should not be returning a lib.pointer to MATLAB something went wrong loading the dll. What warnings were reported when loading the library?
BOOL is not a traditional c data type and should be returned to MATLAB as a simple number 0 for false and 1 for true. However a header file is frequently needed to define BOOL for a library. In this case I expect the library header expects windows.h to be included before it is used.
You can fix the header file by adding appropriate typedefs or includes. Or you can generate a prototype file and fix that using uint8 or logical as the data type.

9 commentaires

Many thanks for your reply Philip!
The warnings displayed includes:
Warning: Warnings messages were produced while parsing. Check the functions you intend to use for correctness. Warning text can be viewed using:
[notfound,warnings]=loadlibrary(...)
Warning: The data type 'error' used by function DLL_Get_Spectra does not exist.
> In loadlibrary at 402
I guess this is what you were talking about for when the dll isn't really loaded correctly.
Can you explain to me what header file I should be using?
I tried adding #include<stdbool.h> to the header file but that doesn't seem to be doing the trick. I also tried adding:
typedef int BOOL
#define TRUE 1
#define FALSE 0
But the same error message persists.
Sorry if this seem very fundamental... I still need to learn this but I'm not sure where is the best place to start.
Also, is the "prototype file" that you mentioned a .dll file that I can write to convert the bool type into uint8/logical?
Thanks again!
Qian
Also, when I included #include <windows.h> in the header code. MATLAB gives:
Warning: Warnings messages were produced while parsing. Check the functions you intend
to use for correctness. Warning text can be viewed using:
[notfound,warnings]=loadlibrary(...)
> In loadlibrary at 347
When I load the dll.
It also gives segmentation violation when I try to use calllib to call on any functions inside.
A prototype file is generated using the |mfilename| option to loadlibrary. I expect some warnings when parsing windows.h look in the |warnings| output from loadlibrary (it is text) for warnings concerning the function you are using or the BOOL type.
Qian, please be cautious, as BOOL is not required to be "int". Some systems use an 8 bit data type such as uint8. MS Windows has changed its definition of BOOL over time.
Because of the difference in what size BOOL is, type interfaces between different languages often substitute a different type with well-define size for BOOL.
(It is also possible to run in to size problems when using "char", "int", "float", "double", but usually that is only an issue if you are transporting between machines. If you _are_ transporting between machines, then either use XDR or use very specific types such as int32 .
Thanks Philip and Walter for continuing the follow-up on this thread.
I tried generating a prototype function (Spec2020Dll_proto.m) with #include <windows.h> in the original header as Philip had suggested.
When I used loadlibrary with the prototype file, no warnings were generated:
loadlibrary('Spec2020Dll.dll', @Spec2020Dll_proto)
>>
I used libfunctionview to verify that the function is indeed loaded:
[int32,doublePtr,doublePtr] DLL_Get_Spectra (doublePtr,doublePtr)
I can see now that the BOOL type is now in MATLAB corresponding to int32.
However, when I'm actually calling a function, I get a segmentation violation note and no output.
If it's a problem with MATLAB parsing windows.h, is there another header that I can use to fix this? I've read that there is another header stdbool.h (which is non-stanard) and perhaps I can find a code for it on the web.
Another solution that I'm considering is to write another .dll in C++ that converts the BOOL and the output to "MATLAB-friendly" types, and then use MATLAB to call that new .dll . Having no C++ experience at all, I can already feel the agony of doing so. Do you think that's going too far? Or in your experience is this a feasible solution?
Thanks again!
Qian
I'm not really convinced that BOOL is the problem.
I agree with Walter the BOOL issue has been fixed. I expect that one of the values you are passing into the function is not correct. You will need to supply the MATLAB code you are using to call DLL_Get_Spectraband and more information like the pixel count you expect. I suggest creating a new question with this information.
It has indeed been resolved! I'm not sure why it didn't work yesterday but it seems to run just fine now. Thanks guys!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by