将 DLL 集成到 .Net 项目中
Integrate DLL into .Net project
我正在尝试将 DLL 文件导入到我的项目中,我已经尝试过 'Add reference' 但没有成功:
'a reference to *.dll could not be added. please make sure that the
file is accessible, and that it is a valid assembly or com component'
我尝试使用 'regsvr32' 注册 dll 但没有成功:
'the module *.dll was loaded but the entry-point DllRegisterServer was
not found'
最后我使用了 DllImport,(我目前正在使用 libclamav.dll 进行测试)
lImport("libclamav.dll")]
public static extern int cl_scanfile(String path);
private void button1_Click(object sender, EventArgs e)
{
string path="e:\scan\111.jpg";
int n;
n = cl_scanfile(path);
}
但是我在执行时出错:
'An attempt was made to load a program with an incorrect format.
(Exception from HRESULT: 0x8007000B)'
有什么建议吗??
听起来您想 p/Invoke 非托管 DLL 中的方法。您应该通读 Microsoft 提供的 Platform Invoke Tutorial,看看是否有帮助。
- 您通常只添加对其他托管 .NET 程序集的引用。如果您尝试使用的 DLL 不是托管 DLL,这可能不是您要查找的内容。
- 如果您打算使用 COM API,通常只使用 regsvr32 注册 DLL。如果您尝试使用的 DLL 不是 COM DLL,这可能不是您要查找的内容。
- 如果您尝试使用非托管/非 COM dll 并希望直接调用其中的非托管函数,您 p/Invoke。根据我对 libclamav.dll 的快速 Google,这可能就是您要尝试做的事情。
如果您收到格式不正确的异常,这可能是因为您的 .NET 应用程序以 'Any CPU' 为目标,但您尝试加载的 DLL 是 32 位 DLL(并且您在64 位机器)。如果是这种情况,您可能希望将 .NET 应用程序的 'Platform Target' 设置为 'x86' 并查看是否有帮助。
要成功 p/Invoke,您还需要在搜索路径中的某处包含要调用的 DLL(及其所有依赖项)。确保这一点的最简单方法是将 DLL 和所有依赖项复制到 .NET 应用程序的 bin\Debug 或 bin\Release 文件夹中,看看是否有帮助。
我正在尝试将 DLL 文件导入到我的项目中,我已经尝试过 'Add reference' 但没有成功:
'a reference to *.dll could not be added. please make sure that the file is accessible, and that it is a valid assembly or com component'
我尝试使用 'regsvr32' 注册 dll 但没有成功:
'the module *.dll was loaded but the entry-point DllRegisterServer was not found'
最后我使用了 DllImport,(我目前正在使用 libclamav.dll 进行测试)
lImport("libclamav.dll")]
public static extern int cl_scanfile(String path);
private void button1_Click(object sender, EventArgs e)
{
string path="e:\scan\111.jpg";
int n;
n = cl_scanfile(path);
}
但是我在执行时出错:
'An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)'
有什么建议吗??
听起来您想 p/Invoke 非托管 DLL 中的方法。您应该通读 Microsoft 提供的 Platform Invoke Tutorial,看看是否有帮助。
- 您通常只添加对其他托管 .NET 程序集的引用。如果您尝试使用的 DLL 不是托管 DLL,这可能不是您要查找的内容。
- 如果您打算使用 COM API,通常只使用 regsvr32 注册 DLL。如果您尝试使用的 DLL 不是 COM DLL,这可能不是您要查找的内容。
- 如果您尝试使用非托管/非 COM dll 并希望直接调用其中的非托管函数,您 p/Invoke。根据我对 libclamav.dll 的快速 Google,这可能就是您要尝试做的事情。
如果您收到格式不正确的异常,这可能是因为您的 .NET 应用程序以 'Any CPU' 为目标,但您尝试加载的 DLL 是 32 位 DLL(并且您在64 位机器)。如果是这种情况,您可能希望将 .NET 应用程序的 'Platform Target' 设置为 'x86' 并查看是否有帮助。
要成功 p/Invoke,您还需要在搜索路径中的某处包含要调用的 DLL(及其所有依赖项)。确保这一点的最简单方法是将 DLL 和所有依赖项复制到 .NET 应用程序的 bin\Debug 或 bin\Release 文件夹中,看看是否有帮助。