Work with DLL file

88 visualizzazioni (ultimi 30 giorni)
Peer Blumido
Peer Blumido il 23 Gen 2020
Commentato: Mohammad Sami il 30 Gen 2020
Hello,
im trying to work with a DMD from Vialux. All i know is that i have to use the functions from the DLL File. I loaded it and all functions are in the workspace.
Now i want to use at frist one function "AlpDev Alloc". In the Description it says "long AlpDevAlloc (long DeviceNum, long InitFlag, ALP_ID *DeviceIdPtr)". I get three ouputs but i have to give three inputs. First two of them are given as "ALP_DEFAULT" for DeviceNum and InitFlag (4 byte integer value). For "DeviveIdPtr" pointer to a writeable 4-byte integer. Its completly new for me with DLLs, could anyone help with this question what i have to do with "ALP_ID *DevideIdPtr" and is the rest correct?
Thanks a lot!
loadlibrary('alp4395', 'alp.h');
libfunc = libfunctions('alp4395');
% long AlpDevAlloc (long DeviceNum, long InitFlag, ALP_ID *DeviceIdPtr) in documentation
[res1, res2, res3] = calllib('alp4395',libfunc{1,1},'ALP_Default', 'ALP_Default',?);
  5 Commenti
Peer Blumido
Peer Blumido il 28 Gen 2020
Modificato: Peer Blumido il 28 Gen 2020
I want to show the information i have.
From the Description ( AlpDevAlloc Function):
From the h. - File:
Should i set DeviceNum and InitFlag to 0 or is this value i get as return? I thought i have to write it as a string in the calllib function.
For ALP_Default = 0:
ID = 0;
ALP_ID = libpointer('uint32',ID); % e.g. DataType = 'int32'
DeviceNum = 0;
InitFlag = 0;
[res1, res2] = calllib('alp4395','AlpDevAlloc',DeviceNum,InitFlag,ALP_ID);
This gives me an error in Calllib "Array must be numeric or logical or a pointer to one.".
Something happend i tried this:
ID = 0;
ALP_ID = libpointer; % e.g. DataType = 'int32'
DeviceNum = 0;
InitFlag = 0;
[res1, res2] = calllib('alp4395','AlpDevAlloc',DeviceNum,InitFlag,ALP_ID);
I get no error and in the workspace for res1 = 1001 and res2 = [ ]. At the moment the device is not connected. Maybe if its connected res2 should show me ALP_ID.
I'm in the laboratory now and i used this:
deviceid = uint32(0);
ALP_ID = libpointer('uint32Ptr', deviceid);
DeviceNum = uint32(0);
InitFlag = uint32(0);
[x1, x2] = calllib('alp4395','AlpDevAlloc',DeviceNum,InitFlag,ALP_ID);
My two ouputs [long, longPtr] are x1 = 1001 and x2 = 0. x1 should be alp_returnvalue and x2 the ALP_ID. But 0 seems not like an ID.
Guillaume
Guillaume il 28 Gen 2020
Can you check in the dll documentation what a return value of 1001 mean?

Accedi per commentare.

Risposte (2)

Guillaume
Guillaume il 28 Gen 2020
Your dll function takes 3 inputs, the first two are scalar numeric indeed. The snippet of documentation you show is not very clear but indeed it looks like you want 0 for the default.
The 3rd input is a pointer to some memory where the function will write something.From your header file snippet it looks like it is indeed a 4-bit unsigned integer (assuming that the dll defines long as a 32-bit integer, which is fairly standard but not guaranteed).
The function only has one output, which is probably an error code or success. I'm unclear why you say you get 3 outputs.
In theory,
deviceptr = libpointer('uint32'); %No need to assign a value, it's overwritten by AlpDevAlloc
res = calllib('alp4395', 'AlpDevAlloc', 0, 0, deviceptr);
ALP_ID = deviceptr.Value;
should work according to the information you've provided. However, do check the value of res against the dll documentation as it may indicate an error of some kind.
If you get a matlab error with the above, then we've not properly understood the signature.
Note that when you did:
ALP_ID = libpointer;
res1 = calllib('alp4395','AlpDevAlloc',DeviceNum,InitFlag,ALP_ID);
you passed a NULL pointer to the library. Two things can happen in this case: the library tries to write to the NULL address and crashes, or the dll checks that the pointer is not NULL and returns an error. Since it didn't crash, it must have returned an error code which I assumes is that 1001 value.

Peer Blumido
Peer Blumido il 28 Gen 2020
Modificato: Peer Blumido il 28 Gen 2020
Hello Guillaume,
thanks for your answer! Thats what ive got about the Function AlpDevAlloc:
I know it wasnt correct that i receive 3 outputs. Im so sorry if i did many dumb mistakes but this is completly new for me. Never worked with DLLs before.
I would suggest this, because it worked without any Error.
loadlibrary('alp4395.dll', 'alp.h');
libfunc = libfunctions('alp4395');
deviceid = uint32(0);
ALP_ID = libpointer('uint32Ptr', deviceid);
DeviceNum = int32(0);
InitFlag = int32(0);
[alp_returnvalue, ALP_ID] = calllib('alp4395','AlpDevAlloc',DeviceNum,InitFlag,ALP_ID);
i hope this can help more find the issue.
i dont know what happend but i wrote at the end
unloadlibrary('alp4395')
and matlab crashed. After removing this and new run i had this in workspace:
5.png
After a second run it turned to the values in workspace above.
  8 Commenti
Mohammad Sami
Mohammad Sami il 30 Gen 2020
Looks like you have manage to get it to work. You can add these as functions in your wrapper class.
Mohammad Sami
Mohammad Sami il 30 Gen 2020
Also I would recommend that your class manage all the pointers internally. If any of the c functions return pointers those may need to be freed once you don't need them or when you are deleting this object.
What I did was to create a private property to store pointers in a cell array. Once the pointer is cleared in C (your dll my have a clear or free function), just set it to libpointer (null).
If a function in c returns a pointer, its stored in the cell array, and i return the cell array index rather then the pointer.
% release pointers
if(~obj.pointers{i}.isNull)
calllib('lib...','release....free...',obj.pointers{i});
obj.pointers{i} = libpointer; % set to null
end

Accedi per commentare.

Categorie

Scopri di più su App Building in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by