当前位置:编程学习 > C#/ASP.NET >>

c# 调用dll问题 急急急

我调用电网给的TestZhuzhan.dll

 /// <summary>
        /// 身份认证函数  作者:龙亿
        /// </summary>
        /// <param name="div">8 字节分散因子,16 进制字符串</param>
        /// <param name="RandAndEndata">8 字节随机数+8 字节密文。</param>
        /// <returns>0,成功;200,连接加密机失败;201,取随机数1 失败;202,取随机数2 失败;203,密钥分散失败;204,数据加密失败;205,取密文失败;</returns>
        [DllImport("TestZhuzhan.dll")]
        public static extern int IdentityAuthentication(string div, byte[] RandAndEndata);

运行报错
未处理 System.DllNotFoundException
  Message="无法加载 DLL“TestZhuzhan.dll”: 找不到指定的模块。 (异常来自 HRESULT:0x8007007E)。"
  Source="cpuCardOper"
  TypeName=""
  StackTrace:
       在 cpuCardOper.Form1.IdentityAuthentication(String Div, Byte[] RandAndEndata)
       在 cpuCardOper.Form1.button1_Click(Object sender, EventArgs e) 位置 E:\c#\c#_wzy\usb_test\cpuCardOper\cpuCardOper\Form1.cs:行号 169
       在 System.Windows.Forms.Control.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
       在 System.Windows.Forms.Button.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.Run(Form mainForm)
       在 cpuCardOper.Program.Main() 位置 E:\c#\c#_wzy\usb_test\cpuCardOper\cpuCardOper\Program.cs:行号 17
       在 System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
--------------------编程问答-------------------- 检查下指定路径下 DLL是否真实存在! --------------------编程问答-------------------- 贴出API原函数! --------------------编程问答-------------------- --------------------编程问答-------------------- 提示无法加载该Dll模块了,你得检查dll路径是否存在,正确。 --------------------编程问答--------------------
引用 1 楼 zjx198934 的回复:
检查下指定路径下 DLL是否真实存在!

贴出API原函数! --------------------编程问答-------------------- 检查下指定debug 下 DLL是否真实存在!

未处理 System.DllNotFoundException
位置 E:\c#\c#_wzy\usb_test\cpuCardOper\cpuCardOper\Form1.cs:行号 169  --------------------编程问答-------------------- 重新添加,
清理项目
再重新生成,
再调用. --------------------编程问答-------------------- --------------------编程问答-------------------- 这个你要看一下,有些dll 要放在程序的bin目录下,有些要放到指定的目录下。我以前也遇到过。祝你好运。 --------------------编程问答-------------------- 电科院给的DLL  没有原型函数 
TestZhuzhan.dll 我放在system32 和 bin中都不行  --------------------编程问答-------------------- 1. 身份认证函数
函数名称 IdentityAuthentication(char *Div,char *RandAndEndata)
函数功能身份认证取随机数和密文
Div,输入参数,8 字节分散因子,16 进制字符串。
参数说明 RandAndEndata,输出参数,字符型,8 字节随机数+8 字节
密文。
成功标志 0,成功;
200,连接加密机失败;
201,取随机数1 失败;
202,取随机数2 失败;
203,密钥分散失败;
204,数据加密失败;
205,取密文失败;

只有这个说明 --------------------编程问答--------------------

 [DllImport()]
 public static extern int IdentityAuthentication(String Div,String RandAndEndata);

 把dll放在debug文件里。
--------------------编程问答-------------------- 硬件应该有测试程序吧  你先用测试程序试试 能在你机器上跑起来不!
一般动态库 拷贝在项目debug 下或者 system32 下 ,你看下买硬件的文档   --------------------编程问答-------------------- TestZhuzhan.dll这个dll是否需要注册,你放到system32下面然后运行regsvr32 TestZhuzhan.dll对TestZhuzhan.dll进行注册看是否还出现异常. --------------------编程问答-------------------- 运行注册命令 提示 loadlibrary("TestZhuzhuan.dll")失败 找不到指定的模块 --------------------编程问答--------------------
引用 15 楼 appleandwzy 的回复:
运行注册命令 提示 loadlibrary("TestZhuzhuan.dll")失败 找不到指定的模块

你把TestZhuzhuan.dll放在D盘下 然后loadlibrary("D://TestZhuzhuan.dll")

我正在使用《Csdn收音机》第一时间获取最新动态! --------------------编程问答-------------------- 提示 windows找不到文件
论坛能上传文件吗? 我把TestZhuzhan.dll 发上去  兄弟们帮我试一下
--------------------编程问答-------------------- 估计是没有引用 --------------------编程问答-------------------- [DllImport()]
 public static extern int IdentityAuthentication(String Div,String RandAndEndata);

这种方式需要引用吗? --------------------编程问答-------------------- 我曾经也遇到过相同的问题,最后找出是因为DLL存放的路径问题,建议你把DLL文件放到项目生成文件夹下,如果不是Debug或许指定了其他目录,也有可能是Release --------------------编程问答-------------------- 把dll放在debug文件夹下,或者在程序里写清楚具体的dll路径,重新生成以下就可以了 --------------------编程问答-------------------- //Delphi Project2.dll位于debug文件下  属性EntryPoint表明连接dll中的入口函数  
[DllImport("Project2.dll", EntryPoint = "Add1")] 
 public static extern int Add(int x, int y); //Add为导出的函数名

在你需要调用dll的地方 加上以上两句代码就行了。。 通常dll文件位于工程文件的debug文件下
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,