NET (Core) COM connection to Catia v5/v6 issue

54 Views Asked by At

I am trying to move way from Net Framework but I cannot use COM with the new NET. As I understand, Net does not have the GetObject/GetActiveObject like Net Framework so we need to implement our Marshal - which I used the code here https://chat.openai.com/share/3f361a43-31b7-4093-ad96-5728ae7df5e9

Issue: I can get the CATIA.APPLICATION but I cannot operate or get any property

Dim CATIA = Marshal2.GetActiveObject("CATIA.APPLICATION")
Dim caption as string = CATIA.Caption ~~~~> this returns the generic error `HRESULT E_FAIL"

I wonder if anyone has successfully used Net Core to make the connection? I would like to note that Catia is installed on remote machine and I have no right to install Visual Studio on the remote machine that why my code must use GetObject equivalent.

Imports System.Runtime.InteropServices
Imports System.Security

Private Class Marshal2
    Friend Const OLEAUT32 As String = "oleaut32.dll"
    Friend Const OLE32 As String = "ole32.dll"

    <SecurityCritical>
    Public Shared Function GetActiveObject(ByVal progID As String) As Object
        Dim obj As Object = Nothing
        Dim clsid As Guid

        ' Appelez d'abord CLSIDFromProgIDEx, puis revenez à CLSIDFromProgID si
        ' CLSIDFromProgIDEx n'existe pas.
        Try
            CLSIDFromProgIDEx(progID, clsid)
        Catch ex As Exception
            CLSIDFromProgID(progID, clsid)
        End Try

        GetActiveObject(clsid, IntPtr.Zero, obj)
        Return obj
    End Function

    <DllImport(OLE32, PreserveSig:=False)>
    <ResourceExposure(ResourceScope.None)>
    <SuppressUnmanagedCodeSecurity>
    <SecurityCritical>
    Private Shared Sub CLSIDFromProgIDEx(<MarshalAs(UnmanagedType.LPWStr)> ByVal progId As String, ByRef clsid As Guid)
    End Sub

    <DllImport(OLE32, PreserveSig:=False)>
    <ResourceExposure(ResourceScope.None)>
    <SuppressUnmanagedCodeSecurity>
    <SecurityCritical>
    Private Shared Sub CLSIDFromProgID(<MarshalAs(UnmanagedType.LPWStr)> ByVal progId As String, ByRef clsid As Guid)
    End Sub

    <DllImport(OLEAUT32, PreserveSig:=False)>
    <ResourceExposure(ResourceScope.None)>
    <SuppressUnmanagedCodeSecurity>
    <SecurityCritical>
    Private Shared Sub GetActiveObject(ByRef rclsid As Guid, ByVal reserved As IntPtr, <MarshalAs(UnmanagedType.Interface)> ByRef ppunk As Object)
    End Sub
End Class
1

There are 1 best solutions below

0
Disvoys On

In c#. Convert it to VB.net.

Using Activator:

Catia = (INFITF.Application)Activator.CreateInstance(Type.GetTypeFromProgID("CATIA.Application"));

Using Marshall:

            Catia = (INFITF.Application)Marshal2.GetActiveObject("Catia.Application");


private static class Marshal2
        {
            internal const string OLE32 = "ole32.dll";
            internal const string OLEAUT32 = "oleaut32.dll";

            [SecurityCritical]
            public static object GetActiveObject(string progID)
            {
                object obj = null;
                Guid clsid;

                try
                {
                    CLSIDFromProgIDEx(progID, out clsid);
                }
                catch (Exception)
                {
                    CLSIDFromProgID(progID, out clsid);
                }

                GetActiveObject(ref clsid, IntPtr.Zero, out obj);
                return obj;
            }

            [DllImport(OLE32, PreserveSig = false)]
            [ResourceExposure(ResourceScope.None)]
            [SuppressUnmanagedCodeSecurity]
            [SecurityCritical]  // auto-generated
            private static extern void CLSIDFromProgID([MarshalAs(UnmanagedType.LPWStr)] string progId, out Guid clsid);

            [DllImport(OLE32, PreserveSig = false)]
            [ResourceExposure(ResourceScope.None)]
            [SuppressUnmanagedCodeSecurity]
            [SecurityCritical]  // auto-generated
            private static extern void CLSIDFromProgIDEx([MarshalAs(UnmanagedType.LPWStr)] string progId, out Guid clsid);

            [DllImport(OLEAUT32, PreserveSig = false)]
            [ResourceExposure(ResourceScope.None)]
            [SuppressUnmanagedCodeSecurity]
            [SecurityCritical]
            private static extern void GetActiveObject(ref Guid rclsid, IntPtr reserved, [MarshalAs(UnmanagedType.Interface)] out object ppunk);
        }