- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在用 C# 创建一个 Windows 窗体应用程序,其中需要在运行时通过文件对话框在应用程序中调用用 C 编写的具有自己的结构和函数的不同 .dll 文件以获得它们的路径。据我了解,这意味着我无法使用 [DllImport("filename")]
,因为路径值在编译时尚未确定。我至少得到了 .h 文件,并且还保证导出的函数具有相同的名称。
我在网上搜索了很多不同的帖子,大多数解决方案最终都指向 PInvoke、Reflection 和/或 InteropServices,但没有明确的示例说明该怎么做。
由于每个这样的 dll 定义了自己的结构,我希望能够:1. 使用 dll 本身的结构定义以避免在 C# 中重新定义所有内容,2. 加载dll后,在应用程序的不同点使用函数。
诚然,我对 C# 很陌生,所以我上面尝试做的事情可能根本不可能。但我现在很茫然,因为我找不到从哪里开始学习来解决这个问题。任何帮助将不胜感激。
引用代码如下:
dll 的头文件之一 classifier.h:
#ifndef SRC_CLASSIFIER_H_
#define SRC_CLASSIFIER_H_
#include "vector.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef BUILD_DYNAMIC_LIB
#ifdef _WIN32
#define _EXPORT __declspec(dllexport)
#else
#define _EXPORT __attribute__((visibility("default")))
#endif
#else
#define _EXPORT
#endif
/** Error codes */
#define CLASSIFIER_OK 0x00
#define CLASSIFIER_INIT_MISSING 0x01
#define CLASIFIER_NO_SAMPLES_PROVIDED 0x02
#define CLASSIFIER_PARAM_OUT_OF_RANGE 0x03
#define CLASSIFIER_DECISION_BUFFER_LEN 20
#define CLASSIFIER_MAX_BURST_LEN 32
typedef struct {
uint8_t sensitivity;
uint8_t noTruckRecognitionLimit;
uint8_t noTruckRecognitionHyst;
uint16_t intensityLimit[CLASSIFIER_MAX_BURST_LEN];
} classifier_Parameter_t;
typedef enum { UNDEFINED = 0x00, TRUCK = 0x01, NO_TRUCK = 0x02 } class_t;
typedef struct {
class_t classifiedAs;
} classifier_Result_t;
typedef struct {
uint8_t noOfSamples;
vec3d_t accSamples[CLASSIFIER_MAX_BURST_LEN];
} classifier_Input_t;
_EXPORT uint8_t classifier_api_Init(classifier_Parameter_t *para);
_EXPORT uint8_t classifier_api_Execute(classifier_Input_t *input,
classifier_Result_t *result);
_EXPORT uint8_t classifier_api_Reset(void);
#ifdef __cplusplus
}
#endif
#endif /* SRC_CLASSIFIER_H_ */
以及 C# 文件 MainForm.cs:
using System;
...
namespace ClassifierEval
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void btnLoadDll_Click(object sender, EventArgs e)
{
openFileDialogLoadDll.ShowDialog();
}
private void openFileDialogLoadDll_FileOk(object sender, CancelEventArgs e)
{
lblLoadedDll.Text = openFileDialogLoadDll.FileName;
///Loading the DLL here?
}
...
}
最佳答案
在回答你的第一个问题时,我不知道有什么方法可以直接从 dll 获取结构定义。 (尝试 LoadLibrary、GetProcAdres),但为调试而构建的可能异常(exception)。如果您知道将在 C 中使用的结构,那么它应该非常简单定义在 C# 中使用的结构 - pinvoke.net 等网站上有很多示例。
例如C中的这个函数声明:
__declspec(dllexport) int scrypt(const uint8_t * passwd, size_t passwdlen,
const uint8_t * salt, size_t saltlen, uint64_t N, uint32_t r,
uint32_t p,uint8_t * buf, size_t buflen);
可以转化为:
[DllImport("SCrypt.dll", CharSet = CharSet.Unicode,
CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
public static extern int scrypt(byte[] passwd, uint passwdlen, byte[] salt, uint saltlen,
ulong N, uint r, uint p, byte[] buf, uint buflen);
其次,您可以在使用程序之前尝试将 dll 复制到已知位置(例如执行目录)。使用适当的 dll 编译代码然后就可以工作了。当然,这种技术假设你知道你要使用的 dll提前引用。
关于c# - 在C#中动态调用不同的C DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59079363/
我有一个 Windows Service(一个发布版本),我替换了一个在 Debug模式下构建的 DLL 并尝试启动服务。它引发了错误无法加载文件或程序集“名称”或其依赖项之一。尝试加载格式不正确的程
我使用 Microsoft Visual Basic 6.0 Enterprise Edition SP6 开发了一个 VB6 应用程序。 我已将可执行文件复制到未安装 VB6 的计算机 C。 我已在
我有很多小的 DLL,我想将它们组合成一个大的(更)DLL(如 suggested here)。我可以通过合并我的项目来做到这一点,但我想要一种不那么干扰的方式。 可以将多个 DLL 合并为一个单元吗
我想将非托管 DLL 和图像文件与托管 DLL 合并。我该怎么做?可能吗? 最佳答案 很有可能。这可以通过 bxilmerge 来完成此解决方案可以将非托管 DLL 和图像或其他文件与托管 DLL 合
如何在Windows Server 2003上安装msvcr71.dll,这是我的软件所需的。我真的不想将此dll复制到system32文件夹,因为它可能会破坏此目标系统。 最佳答案 只需将其复制到程
我最近遇到了一个安装在我的系统上的 DLL,Dependancy Walker(以及我尝试过的所有其他实用程序)说按名称或顺序导出为零,但文件大小约为 4mb。我认为 DLL 的唯一目的是导出供其他代
我终于让我的本地主机在本地显示该站点。一切似乎都在朝着这个方向努力。我的下一步是当网站使用 ActiveX.dll 中的函数时,实际上能够从 VB6 IDE 进入代码 更新: 我更新了代码并删除了我在
首先,我指的是Windows环境和VC++编译器。 我想要做的是重建一个Vc++ dll并与已经链接到该lib的exe保持兼容性,而不必重建该exe或使用LoadLibrary动态加载该dll。换句话
我以前这样做过,我不记得我是从网上下载的 DLL 还是其他东西,但我不想感染病毒。我需要访问这个命名空间,以便我可以拥有 Webbrowswer 控件不提供的额外功能。 如何准确添加 Com 引用。还
我正在尝试为依赖于 ghostscript 的库创建 Nuget 包,因此引用 gsdll32.dll - 一个非托管库。我不能只包含一个标准的 dll 引用。我应该把它放在 nuget 目录结构中的
如果我有 Windows 可执行文件,我如何找出它将加载哪些 dll? 我只是谈论哪些将静态加载,而不是那些可能使用 LoadLibrary 等动态加载的内容。 最佳答案 dumpbin是VC++自带
这可能是一个非常菜鸟的问题,但在当今的网络应用程序开发世界中,许多程序员不需要过多处理 dll,因此也懒得去了解它们的用途。 那么什么是dll? 它有什么用? 它是如何工作的? 如何创建一个? 在什么
所以我刚刚开始使用 OpenTK,并将此代码放在一个继承 GameWindow 类的类中: protected override void OnRenderFrame(FrameEventArgs e
如何调试未由 java 应用程序加载的 dll。 场景是这样的:我的 java 应用正在加载正在使用另一个 dll 的 jni.dll,而那个 dll 正在使用另一个 dll。 javajni.dll
我将“更好”放在引号中,因为这是一个定性问题。几年来我一直在编写 COM DLL,直到最近才发现并成功使用了带有 Typelib 概念的标准 DLL。 使用 COM DLL 代替 DLL+Typeli
这种用户机器可能没有msvcp100.dll、msvcp100.dll之类dll的情况怎么处理?我不希望我的软件因为这种错误而无法安装在用户的机器上。我一直在考虑要么找到一个工具并将每个需要的 dll
在 VS2012 中使用 C# .NET 和 COM 互操作,我正在开发一个用于其他几个程序的公共(public)库。为了简化集成,我想将整个库缩减为一个 DLL。该库的特点之一是本地化。它有包含多种
我在 .Net 中使用 FieldTalk Modbus。当我运行应用程序时,我在为 MbusTcpMasterProtocol 创建对象时遇到错误。 MbusTcpMasterProtocol mb
我想指出,我知道如何向/从 GAC 添加/删除程序集。我要问的是,是否有人可以从技术角度向我解释它是如何工作的。将 dll 放在那里有什么意义 - 从节省资源的角度来看。 亲切的问候 最佳答案 将东西
我继承了一个传统的经典 ASP 应用程序,它使用 VB6 ActiveX DLL 来执行业务逻辑。 我想跟踪加载和卸载 DLL 的点。有没有办法在 VB6 DLL 中拦截这些事件? 在相关说明中,Cl
我是一名优秀的程序员,十分优秀!