尝试从 matlab 调用 dll 函数导致崩溃

2023-12-22

我尝试在 MATLAB 中使用第三方外部 DLL(来自 usbmicro),但它总是使 MATLAB 崩溃。这是来自说明 C 程序中函数调用语法的文档:

int USBm_About( char *about );

我尝试了这个 MATLAB 脚本(是的,它非常笨拙,我是 MATLAB 菜鸟):

>> loadlibrary('USBm.dll','USBmAPI.h')
>> libfunctions('USBm')
>> s='sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss';
>> st=strcat(s,s,s,s);
>> vp = libpointer('voidPtr',[int8(st) 0]);
>> result=calllib('USBm','USBm_About',vp)

和这个:

>> loadlibrary('USBm.dll','USBmAPI.h')
>> libfunctions('USBm')
>> s='sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss';
>> st=strcat(s,s,s,s);
>> vp=libpointer('cstring',st);
>> result=calllib('USBm','USBm_About',vp)

在这两种情况下,calllib()调用会导致 MATLAB 因分段错误而崩溃。

MATLAB版本为7.10;操作系统是Windows Vista。


Update:

Here's a screenshot of libfunctionsview USBm: screenshot

这是头文件:

#ifndef FILE_USBmAPI_h
#define FILE_USBmAPI_h


// Prototypes for this DLL.
// These are the API functions available to the .dll user.


// Discovery routine
extern "C" __declspec(dllexport) int USBm_FindDevices(void);

// Return info about devices
extern "C" __declspec(dllexport) int USBm_NumberOfDevices(void);
extern "C" __declspec(dllexport) int USBm_DeviceValid(unsigned char);
extern "C" __declspec(dllexport) int USBm_DeviceVID(unsigned char);
extern "C" __declspec(dllexport) int USBm_DevicePID(unsigned char);
extern "C" __declspec(dllexport) int USBm_DeviceDID(unsigned char);
extern "C" __declspec(dllexport) int USBm_DeviceFirmwareVer(unsigned char);
extern "C" __declspec(dllexport) int USBm_DeviceMfr(unsigned char, char *);
extern "C" __declspec(dllexport) int USBm_DeviceProd(unsigned char, char *);
extern "C" __declspec(dllexport) int USBm_DeviceSer(unsigned char, char *);

// General USBmicro U4xx device access
extern "C" __declspec(dllexport) int USBm_ReadDevice(unsigned char, unsigned char *);
extern "C" __declspec(dllexport) int USBm_SetReadTimeout(unsigned int);
extern "C" __declspec(dllexport) int USBm_WriteDevice(unsigned char, unsigned char *);
extern "C" __declspec(dllexport) int USBm_CloseDevice(unsigned char);

// DLL string info access
extern "C" __declspec(dllexport) int USBm_RecentError(char *);
extern "C" __declspec(dllexport) int USBm_ClearRecentError(void);
extern "C" __declspec(dllexport) int USBm_DebugString(char *);
extern "C" __declspec(dllexport) int USBm_Copyright(char *);
extern "C" __declspec(dllexport) int USBm_About(char *);
extern "C" __declspec(dllexport) int USBm_Version(char *);



// General U4x1 device functions
// -----------------------------

// Port initialization
extern "C" __declspec(dllexport) int USBm_InitPorts(unsigned char);
extern "C" __declspec(dllexport) int USBm_InitPortsU401(unsigned char);
extern "C" __declspec(dllexport) int USBm_InitPortsU421(unsigned char);
extern "C" __declspec(dllexport) int USBm_InitPortsU451(unsigned char);

// Port/bit reading and writing
extern "C" __declspec(dllexport) int USBm_WriteA(unsigned char, unsigned char);
extern "C" __declspec(dllexport) int USBm_WriteB(unsigned char, unsigned char);
extern "C" __declspec(dllexport) int USBm_WriteABit(unsigned char, unsigned char, unsigned char);
extern "C" __declspec(dllexport) int USBm_WriteBBit(unsigned char, unsigned char, unsigned char);
extern "C" __declspec(dllexport) int USBm_ReadA(unsigned char, unsigned char *);
extern "C" __declspec(dllexport) int USBm_ReadB(unsigned char, unsigned char *);
extern "C" __declspec(dllexport) int USBm_SetBit(unsigned char, unsigned char);
extern "C" __declspec(dllexport) int USBm_ResetBit(unsigned char, unsigned char);

// Port direction
extern "C" __declspec(dllexport) int USBm_DirectionA(unsigned char, unsigned char, unsigned char);
extern "C" __declspec(dllexport) int USBm_DirectionAOut(unsigned char);
extern "C" __declspec(dllexport) int USBm_DirectionAIn(unsigned char);
extern "C" __declspec(dllexport) int USBm_DirectionAInPullup(unsigned char);
extern "C" __declspec(dllexport) int USBm_DirectionB(unsigned char, unsigned char, unsigned char);
extern "C" __declspec(dllexport) int USBm_DirectionBOut(unsigned char);
extern "C" __declspec(dllexport) int USBm_DirectionBIn(unsigned char);
extern "C" __declspec(dllexport) int USBm_DirectionBInPullup(unsigned char);

// Strobbing a byte of data
extern "C" __declspec(dllexport) int USBm_StrobeWrite(unsigned char, unsigned char, unsigned char, unsigned char);
extern "C" __declspec(dllexport) int USBm_StrobeRead(unsigned char, unsigned char *, unsigned char, unsigned char);
extern "C" __declspec(dllexport) int USBm_StrobeWrite2(unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char);
extern "C" __declspec(dllexport) int USBm_StrobeRead2(unsigned char, unsigned char *, unsigned char, unsigned char, unsigned char, unsigned char);
extern "C" __declspec(dllexport) int USBm_StrobeWrites(unsigned char, unsigned char *, unsigned char *);
extern "C" __declspec(dllexport) int USBm_StrobeReads(unsigned char, unsigned char *, unsigned char *);

// Reading pin-change latches
extern "C" __declspec(dllexport) int USBm_ReadLatches(unsigned char, unsigned char *);

// LCD routines
extern "C" __declspec(dllexport) int USBm_InitLCD(unsigned char, unsigned char, unsigned char);
extern "C" __declspec(dllexport) int USBm_LCDCmd(unsigned char, unsigned char);
extern "C" __declspec(dllexport) int USBm_LCDData(unsigned char, unsigned char);

// SPI routines
extern "C" __declspec(dllexport) int USBm_InitSPI(unsigned char, unsigned char);
extern "C" __declspec(dllexport) int USBm_SPIMaster(unsigned char, unsigned char *, unsigned char *);
extern "C" __declspec(dllexport) int USBm_SPISlaveWrite(unsigned char, unsigned char, unsigned char *);
extern "C" __declspec(dllexport) int USBm_SPISlaveRead(unsigned char, unsigned char *, unsigned char *);

// 2-wire routines
extern "C" __declspec(dllexport) int USBm_Wire2Control(unsigned char, unsigned char *);
extern "C" __declspec(dllexport) int USBm_Wire2Data(unsigned char, unsigned char *);

// Stepper routine
extern "C" __declspec(dllexport) int USBm_Stepper(unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char, unsigned char);

// 1-wire routines
extern "C" __declspec(dllexport) int USBm_Reset1Wire(unsigned char, unsigned char *);
extern "C" __declspec(dllexport) int USBm_Write1Wire(unsigned char, unsigned char);
extern "C" __declspec(dllexport) int USBm_Read1Wire(unsigned char, unsigned char *);
extern "C" __declspec(dllexport) int USBm_Write1WireBit(unsigned char, unsigned char);
extern "C" __declspec(dllexport) int USBm_Read1WireBit(unsigned char, unsigned char *);




#endif // multiple inclusion prevention

// End of file
//---------------------------------------------------------------------------

Update:

我尝试改变这一行:

extern "C" __declspec(dllexport) int USBm_About(char *);

to this:

extern "C" __declspec(dllimport) int USBm_About(char *);

在头文件中,然后重新启动 MATLAB。我再次运行代码,MATLAB 仍然崩溃。


我从以下位置下载了 DLLUSBmicro http://www.usbmicro.com/网站,并尝试use it http://www.mathworks.com/help/techdoc/matlab_external/f23224dfi7.html通过致电loadlibrary() http://www.mathworks.com/help/techdoc/ref/loadlibrary.html。不幸的是,正如你所说,这使我的 MATLAB 会话崩溃了。

经过一些research http://www.mathworks.com/support/solutions/data/1-671ZZL.html,我发现这个 DLL 公开其函数的方式与 MATLAB 期望的调用约定不兼容(cdecl 与 stdcall)。

这是我修复头文件的方法:

#ifndef FILE_USBmAPI_h
#define FILE_USBmAPI_h

#ifdef __cplusplus
extern "C" {
#endif

// ...
int __stdcall USBm_About(char *);
int __stdcall USBm_Version(char *);
// ...

#ifdef __cplusplus
}
#endif

#endif

现在您可以调用任何导出的函数。例子:

%# load library and see exported functions signatures
if ~libisloaded('USBm')
    loadlibrary('USBm.dll','USBmAPI.h')
    libfunctions('USBm','-full')
end

%# call the function: `int USBm_About(char *)`
str = repmat(' ',1,100);                           %# allocate buffer
pStr = libpointer('stringPtr',str);                %# pointer to string
[num str2] = calllib('USBm','USBm_About',pStr)
clear pStr

%# unload library
unloadlibrary USBm

请注意 MATLAB 为我们提供的函数签名:

[int32, cstring] USBm_About(cstring)

有趣的是,这个函数有一个额外的输出参数。原因是 MATLAB 并不真正支持按引用传递,尽管您可以创建与 C 指针兼容的 MATLAB 参数。

因此,如果 C 函数在通过引用传递的输入参数中返回数据,MATLAB 将创建其他输出参数来返回这些值(以及以Ptr or PtrPtr).

现在虽然输入参数pStr类似于指向 char 类型的指针,但它不是真正的指针,因为它不包含 MATLAB 字符数组的地址str。函数执行时,返回正确结果,但不修改中的值str(在这种情况下也不pStr对于这一点)。

我得到的输出是:

>> num
num =
     0

>> str2
str2 =
 USBm.dll by USBmicro L.L.C. (www.usbmicro.com). Supports: U401, U421
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尝试从 matlab 调用 dll 函数导致崩溃 的相关文章

  • 结构化绑定中缺少类型信息

    我刚刚了解了 C 中的结构化绑定 但有一件事我不喜欢 auto x y some func is that auto正在隐藏类型x and y 我得抬头看看some func的声明来了解类型x and y 或者 我可以写 T1 x T2 y
  • C# 异步等待澄清?

    我读了here http blog stephencleary com 2012 02 async and await html that 等待检查等待的看看它是否有already完全的 如果 可等待已经完成 那么该方法将继续 运行 同步
  • 没有特殊字符的密码验证器

    我是 RegEx 的新手 已经进行了大量搜索 但没有找到任何具体内容 我正在编写一个验证密码字符串的正则表达式 可接受的字符串必须至少具有 4 种字符类型中的 3 种 数字 小写字母 大写字母 特殊字符 我对包含有一个想法 也就是说 如果这
  • 通过引用传递 [C++]、[Qt]

    我写了这样的东西 class Storage public Storage QString key const int value const void add item QString int private QMap
  • std::vector 与 std::stack

    有什么区别std vector and std stack 显然 向量可以删除集合中的项目 尽管比列表慢得多 而堆栈被构建为仅后进先出的集合 然而 堆栈对于最终物品操作是否更快 它是链表还是动态重新分配的数组 我找不到关于堆栈的太多信息 但
  • 随着时间的推移,添加到 List 变得非常慢

    我正在解析一个大约有 1000 行的 html 表 我从一个字符串中添加 10 个字符串 td 每行到一个list td
  • 需要帮助优化算法 - 两百万以下所有素数的总和

    我正在尝试做一个欧拉计划 http projecteuler net问题 我正在寻找 2 000 000 以下所有素数的总和 这就是我所拥有的 int main int argc char argv unsigned long int su
  • 访问外部窗口句柄

    我当前正在处理的程序有问题 这是由于 vista Windows 7 中增强的安全性引起的 特别是 UIPI 它阻止完整性级别较低的窗口与较高完整性级别的窗口 对话 就我而言 我想告诉具有高完整性级别的窗口进入我们的应用程序 它在 XP 或
  • C# - 当代表执行异步任务时,我仍然需要 System.Threading 吗?

    由于我可以使用委托执行异步操作 我怀疑在我的应用程序中使用 System Threading 的机会很小 是否存在我无法避免 System Threading 的基本情况 只是我正处于学习阶段 例子 class Program public
  • 为什么这个字符串用AesCryptoServiceProvider第二次解密时不相等?

    我在 C VS2012 NET 4 5 中的文本加密和解密方面遇到问题 具体来说 当我加密并随后解密字符串时 输出与输入不同 然而 奇怪的是 如果我复制加密的输出并将其硬编码为字符串文字 解密就会起作用 以下代码示例说明了该问题 我究竟做错
  • 如何定义一个可结构化绑定的对象的概念?

    我想定义一个concept可以检测类型是否T can be 结构化绑定 or not template
  • 实例化类时重写虚拟方法

    我有一个带有一些虚函数的类 让我们假设这是其中之一 public class AClassWhatever protected virtual string DoAThingToAString string inputString retu
  • 编译时展开 for 循环内的模板参数?

    维基百科 here http en wikipedia org wiki Template metaprogramming Compile time code optimization 给出了 for 循环的编译时展开 我想知道我们是否可以
  • 在 WPF 中使用 ReactiveUI 提供长时间运行命令反馈的正确方法

    我有一个 C WPF NET 4 5 应用程序 用户将用它来打开某些文件 然后 应用程序将经历很多动作 读取文件 通过许多插件和解析器传递它 这些文件可能相当大 gt 100MB 因此这可能需要一段时间 我想让用户了解 UI 中发生的情况
  • C++ 继承的内存布局

    如果我有两个类 一个类继承另一个类 并且子类仅包含函数 那么这两个类的内存布局是否相同 e g class Base int a b c class Derived public Base only functions 我读过编译器无法对数
  • 对于某些 PDF 文件,LoadIFilter() 返回 -2147467259

    我正在尝试使用 Adob e IFilter 搜索 PDF 文件 我的代码是用 C 编写的 我使用 p invoke 来获取 IFilter 的实例 DllImport query dll SetLastError true CharSet
  • C# 中最小化字符串长度

    我想减少字符串的长度 喜欢 这串 string foo Lorem ipsum dolor sit amet consectetur adipiscing elit Aenean in vehicula nulla Phasellus li
  • C# 使用“?” if else 语句设置值这叫什么

    嘿 我刚刚看到以下声明 return name null name NA 我只是想知道这在 NET 中叫什么 是吗 代表即然后执行此操作 这是一个俗称的 条件运算符 三元运算符 http en wikipedia org wiki Tern
  • 指针和内存范围

    我已经用 C 语言编程有一段时间了 但对 C 语言还是很陌生 有时我对 C 处理内存的方式感到困惑 考虑以下有效的 C 代码片段 const char string void where is this pointer variable l
  • 从 mvc 控制器使用 Web api 控制器操作

    我有两个控制器 一个mvc控制器和一个api控制器 它们都在同一个项目中 HomeController Controller DataController ApiController 如果我想从 HomeController 中使用 Dat

随机推荐

  • 体系结构 x86_64 的未定义符号:OS X、Boost Log、CMake

    链接的另一个问题 操作系统 X 10 9 5 x64 跨平台项目正在使用 CMake 工具链构建 使用升压 将所有可执行文件与 Boost Log 链接时出错 确实找到了 BOOST PATH 单元测试与 Boost Test 成功链接 第
  • Java - 通过浏览器/URL 连接到 ServerSocket

    我正在编写一个软件 并且受到无法使用套接字连接到使用 ServerSocket 的 java 应用程序的限制 我想我应该尝试使用 URL 连接 因为可以定义要连接到的端口 e g 127 0 0 1 62666 我的服务器应用程序正在侦听连
  • 无法建立到服务器的链接

    一个非常简单的插入函数 它给出了一些令人讨厌的错误 Like Warning mysql query Access denied for user one com using password NO in customers be be h
  • 编写一个程序来抓取论坛

    我需要编写一个程序来抓取论坛 我应该使用 Scrapy 框架用 Python 编写程序还是应该使用 Php cURL 还有与 Scrapy 相当的 Php 吗 Thanks 我会选择 Python 因为它具有卓越的 libxml2 绑定 特
  • 如何从unix连接到oracle数据库

    我正在尝试从我的 UNIX 机器连接到 Oracle 数据库 总的来说 我对剧本写作很陌生 我知道如何浏览unix并编写了基本脚本 读取 显示 并使用bash命令执行它们 我还知道如何查看 unix 中的变量 用户和系统 你能告诉我需要做什
  • Django 多项选择字段/复选框选择多个

    我有一个 Django 应用程序 想要在用户的个人资料中显示多项选择复选框 然后他们将能够选择多个项目 这是我的 models py 的简化版本 from profiles choices import SAMPLE CHOICES cla
  • 根据局部最小值/最大值计算累积增长/下降

    我正在学习 R 及其通过 quantmod lib 在交易任务中的应用 并定期浏览社区 从这里获得很多新知识和技巧 我对 R 的总体印象 特别是 quantmod lib 它非常棒 此时我需要经验丰富的 R 用户的帮助 我正在使用通过 ge
  • 如何将结构化数据保存到文件?

    我的程序包含大量具有恒定输出的预计算 我想避免在下次运行程序时运行此预计算 因此 我想在程序第一次运行时将其输出保存到文件中 并在下次运行程序时加载它 输出包含我自己定义的非常见数据类型 对象和结构 我该如何去做呢 反 序列化 Rust 对
  • 为什么我的测试没有在 Odoo 10 上运行?

    我创建了一个简单的待办事项应用程序 其结构如下 todo app init py manifest py tests init py tests todo py todo model py 在测试文件夹下我有 测试 init py codi
  • 如何向 Chart.js 添加数据集切换?

    我正在使用 Chart js 创建折线图 我想要四个不同的数据集 默认情况下它们都是可见的 但可以通过单击按钮来打开和关闭 如何才能实现这一目标 我似乎无法在文档中找到答案 addData removeData and update all
  • 如何检查命令是否可以执行?

    我有一个脚本将向用户显示一些压缩选项 gzip zip 和 bzip 由于这意味着可以在 Linux 和 Windows 上运行 并且 Zlib 库不可用 因此我必须使用 Unix 命令来压缩任何文件 当然 如果脚本在 Windows 上运
  • 如何使用 T-SQL 进行数据透视表?

    如何从此格式转换表格 Id Data Section 1 1AAA AAA 1 1BBB BBB 1 1CCC CCC 2 2AAA AAA 2
  • Python Websockets 无法通过互联网连接

    我只是想通过互联网获得一个非常基本的 websocket 连接 该代码看起来不错 因为它在连接到本地主机时有效 但由于某种原因 当我尝试通过互联网使用它时失败了 我正在使用网络套接字 https pypi python org pypi w
  • 为 pyspark 设置 SparkContext

    我是新手spark and pyspark 如果有人解释一下到底是什么 我将不胜感激SparkContext参数做什么 我该如何设置spark context对于Python应用程序 See here http spark apache o
  • 为什么 Cloneable 没有被弃用?

    人们普遍认为CloneableJava 中的接口已损坏 造成这种情况的原因有很多 我就不一一列举了 others http howtodoinjava com 2012 11 10 cloneable interface is broken
  • 将生成器对象转换为列表以进行调试[重复]

    这个问题在这里已经有答案了 当我使用 IPython 在 Python 中进行调试时 有时会遇到断点 并且我想检查当前是生成器的变量 我能想到的最简单的方法是将其转换为列表 但我不清楚在一行中执行此操作的简单方法是什么ipdb 因为我对 P
  • 如何使用 matplotlib 为烛台的灯芯部分着色?

    我正在寻找使用 matplotlib 将烛台的灯芯部分变成黑色 我在文档中找不到任何提及它的内容 但我已经看到图片示例表明它可以完成 这是我目前拥有的 这是灯芯被涂成黑色的示例 Update 我使用了下面提供的解决方案 但稍微更改了代码以删
  • 将 iOS (iPhone) 应用程序移植到 Mac?

    有没有首选的方法来解决这个问题 有问题的应用程序不太大 我花了几个月的时间编写的单人游戏 编辑 我应该补充一点 我没有 mac 开发经验 除了作为一名 iOS 开发人员自然而然的事情之外 编辑 游戏中大量使用的类 NSObject UIVi
  • 为注释字段设置默认空值时出错

    为什么我收到错误 属性值必须是常量 不是null持续的 Target ElementType TYPE Retention RetentionPolicy RUNTIME public interface SomeInterface Cla
  • 尝试从 matlab 调用 dll 函数导致崩溃

    我尝试在 MATLAB 中使用第三方外部 DLL 来自 usbmicro 但它总是使 MATLAB 崩溃 这是来自说明 C 程序中函数调用语法的文档 int USBm About char about 我尝试了这个 MATLAB 脚本 是的