c#com对象无法获取ref对象值

本文关键字:对象 ref 获取 c#com | 更新日期: 2023-09-27 17:58:31

我们试图获取COM对象返回的值,函数是:

bool AxSB100BPC.GetEnrollData(int dwMachineNumber,
int dwEnrollNumber,
int dwEMachineNumber,
int dwBackupNumber,
ref int dwMachinePrivilege,
ref object dwEnrollData,
ref int dwPassWord

我们将此函数称为:

vRet = axSB100BPC1.GetEnrollData(mMachineNumber,
                                 vEnrollNumber,
                                 vEMachineNumber,
                                 vFingerNumber,
                                 ref vPrivilege,
                                 ref oglngEnrollData,
                                 ref glngEnrollPData);

问题在于oglngEnrollData,它是ref object的类型。它被声明为object oglngEnrollData;

错误为:

System.Runtime.InteropServices.COMException未处理HResult=-2147352571消息=Tür uyuşmazlığı。(HRESULT出现异常:0x80020005(DISP_E_TYPEMIMACH))来源=mscorlib错误代码=-2147352571StackTrace:位于System.RuntimeType.ForwardCallToInvokeMember(字符串成员名称、BindingFlags标志、对象目标、Int32[]aWrapperTypes、MessageData和msgData)在SB100BPCLib_DSB100BC.GetEnrollData(Int32 dwMachineNumber、Int32 dwEnrollNumber、Int32dwEMachineNumber、Int32-dwBackupNumber、Int32_&dwMachinePrivilege、Object&dwEnrollData、Int32&dwPassWord)在AxSB100BPCLib.AxSB100BPC.GetEnrollData(Int32 dwMachineNumber、Int32 dwEnrollNumber、Int32dwEMachineNumber、Int32-dwBackupNumber、Int32''dwMachinePrivilege、Object&dwEnrollData、Int32&dwPassWord)在enrolldata。f:''enrolldata''Form1.cs:line 196中的Form1.button_2_Click(Object sender,EventArgs e)位于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(消息和消息,鼠标按钮,Int32次点击)位于System.Windows.Forms.Control.WndProc(消息和m)位于System.Windows.Forms.ButtonBase.WndProc(消息和m)位于System.Windows.Forms.Button.WndProc(消息和m)位于System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息-m)位于System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息-m)在System.Windows.Forms.NativeWindow.DebugableCallback(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原因,ApplicationContext上下文)位于System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32原因,ApplicationContext上下文)位于System.Windows.Forms.Application.Run(Form mainForm)在enrolldata。f:''enrolldata''Program.cs:line 19中的Program.Main()位于System.AppDomain_nExecuteAssembly(RuntimeAssembly程序集,String[]参数)位于System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String[]args)位于Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()位于System.Threading.ThreadHelper.ThreadStart_Context(对象状态)位于System.Threading.ExecutionContext.RunInternal(ExecutionContext ExecutionContext,ContextCallback回调,对象状态,布尔值preserveSyncCtx)在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,Object state,Boolean preserveSyncCtx)在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态)位于System.Threading.ThreadHelper.ThreadStart()InnerException:

任何想法都欢迎

c#com对象无法获取ref对象值

另一个论坛上的这个问题似乎在处理相同接口的相同问题。

这只是基于该线程的猜测,但问题似乎是dwEnrollData参数实际上是一种变体类型(而不是托管的System.Object类型),在这种情况下,您应该尝试使用System.Runtime.InteropServices.VariantWrapper来包装传递到方法中的参数值。