各位:
     厂家提供了一个dll,需要用regsvr32先注册,然后再调用。我按照说明,先用regsvr32注册了dll,用VB的话调用没有问题,但用c#调用就报异常。
具体如下:接口方法说明:
1、Login(网络服务器, 数据库名字, admin的密码, 返回的错误信息)
作用:登陆系统,调用其它方法前必须先调用它
返回值:0 - 成功,1 - 密码不正确,2 - 人员不存在VB代码(测试通过):Sub test()
  Dim obj As Object
  Dim sErr As String
  Set obj = CreateObject("OperatorManager.OperManager", sErr)
  s = obj.Login("127.0.0.1", "test2", "admin", sErr)
  MsgBox s
  MsgBox sErr
  Set obj = Nothing
End Subc#代码(报异常):运行到oType.InvokeMember("Login", System.Reflection.BindingFlags.InvokeMethod, null, o, parts);
时报异常"调用的目标发生了异常" System.Type oType = System.Type.GetTypeFromProgID("OperatorManager.OperManager");
            
            object o = System.Activator.CreateInstance(oType);             string[] parts = new string[4];
             parts[0] = "127.0.0.1";
             parts[1] = "test2";
             parts[2] = "admin";             oType.InvokeMember("Login", System.Reflection.BindingFlags.InvokeMethod, null, o, parts);
             MessageBox.Show(parts[3]);
异常详细信息为:
未处理 System.Reflection.TargetInvocationException
  Message=调用的目标发生了异常。
  Source=mscorlib
  StackTrace:
       在 System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
       在 System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
       在 System.Type.InvokeMember(String name, BindingFlags invokeAttr, Binder binder, Object target, Object[] args)
       在 TestService.Form3.button1_Click(Object sender, EventArgs e) 位置 E:\TestService\Form3.cs:行号 62
       在 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(IntPtr 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)
       在 TestService.Program.Main() 位置 E:\TestService\Program.cs:行号 18
       在 System.AppDomain._nExecuteAssembly(RuntimeAssembly 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, Boolean ignoreSyncCtx)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.Runtime.InteropServices.COMException
       Message=灾难性故障 (异常来自 HRESULT:0x8000FFFF (E_UNEXPECTED))
       Source=""
       ErrorCode=-2147418113
       InnerException: 

解决方案 »

  1.   

    直接在工程中插入引用dll试试,打包的时候,报靶这个dll文件一块打包到目录下
      

  2.   

    VB和C#测试时是同一台机器(环境)吗?都是32位系统?
      

  3.   

    用regsvr32注册之后,你引用到工程里面没有?如果这com组件不依赖硬件,你可以发给我,帮你看看:[email protected]
      

  4.   

    com组件可以直接加入项目引用,然后就可以按早期bind方式直接实例化并调用了,没必要使用反射啊,除非有特别的要求.
    另外,如果你坚持使用反射,可以试试这样是否可以:
    oType.InvokeMember("Login", System.Reflection.BindingFlags.InvokeMethod|BindingFlags.Public|BindingFlags.Instance, null, o, parts);
      

  5.   

    我觉得最好是加入Com 引用,然后使用vs生成的代理类。
      

  6.   

    最近,我也遇到这个问题……调用异常:
    System.InvalidCastException: 无法将类型为“Dnb3.ClassCkyClass”的 COM 对象强制转换为接口类型“Dnb3._ClassCky”。此操作失败的原因是对 IID 为“{5811805B-18DE-41BD-8265-3E0A1F390FC3}”的接口的 COM 组件调用 QueryInterface 因以下错误而失败: 不支持此接口 (异常来自 HRESULT:0x80004002 (E_NOINTERFACE))。
       在 Dnb3.ClassCkyClass.set_Commport(Int16 Commport)
    但是这个参数 在VB里面是 Integer,在C#里面就是short ,我也是传的short,肯定没问题的,但是库里类的某些方法在C#里看不到,后来我用了 vs自带的 tlbimp.exe 程序 转换了下dll文件,命令如下: tlbimp TestLib.dll /transform:dispret /out:myTestLib.dll   这样C#里面都可以看到类的方法了,不过暂时没测试是否没这个异常了……