How to run Matlab generated .DLL files from Python

9 ビュー (過去 30 日間)
Patiala Ghos
Patiala Ghos 2021 年 7 月 21 日
コメント済み: Pratyush 2025 年 8 月 19 日
I have written a simple test code as shown below:
function out = test(a,b)
out = a+b;
end
Then I have created .DLL file from MATLAB-> APPS -> library compiler -> c Shared Library.
Then I have tried to run it from Python. Here is my test code -
import ctypes
mydll = ctypes.cdll.LoadLibrary("test.dll")
a = 12
b = 100
c = mydll.test(a,b)
I expected to get 112. Instead I am getting following error -
AttributeError: function 'test' not found
I am using Matlab 2021a and have already installed Matlab runtime compiler v910. I think I need to invoke it from Python but don't know how to do this.
Can anyone suggest me how can I call Matlab generated .DLL file from Python?
TIA
  3 件のコメント
Patiala Ghos
Patiala Ghos 2021 年 7 月 22 日
Hi,
Here are two things that I need to take care of –
  1. I need to protect my code so that the end-user cannot change it easily. That’s why I want to convert it to a binary file. So, I chose to convert it to a .dll file. Now, as for the Python package, as far as I know, it is converted to .ctf format. I think .ctf format also converts to a binary file but I am sure.
  2. For my project, processing time is a concerning issue. That’s why I need to know which one is the faster way of running Matlab code in python – converting to .dll or converting to a python package.
Thank you.
cr
cr 2023 年 7 月 17 日
There could be reasons why one would want to generate c shared lib for use in python. Licence is one. I have compiler lic and 2014b give me a way to output dll. To generate python libs I need another expensive licence. I have never been able to make dll work from C/C++

サインインしてコメントする。

回答 (2 件)

Yongjian Feng
Yongjian Feng 2021 年 7 月 23 日
Try to call your dll from a C program first, to make sure it actually works?
  1 件のコメント
Mark Magdaleno
Mark Magdaleno 2022 年 3 月 17 日
Does this mean the python syntax is correct as indicated? If not can you supply know to work python syntax.
Thanks

サインインしてコメントする。


Georgy Gilyarovskiy
Georgy Gilyarovskiy 2023 年 10 月 6 日
I have relative question:
import ctypes
FileName = 'get_frame.dll'
mydll = ctypes.CDLL(FileName)
i = ctypes.c_int32(5)
s = ctypes.c_int32(32)
a = ctypes.create_unicode_buffer(1024)
mydll.get_frame(i, s, ctypes.byref(a))
This code gives me the following error:
OSError: exception: access violation reading 0x0000000000000000
I suppose that it's related to my python. It's probably 64-bit version.
Do you have any ideas how to solve this problem properly?
  1 件のコメント
Pratyush
Pratyush 2025 年 8 月 19 日
Were you able to resolve your issue?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangePython Package Integration についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by