c# 调用自定义的用c实现的dll,先是显示未能添加对dll的引用 请确保此文件可访问并且是一个有效的程序集或com组件

yy_9117 2019-04-09 04:38:25
如题,
1.我把我自己写的dll放入工程的bin/debug下,
2.然后右键引用,选择浏览框然后然后添加了这个dll,
报错显示
调用自定义的用c实现的dll,先是显示未能添加对dll的引用 请确保此文件可访问并且是一个有效的程序集或com组件,
3.然后我就就去注册该Dll
regsvr32 D:\Program Files\c-source-code\WordAddIn1\WordAddIn1\bin\Debug\DllTest.dll
4.注册报错


请问怎么解决?
...全文
3808 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yy_9117 2019-04-09
  • 打赏
  • 举报
回复
引用 4 楼 stherix 的回复:
[quote=引用 3 楼 yy_9117 的回复:] [quote=引用 1 楼 stherix 的回复:] .net只能引用托管的dll 非托管的native dll,可以通过两种办法 1是用DllImport导入其中的函数进行调用 2是用c++/cli写个wrapper,当然如果没有源码,还不如用第一种方法
我在工程代码里写了 [DllImport(@"DllTest.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "printHello")] public extern unsafe static void printHello(); [DllImport(@"DllTest.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "EncryptBuffer")] public extern unsafe static int EncryptBuffer(ref byte result, UInt32 len, string key); [DllImport(@"DllTest.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "DecryptBuffer")] public extern unsafe static int DecryptBuffer(ref byte result, UInt32 len, string key);[/quote] 这样就可以了 不需要在项目里添加引用,只要放在和你的程序同一文件夹就可以了[/quote] ok了
yy_9117 2019-04-09
  • 打赏
  • 举报
回复
引用 4 楼 stherix 的回复:
[quote=引用 3 楼 yy_9117 的回复:] [quote=引用 1 楼 stherix 的回复:] .net只能引用托管的dll 非托管的native dll,可以通过两种办法 1是用DllImport导入其中的函数进行调用 2是用c++/cli写个wrapper,当然如果没有源码,还不如用第一种方法
我在工程代码里写了 [DllImport(@"DllTest.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "printHello")] public extern unsafe static void printHello(); [DllImport(@"DllTest.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "EncryptBuffer")] public extern unsafe static int EncryptBuffer(ref byte result, UInt32 len, string key); [DllImport(@"DllTest.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "DecryptBuffer")] public extern unsafe static int DecryptBuffer(ref byte result, UInt32 len, string key);[/quote] 这样就可以了 不需要在项目里添加引用,只要放在和你的程序同一文件夹就可以了[/quote] 我试一试
yy_9117 2019-04-09
  • 打赏
  • 举报
回复
引用 2 楼 第27个Bug 的回复:
C#调用C或者C++是通过DllImport的 可以参考微软示例: https://docs.microsoft.com/zh-cn/dotnet/api/system.runtime.interopservices.dllimportattribute?redirectedfrom=MSDN&view=netframework-4.7.2
我有声明导入的函数, 在Console的工程项目下可以添加该引用 但是我这个是VSTO的外接程序的工程里添加就报错了上面的错
stherix 2019-04-09
  • 打赏
  • 举报
回复
引用 3 楼 yy_9117 的回复:
[quote=引用 1 楼 stherix 的回复:] .net只能引用托管的dll 非托管的native dll,可以通过两种办法 1是用DllImport导入其中的函数进行调用 2是用c++/cli写个wrapper,当然如果没有源码,还不如用第一种方法
我在工程代码里写了 [DllImport(@"DllTest.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "printHello")] public extern unsafe static void printHello(); [DllImport(@"DllTest.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "EncryptBuffer")] public extern unsafe static int EncryptBuffer(ref byte result, UInt32 len, string key); [DllImport(@"DllTest.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "DecryptBuffer")] public extern unsafe static int DecryptBuffer(ref byte result, UInt32 len, string key);[/quote] 这样就可以了 不需要在项目里添加引用,只要放在和你的程序同一文件夹就可以了
yy_9117 2019-04-09
  • 打赏
  • 举报
回复
引用 1 楼 stherix 的回复:
.net只能引用托管的dll 非托管的native dll,可以通过两种办法 1是用DllImport导入其中的函数进行调用 2是用c++/cli写个wrapper,当然如果没有源码,还不如用第一种方法
我在工程代码里写了 [DllImport(@"DllTest.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "printHello")] public extern unsafe static void printHello(); [DllImport(@"DllTest.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "EncryptBuffer")] public extern unsafe static int EncryptBuffer(ref byte result, UInt32 len, string key); [DllImport(@"DllTest.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "DecryptBuffer")] public extern unsafe static int DecryptBuffer(ref byte result, UInt32 len, string key);
stherix 2019-04-09
  • 打赏
  • 举报
回复
.net只能引用托管的dll 非托管的native dll,可以通过两种办法 1是用DllImport导入其中的函数进行调用 2是用c++/cli写个wrapper,当然如果没有源码,还不如用第一种方法

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧