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
In c#. Convert it to VB.net.
Using Activator:
Using Marshall: