Tuesday, 3 January 2006

How to stop TlbImp requiring admin privileges

It seems that the Type Library Importer tool, TlbImp.exe, sometimes needs administrative privileges to do its job, if you’re trying to create a Primary Interop Assembly (or otherwise a strong-named reference). At work today, even admin privileges weren’t enough – I’m not sure if something’s been broken after installing Visual Studio 2005 since it used to work fine. The error given is ‘Invalid strong name parameters specified.’

What seems to be required is that the strong-name-key needs to be added to a key container in order to be used, which TlbImp is doing for you behind the scenes. The container used by strong-name functions can be either a user or a machine container. The default seems to be a machine container (at least that’s been the case both here at home and at work).

You can switch to a user container using the following command from a Visual Studio command prompt (either from the Start Menu’s Visual Studio group, or by running vsvars32.bat from the Common7\Tools directory, or sdkvars.bat from the .NET Framework SDK Bin directory):

sn -m n

Having done this, you should no longer need administrative privileges to strongly-name an assembly.

1 comment:

Anonymous said...

Mike, thank you very much for your tip . There is no admin privileges my ordinary PC desktop at workplace: Citigroup in New Jersey. Downloads and installations are normally prohibited.

I tried to compile samples of C:\Program Files\Microsoft.NET\SDK\v1.1\Samples\Technologies\Interop\Applications\Office
. I am interested in writing Excel files from C#; it is only Office2000 installed here therefore I guess I have to use the Interop feature.

I was able to compile only after using the command as you recommended "sn -m n".

Thanks again, you saved me a lot of time.
Marius,
marius@dandy.net

C:\tmp\Office\Excel>set ExcelTlb=C:\Program Files\Microsoft Office\Office\excel9.olb

C:\tmp\Office\Excel>nmake

Microsoft (R) Program Maintenance Utility Version 7.00.9466
Copyright (C) Microsoft Corporation. All rights reserved.

tlbimp /silent "C:\Program Files\Microsoft Office\Office\excel9.olb" /keyfile:.\Excel.Key /
:.\Excel.DLL
Microsoft (R) .NET Framework Type Library to Assembly Converter 1.0.3705.0
Copyright (C) Microsoft Corporation 1998-2001. All rights reserved.

TlbImp error: Invalid strong name parameters specified.
NMAKE : fatal error U1077: 'tlbimp' : return code '0x64'
Stop.

C:\tmp\Office\Excel>sn -m n

Microsoft (R) .NET Framework Strong Name Utility Version 1.0.3705.0
Copyright (C) Microsoft Corporation 1998-2001. All rights reserved.

Key containers are user based

C:\tmp\Office\Excel>nmake

Microsoft (R) Program Maintenance Utility Version 7.00.9466
Copyright (C) Microsoft Corporation. All rights reserved.

tlbimp /silent "C:\Program Files\Microsoft Office\Office\excel9.olb" /keyfile:.\Excel.Key /
:.\Excel.DLL
gacutil -i .\Excel.DLL

Microsoft (R) .NET Global Assembly Cache Utility. Version 1.0.3705.0
Copyright (C) Microsoft Corporation 1998-2001. All rights reserved.

Assembly successfully added to the cache
cd CS
nmake all

Microsoft (R) Program Maintenance Utility Version 7.00.9466
Copyright (C) Microsoft Corporation. All rights reserved.

csc /debug+ /r:..\Excel.dll /out:AutoExcel.exe AutoExcel.cs
Microsoft (R) Visual C# .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (C) Microsoft Corporation 2001. All rights reserved.

cd ..
cd VB
nmake all

Microsoft (R) Program Maintenance Utility Version 7.00.9466
Copyright (C) Microsoft Corporation. All rights reserved.

vbc /t:exe /debug+ /optionstrict- /r:..\Excel.dll /out:AutoExcel.exe AutoExcel.vb
Microsoft (R) Visual Basic .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.00.3705
Copyright (C) Microsoft Corporation 1987-2001. All rights reserved.

cd ..
cd VC
nmake all

Microsoft (R) Program Maintenance Utility Version 7.00.9466
Copyright (C) Microsoft Corporation. All rights reserved.

cl /c /CLR /AI.. /Zi AutoExcel.cpp
Microsoft (R) C/C++ Standard Compiler Version 13.00.9466 for .NET Framework
Copyright (C) Microsoft Corporation 1984-2001. All rights reserved.

AutoExcel.cpp
link /debug /nod:libcpmt.lib kernel32.lib mscoree.lib /out:AutoExcel.exe AutoExcel.obj
link: extra operand `kernel32.lib'
Try `link --help' for more information.
NMAKE : fatal error U1077: 'link' : return code '0x1'
Stop.
cd ..

C:\tmp\Office\Excel>