none
c#调用进程外Com组件Intptr参数问题 RRS feed

  • 问题

  • 1.最近在解决64位应用程序调用32位dll的问题,想使用进程外Com来解决,编写了相应的Demo,当接口方法的参数是string或int时程序都能正常运行,但接口方法的参数含有IntPtr类型时程序报错
    2.接口方法: public void TestPtr(IntPtr p)

        {
            MessageBox.Show("1111111111");
        }
        

    外部调用:

             System.Type t = Type.GetTypeFromProgID("TestComServer.TestComVisibleClass");
            dynamic o = Activator.CreateInstance(t);
            string iCameraID = txtCameraIndexCode.Text;//"12345678001310000017";
            System.IntPtr ptrCertFile = Marshal.StringToBSTR(iCameraID);
            o.TestPtr(ptrCertFile);       

    3.报错内容

    未处理System.InvalidCastException
      _HResult=-2147467262
      _message=OleAut 报告了类型不匹配。
      HResult=-2147467262
      IsTransient=false
      Message=OleAut 报告了类型不匹配。
      RemoteStackTrace=   在 Microsoft.Win32.OAVariantLib.ChangeTypeEx(Variant& result, Variant& source, Int32 lcid, IntPtr typeHandle, Int32 cvType, Int16 flags)
       在 Microsoft.Win32.OAVariantLib.ChangeType(Variant source, Type targetClass, Int16 options, CultureInfo culture)
       在 System.OleAutBinder.ChangeType(Object value, Type type, CultureInfo cultureInfo)
       在 System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
       在 System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
       在 System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
       在 System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)

      Source=mscorlib
      StackTrace:
           在 Microsoft.Win32.OAVariantLib.ChangeTypeEx(Variant& result, Variant& source, Int32 lcid, IntPtr typeHandle, Int32 cvType, Int16 flags)
           在 Microsoft.Win32.OAVariantLib.ChangeType(Variant source, Type targetClass, Int16 options, CultureInfo culture)
           在 System.OleAutBinder.ChangeType(Object value, Type type, CultureInfo cultureInfo)
           在 System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
           在 System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
           在 System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
           在 System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
           在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
           在 System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
           在 System.Dynamic.ComRuntimeHelpers.CheckThrowException(Int32 hresult, ExcepInfo& excepInfo, UInt32 argErr, String message)
           在 CallSite.Target(Closure , CallSite , ComObject , IntPtr )
           在 System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
           在 CallSite.Target(Closure , CallSite , Object , IntPtr )
           在 System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid2[T0,T1](CallSite site, T0 arg0, T1 arg1)
           在 TestDeviceSDK.MainForm.button5_Click(Object sender, EventArgs e) 位置 c:\TestComServer\新建文件夹\Test8300SDK\MainForm.cs:行号 160
           在 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)
           在 TestDeviceSDK.Program.Main() 位置 c:\TestComServer\新建文件夹\Test8300SDK\Program.cs:行号 16
           在 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.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           在 System.Threading.ThreadHelper.ThreadStart()
      InnerException: 

    请各位大神帮忙解决,谢谢

    2018年3月5日 8:06

全部回复

  • 这样修改试一下

    int iCameraID =Convert.ToInt32(txtCameraIndexCode.Text);

    System.IntPtr ptrCertFile = Marshal.StringToBSTR(iCameraID);


    专注于.NET ERP/CRM开发框架,C/S架构,SQL Server + ORM(LLBL Gen Pro) + Infragistics WinForms

    2018年3月6日 0:53