Work with DLL file
62 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Guillaume
2020-1-28
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.
0 个评论
Peer Blumido
2020-1-28
编辑:Peer Blumido
2020-1-28
8 个评论
Mohammad Sami
2020-1-30
Looks like you have manage to get it to work. You can add these as functions in your wrapper class.
Mohammad Sami
2020-1-30
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!