各位:
厂家提供了一个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:
厂家提供了一个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:
解决方案 »
- 谁能给出一个完整的在IIS中承载WCF服务并且可实现跨计算机(相同工作组)运行且能向客户端返回数据集的例子
- DataReceived事件只触发一次!急!急!急!
- 如何用winforms父窗体中添加TreeView管理子窗体
- 高手帮忙看下如何截取文件名
- 在C#中引用外部的DLL(也是用C#编写的),使用中为何不能出现注释???
- 怎样将uint类型的数据转换成网络字节顺序的二进制数组?
- 谁能帮我看看这段代码!什么地方不对阿!??
- C#中用Findwindow函数时,lpWindowName传入窗口标题,那lpClassName传入何值呢?
- 历史遗留问题收集(2):请将你的没有解决但是还想解决的问题贴在这儿,请各路高手关注讨论啊。
- 关于活动的解释!
- access和ftp的一个问题
- .net B/S 登陆时 显示 (正在登陆) 数秒后 再登陆(求助)
另外,如果你坚持使用反射,可以试试这样是否可以:
oType.InvokeMember("Login", System.Reflection.BindingFlags.InvokeMethod|BindingFlags.Public|BindingFlags.Instance, null, o, parts);
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#里面都可以看到类的方法了,不过暂时没测试是否没这个异常了……