Конференция ".Net" » problema s CoCreateInstance
 
  • jonni © (21.10.06 05:14) [0]
    Pitayus' vipolnit' vot takoy kod nizhe, no CoCreateInstnace vidaet neizvestniy hr i COMException vidaet ctho hr nezivestnie.
    Chto ya delayu ne pravil'no?
    Lyubie komentarii budu ochen' polezni. Spasibo.

    //-----------------------------------------------------------
           using System;
           using System.Runtime.InteropServices;
           using System.Windows.Forms;
           using Microsoft.VisualStudio.OLE.Interop;

           [DllImport("OLE32.DLL", EntryPoint = "CoCreateInstance", CallingConvention = CallingConvention.StdCall)]
           public static extern uint CoCreateInstance(ref Guid ClassGuid, IntPtr pUnkOuter, CLSCTX dwClsContext, ref Guid InterfaceGuid, ref IntPtr Result);
           [DllImport("ole32.dll", ExactSpelling = true, EntryPoint = "CoInitializeEx", CallingConvention = CallingConvention.StdCall, SetLastError = false, PreserveSig = false)]
           internal static extern void CoInitializeEx(IntPtr pvReserved, uint dwCoInit);

           public static Guid CGID_ShellServiceObject = new Guid(0x000214D2, 0x0000, 0x0000, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);
           public static Guid IID_IOleCommandTarget   = new Guid(0xB722BCCB, 0x4E68, 0x101B, 0xA2, 0xBC, 0x00, 0xAA, 0x00, 0x40, 0x47, 0x70);
           public const uint S_OK = 0x00000000;

           public enum COINIT
           {
           COINIT_MULTITHREADED = 0x0,
           COINIT_APARTMENTTHREADED = 0x2,
           COINIT_DISABLE_OLE1DDE = 0x4,
           COINIT_SPEED_OVER_MEMORY = 0x8
           }

           IntPtr Result = IntPtr.Zero;
           uint hr = S_OK;
           CoInitializeEx(IntPtr.Zero, (uint)COINIT.COINIT_APARTMENTTHREADED | (uint)COINIT.COINIT_DISABLE_OLE1DDE);

           hr = CoCreateInstance(ref CGID_ShellServiceObject, IntPtr.Zero, CLSCTX.CLSCTX_INPROC_HANDLER | CLSCTX.CLSCTX_INPROC_SERVER, ref IID_IOleCommandTarget, ref Result);
           if (hr == S_OK)
           {
               pCmdTarget = (IOleCommandTarget)(Marshal.GetObjectForIUnknown(Result));
               pCmdTarget.Exec(ref CGID_ShellServiceObject, 2, 0, IntPtr.Zero, IntPtr.Zero);
           }
           else
           {
               throw new COMException("Error creating instance", (int)hr);
           }
  • jonni © (24.10.06 00:16) [1]
    heh... navernoe nikto s COM ne hochet rabotat' v .NET :(
  • DiamondShark © (24.10.06 00:27) [2]
    Во-первых, зачем такие сложности?
    Почему не нравится обычный путь
    Type.GetTypeFromCLSID(guid)
    Activator.CreateInstance(type)

    Во-вторых, что это за объект? Какой-то CLSID у него подозрительный...
  • DiamondShark © (24.10.06 00:28) [3]

    > heh... navernoe nikto s COM ne hochet rabotat' v .NET :(

    таким образом, как написано -- правда, никто не хочет.
    ;)
  • jonni © (24.10.06 03:27) [4]
    DiamondShark ©   (24.10.06 00:27) [2]

    da vi pravi... sdelal oshibku.. v createinstance dolzhen bil bit' drugoy GUID.

    eto Shell Service Object;
    public Guid MyObject  = new Guid(0x35CEC8A3, 0x2BE6, 0x11D2, 0x87, 0x73, 0x92, 0xE2, 0x20, 0x52, 0x41, 0x53);

    posledoval vashemu sovetu...
    Type mytype = Type.GetTypeFromCLSID(MyObject);
    pCmdTarget = (IOleCommandTarget)Activator.CreateInstance(mytype);
    pCmdTarget.Exec(ref CGID_ShellServiceObject, 2, 0, IntPtr.Zero, IntPtr.Zero);

    i vse rabotaet,.... URA....
    budu ochen' blagodaren esli posovetuete gde mozheno prochitat' pro Activator (ne msdn, tam ya uzhe bill, opisanie tam konehcno horoshee, no vto kak ispol'zovat' primerov malo)
 
Конференция ".Net" » problema s CoCreateInstance
Есть новые Нет новых   [134430   +2][b:0][p:0]