Thông tin lỗi
1. Nội dung lỗi
Lỗi hiển thị trong AutoCAD có nội dung:
Tạm dịch:
- 
Lỗi không được xử lý xuất hiện trong ứng dụng. 
- 
Không tìm thấy phương thức: System.Runtime.InteropServices.Marshal.GetActiveObject(System.String).
2. Nguyên nhân gây lỗi
- 
Phương thức GetActiveObjectkhông tồn tại hoặc không khả dụng:- 
Phương thức này thuộc thư viện System.Runtime.InteropServices.Marshalvà được sử dụng để lấy phiên bản đang chạy của AutoCAD.
- 
Một số framework .NET (đặc biệt là .NET Core/.NET 5+) đã loại bỏ hoặc hạn chế sử dụng phương thức này. Lỗi này sẽ gặp khi sử dụng AutoCAD 2025 do đã thay đổi .NET 
 
- 
- 
Chưa tham chiếu đúng thư viện COM: - 
Nếu đang sử dụng AutoCAD .NET API, bạn cần đảm bảo thư viện Autodesk.AutoCAD.Interopđược tham chiếu đúng.
 
- 
- 
Không cài đặt đúng môi trường AutoCAD COM: 
- 
Khi chạy ứng dụng ngoài AutoCAD (ví dụ: từ ứng dụng Windows Forms hoặc Console App), AutoCAD COM có thể chưa được đăng ký đúng cách. 
3. Giải pháp khắc phục
✅ Cách 1: Kiểm tra framework .NET
Nếu bạn đang dùng .NET 5+ hoặc .NET Core, hãy thử chuyển sang .NET Framework 4.8 vì các phiên bản mới đã giới hạn một số API COM.
✅ Cách 2: Kiểm tra và thêm tham chiếu thư viện
- 
Mở Solution Explorer trong Visual Studio. 
- 
Chuột phải vào References → Add Reference. 
- 
Chọn tab COM và tìm Autodesk AutoCAD Object Library.
- 
Thêm cả thư viện System.Runtime.InteropServices nếu chưa có. 
✅ Cách 3: Thay thế GetActiveObject bằng một phương thức thay thế
Khắc phục: Sử dụng phương thức thay thế
using System;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
namespace AJS_Marshal2
{
    public static class Marshal2
    {
        internal const String OLEAUT32 = "oleaut32.dll";
        internal const String OLE32 = "ole32.dll";
        [System.Security.SecurityCritical]  // auto-generated_required
        public static Object GetActiveObject(String progID)
        {
            Object obj = null;
            Guid clsid;
            // Call CLSIDFromProgIDEx first then fall back on CLSIDFromProgID if
            // CLSIDFromProgIDEx doesn't exist.
            try
            {
                CLSIDFromProgIDEx(progID, out clsid);
            }
            //            catch
            catch (Exception)
            {
                CLSIDFromProgID(progID, out clsid);
            }
            GetActiveObject(ref clsid, IntPtr.Zero, out obj);
            return obj;
        }
        //[DllImport(Microsoft.Win32.Win32Native.OLE32, PreserveSig = false)]
        [DllImport(OLE32, PreserveSig = false)]
        [ResourceExposure(ResourceScope.None)]
        [SuppressUnmanagedCodeSecurity]
        [System.Security.SecurityCritical]  // auto-generated
        private static extern void CLSIDFromProgIDEx([MarshalAs(UnmanagedType.LPWStr)] String progId, out Guid clsid);
        //[DllImport(Microsoft.Win32.Win32Native.OLE32, PreserveSig = false)]
        [DllImport(OLE32, PreserveSig = false)]
        [ResourceExposure(ResourceScope.None)]
        [SuppressUnmanagedCodeSecurity]
        [System.Security.SecurityCritical]  // auto-generated
        private static extern void CLSIDFromProgID([MarshalAs(UnmanagedType.LPWStr)] String progId, out Guid clsid);
        //[DllImport(Microsoft.Win32.Win32Native.OLEAUT32, PreserveSig = false)]
        [DllImport(OLEAUT32, PreserveSig = false)]
        [ResourceExposure(ResourceScope.None)]
        [SuppressUnmanagedCodeSecurity]
        [System.Security.SecurityCritical]  // auto-generated
        private static extern void GetActiveObject(ref Guid rclsid, IntPtr reserved, [MarshalAs(UnmanagedType.Interface)] out Object ppunk);
        //Collected by lisp.vn
    }
}Microsoft.Office.Interop.Excel.Application xlApp =(Microsoft.Office.Interop.Excel.Application)Marshal2.GetActiveObject("Excel.Application");
using System; using System.Runtime.InteropServices; namespace AJS_Marshal2 { public class Excels { [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll", SetLastError = true)] private static extern System.IntPtr FindWindow(string lpClassName, string lpWindowName); public static Microsoft.Office.Interop.Excel.Application StartExcel() { Microsoft.Office.Interop.Excel.Application xlApp = null; try { //xlApp = (Microsoft.Office.Interop.Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"); xlApp = (Microsoft.Office.Interop.Excel.Application)Marshal2.GetActiveObject(Defs.Excel); xlApp.Visible = true; } catch (System.Runtime.InteropServices.COMException ex) { xlApp = new Microsoft.Office.Interop.Excel.Application(); } if (xlApp != null) { object misValue = System.Reflection.Missing.Value; if (xlApp.Workbooks.Count == 0) { Microsoft.Office.Interop.Excel.Workbook wb = xlApp.Workbooks.Add(misValue); wb.Activate(); } xlApp.Visible = true; xlApp.ActiveWindow.Activate(); string caption = xlApp.Caption; IntPtr handler = FindWindow(null, caption); SetForegroundWindow(handler); } else System.Windows.MessageBox.Show("Excel is not properly installed!!");//Collected by lisp.vn return xlApp; } } }

 
 
Không có nhận xét nào:
Đăng nhận xét