Save a password protected drawing without password with VB.NET

Save a password protected drawing without password with VB.NET

Anonymous
Not applicable
1,210 Views
3 Replies
Message 1 of 4

Save a password protected drawing without password with VB.NET

Anonymous
Not applicable

Hello,

 

our drawings are protected by a password.

We have a VB.NET programm to export a drawing without password.

 

...

Dim securityPar As New SecurityParameters(Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)

doc.Database.SaveAs("C:\Temp\Test.DWG", False, DwgVersion.Current, securityPar)

doc.CloseAndSave("C:\Temp\Test.DWG")

...

 

In AutoCAD Architecture 2009 (Win XP) it works fine.

In AutoCAD Architecture 2011 (Win 7) the drawing is still password protected.

 

Thank you for help,

 

Udo

0 Likes
Accepted solutions (1)
1,211 Views
3 Replies
Replies (3)
Message 2 of 4

Alexander.Rivilis
Mentor
Mentor

With help of ObjecARX (native) you can clear password protection: http://forums.autodesk.com/t5/Autodesk-ObjectARX/any-way-to-remove-password-from-a-dwg/m-p/3429797#M...

I've not found corresponding method AutoCAD .NET API. IMHO you have to P/Invoke setSecurityParams method.

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 3 of 4

Alexander.Rivilis
Mentor
Mentor
Accepted solution

Try this code (minimum testing with AutoCAD 2012 x86):

 

using System;
using System.Runtime.InteropServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;

// This line is not mandatory, but improves loading performances
[assembly: CommandClass(typeof(Rivilis.ClearPassword))]

namespace Rivilis
{

    // This class is instantiated by AutoCAD for each document when
    // a command is called by the user the first time in the context
    // of a given document. In other words, non static data in this class
    // is implicitly per-document!
    public class ClearPassword
    {
        [StructLayout(LayoutKind.Sequential)]
        // From dbsecurity.h
        public struct SecurityParams
        {
            public uint cbSize;
            public uint ulFlags;    // see enum above for flag values
            // data relevant to password protection
            public string wszPassword;
            public uint ulProvType;
            public string wszProvName;
            public uint ulAlgId;        // SECURITYPARAMS_ALGID_RC4
            public uint ulKeyLength;
            // data relevant to digital signatures
            public string wszCertSubject;
            public string wszCertIssuer;
            public string wszCertSerialNum;
            public string wszComment;
            public string wszTimeServer;
        };
        [DllImport("acdb18.dll", CallingConvention = CallingConvention.ThisCall,
            EntryPoint = "?setSecurityParams@AcDbDatabase@@QAE_NPBUSecurityParams@@_N@Z")]
        static extern private bool setSecurityParamsX32(IntPtr db, ref SecurityParams pSecParams, bool bSetDbMod = true);
        [DllImport("acdb18.dll", CallingConvention = CallingConvention.ThisCall, 
            EntryPoint = "?setSecurityParams@AcDbDatabase@@QEAA_NPEBUSecurityParams@@_N@Z")]
        static extern private bool setSecurityParamsX64(IntPtr db, ref SecurityParams pSecParams, bool bSetDbMod = true);

        static public bool setSecurityParams(Database db, ref SecurityParams parms, bool bSetDbMod = true)
        {
            if (IntPtr.Size == 4) return setSecurityParamsX32(db.UnmanagedObject, ref parms, bSetDbMod);
                     else         return setSecurityParamsX64(db.UnmanagedObject, ref parms, bSetDbMod);
        }

        [CommandMethod("ClearPWD", CommandFlags.Modal)]
        public void ClearPWD()
        {
            Database db = HostApplicationServices.WorkingDatabase;
            SecurityParams parms = new SecurityParams();
            parms.cbSize = (uint)Marshal.SizeOf(parms);
            parms.ulFlags = 0; // Clear security flag
            setSecurityParams(db, ref parms);
        }
    }
}

For VB.NET you can convert C# code: http://www.developerfusion.com/tools/convert/csharp-to-vb/

 

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

Message 4 of 4

Anonymous
Not applicable

Hello Alexander,

 

thank you! It works very fine in AutoCAD Architecture 2011.

 

Udo

0 Likes