Archive for the ‘ Programming ’ Category

Error report after user closes Revit

I got  bug report, where the customer was saying, that Revit is crashing when our addin is installed. After discussion, I got some more information, but still not enough to solve it. It was indeed problem with our addin – Revit didn’t crash, but the user got an error report, when Revit was closing.

After some more investigation, I found out, that this wasn’t happening on development machines, since we got UAC turned off. Once I got UAC to “Always notify”, I got the same problem.

The reason behind this was, that we are creating log file in SpecialFolder.CommonApplicationData directory (C:\ProgramData\ on on Windows 7 ) , which is where Windows does not allow us to write anything. Problem was solved once I moved log file to current user’s application data (SpecialFolder.ApplicationData). I might add, that I am on Windows 7 64bit.

Below are the directories, which are affected by UAC setting (Windows Vista & Windows 7):

SpecialFolder.CommonApplicationData: C:\ProgramData
SpecialFolder.ProgramFiles: C:\Program Files

This one is safe:

SpecialFolder.ApplicationData:        C:\Documents and Settings\myusername\Application Data

If for some reason you wan to keep your file in these directories, you can try with UAC elevation.
And those folders on Windows XP:

SpecialFolder.CommonApplicationData:  C:\Documents and Settings\All Users\Application Data
SpecialFolder.ProgramFiles:           C:\Program Files
SpecialFolder.ApplicationData:        C:\Documents and Settings\myusername\Application Data

Another interesting thing is, that any Revit shared parameter definition file stored in CommonApplicationData isn’t affected by this setting.

Enhanced by Zemanta

InnoSetup: Check if Revit is running

When installing Revit (or AutoCAD) addins, you have to check if process is running otherways files are not copied correctly.

I am using Inno Setup for my setup projects, since I found it extremely powerful and easy to use. Previously I used Visual Studio Installer, you can read Kean’s tutorial how to implement it. The solution was great since you got the MSI file, which you could include in other installers, but there was to much mantainance and then came the news about retirement.

The code to check in Inno Setup if Revit is running:

[Code]
function IsModuleLoaded(modulename : PAnsiChar): Boolean;
  external 'IsModuleLoaded2@files:psvince.dll stdcall';

function InitializeSetup(): Boolean;
var
    Guid : string;
    ErrorCode : integer;
    nMsgBoxResult: Integer;
  foo : string;
  dir: string;
begin
  Result := True;
  while IsModuleLoaded2('Revit.exe') and (nMsgBoxResult <> IDCANCEL) do
  begin
      nMsgBoxResult := MsgBox(CustomMessage('RevitRunning'), mbConfirmation, MB_RETRYCANCEL)
  end;
  // if Cancel is pressed
  if nMsgBoxResult = IDCANCEL then
  begin
    Result := False;
  end;
end;

You need to include psvince.dll file in the [Files] section of the script, for the setup to run properly.

[Files]
Source: {#PATH}\psvince\x86\psvince.dll; Flags: dontcopy

You can download source for psvince.dll. When tested on 64 bit machines, I noticed, that 64 bit processes are not found, so I came up with this:

– add definition for new function at the top of psvince.cpp

BOOL WINAPI EnumProcs2(char* procname);

– copy code bellow in psvince.cpp

int APIENTRY IsModuleLoaded2( char *lpModule )
{
    return EnumProcs2( lpModule );
}

BOOL WINAPI EnumProcs2(char* procname)
{
    //MessageBox(NULL, procname, "msg", MB_OK);
    HANDLE handleToSnapshot;
    PROCESSENTRY32 procEntry;
    procEntry.dwSize = sizeof(PROCESSENTRY32);
    handleToSnapshot = CreateToolhelp32Snapshot(2, 0);
    if (Process32First(handleToSnapshot, &procEntry))
    {
        do
        {
            //MessageBox(NULL, procEntry.szExeFile, "msg",MB_OK);
            if (strcmp(procname, procEntry.szExeFile) == 0)
            {
                //delete handleToSnapshot;
                return TRUE;
            }
        } while (Process32Next(handleToSnapshot, &procEntry));
    }
    return FALSE;
}

– update exports in psvince.def

Obfuscate Revit addins

EDIT: What is described below, does not work, if you have UAC enabled – you can not copy files in C:\Program Files\ directory or subdirectories – see this post for more information.


All of you that develop Revit addins have or will at one point need some obfuscation tool to protect your intellectual property. Jeremy and Kean have already written about what obfuscation is and how to do it for either Revit or AutoCAD addins.

I would like to add few notes which may be useful for you. Since I was on a tight budget, the only option for me was free obfuscator. I chose Eazfuscator.NET.

When I ran the obfuscation, I got an error saying “Unable to find type…” – see this post for more info. Daren had the similar problem and he solved it with free version of Babel.NET.

The solution was, to copy all the assemblies in Revit directory (e.g. C:\Program Files\Autodesk\Revit Architecture 2011\Program\) obfuscate it there and then move it back to working directory. Here is the BATCH file which does the trick (the file has to be located in your bin directory):

rem copy libraries
set file=Library1.dll
copy /y %file% "C:\Program Files\Autodesk\Revit Architecture 2011\Program\"
set file=Library2.dll
copy /y %file% "C:\Program Files\Autodesk\Revit Architecture 2011\Program\"

rem copy addin
set assembly=Addin.dll
copy /y %assembly% "C:\Program Files\Autodesk\Revit Architecture 2011\Program\"

rem obfuscate
"C:\Program Files (x86)\Eazfuscator.NET\eazfuscator.net.exe" "C:\Program Files\Autodesk\Revit Architecture 2011\Program\%assembly%"
move /y "C:\Program Files\Autodesk\Revit Architecture 2011\Program\%assembly%"

rem cleanup libraries
set file=Library1.dll
del "C:\Program Files\Autodesk\Revit Architecture 2011\Program\%file%"
set file=Library2.dll
del "C:\Program Files\Autodesk\Revit Architecture 2011\Program\%file%"

I haven’t tried it yet, but I guess the method works also for AutoCAD addins.

Enhanced by Zemanta