From: Eyup Cinar on
Hello,

I have been trying to call a dll library that grabs data from an acquisition device.

I have managed to load the libraries without any problem into Matlab's workspace, I can see the function definitions from matlab. I started programming but got stuck on one thing which I don't know how to solve.

I am trying to call a function from a dlll with a function definition such as follows:

extern "C" __declspec(dllimport) int _stdcall EE_DataGetNumberOfSample (DataHandle hData, unsigned int* nSampleOut);

it updates the number of samples read from the buffer into the variable, nSampleOut , so in order to call this function in Matlab this is what I am doing.

[hData]=calllib('dataLib','EE_DataCreate'); % dataHandle pointer
nSamplesTakenPtr=uint32(0); % unsigned integer create and initialize to 0

calllib('dataLib','EE_DataGetNumberOfSample',hData,nSamplesTakenPtr);


However I can't see any change in the variable when I look at the workspace, it stays all the time as initialized to 0. Isn't the Matlab converts the pass by value to pass by reference by itself ?

I can see with other data read functions the data is coming to the Matlab's workspace with the same data handle however I need to have this variable to be updated periodically.

Any idea is appreciated.

Thanks
From: Philip Borghesani on

"Eyup Cinar" <exc8020(a)rit.edu> wrote in message news:hscqdg$2j4$1(a)fred.mathworks.com...
>
> I am trying to call a function from a dlll with a function definition such as follows:
>
> extern "C" __declspec(dllimport) int _stdcall EE_DataGetNumberOfSample (DataHandle hData, unsigned int* nSampleOut);
>
> it updates the number of samples read from the buffer into the variable, nSampleOut , so in order to call this function in Matlab
> this is what I am doing.
> [hData]=calllib('dataLib','EE_DataCreate'); % dataHandle pointer
> nSamplesTakenPtr=uint32(0); % unsigned integer create and initialize to 0
>
> calllib('dataLib','EE_DataGetNumberOfSample',hData,nSamplesTakenPtr);
>

MATLAB passes by value unless a libpointer object is used. The simplest way to let calllib do the conversions for you is to use two
(or three) outputs from calllib:

[retval,copyofhData,SamplesTaken]=calllib('dataLib','EE_DataGetNumberOfSample',hData,0);
it is possible that only two outputs are needed:
[retval,SamplesTaken]=calllib('dataLib','EE_DataGetNumberOfSample',hData,0);

If you wish to use a libpointer do this:
nSamplesTakenPtr=libpointer('uint32Ptr',0); % unsigned integer create and initialize to 0

calllib('dataLib','EE_DataGetNumberOfSample',hData,nSamplesTakenPtr);
samples=nSamplesTakenPtr.value;

Phil