Registering a .NET DLL with Regasm using Powershell fails - windows

With visual studio 2012 I created a DLL file in C# which can be used in Access 2013 by referencing it. This all works fine. I Created the DLL and registered it with CMD doing: RegAsm.exe -tlb -codebase C:\MyFolderX\MyDLL.dll
This all works fine, but because the DLL needs to be registered at multiple computers I wanted to do this with 1 click instead of doing it manually at each user computer.
Because users already use a BATCH file to launch the Access frontend application (which uses the DLL) I thought it would be wise to register it once when they use the BATCH startup. So to do this this I added the following in my BATCH script:
cd C:\Windows\Microsoft.NET\Framework\v4.0.30319
RegAsm.exe -tlb -codebase C:\MyFolderX\MyDLL.dll
This sadly doesn't work because it has to be done in admin mode and checking the checkbox run as administrator just jumps through my BATCH code without doing anything for some reason.
So I though, why not use a Powershell script to do the same and launch that from my batch script.
To do this I created the following script:
#Register the assembly
$RegAsm = 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe -codebase -tlb'
$Assembly = 'C:\MyFolderX\MyDLL.dll'
Start-Process $RegAsm $Assembly
pause
This however keeps giving the error:
Start-Process : This command cannot be run due to the error: The system cannot find the file specified. At C:\users\me\Desktop\RegisterMyDLL.pst1
+Start-Process $RegAsm $Assembly
InvalidOperation: (:) Start-Process
FullyQualifiedErrorId : InvalidOperationException, Microsoft.Powershell.Commands.StartProcessCommand
I double checked the location of the DLL and it it just there.. Anyone have a clue what I am doing wrong? Perhaps some syntax error or quote to much? Already tried to escape my backslashes but this didn't had any effect.
Or perhaps there is an better way to achieve easy DLL registering at multiple users?

Does this work?
."C:\Windows\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe" -codebase -tlb $Assembly

Related

Powershell Closes Instantly

I've looked all of the the internet and I cannot find any information that applies to this situation.
Powershell closes immediately upon starting. When I run it in Command Prompt I get the following:
Windows PowerShell terminated with the following error:
Unable to cast object of type 'System.String' to type 'System.String[]'.
I have checked the following folders and no profiles exist:
c:\users\me\appdata\microsoft\windows\powershell
c:\windows\system32\windowspowershell\v1.0\
c:\windows\systwow64\windowspowershell\v1.0\
I have tried to run with the following commands and have no luck:
powershell -noexit
powershell -noprofile
I have run the following commands and have no luck:
sfc.exe /scannow
DISM.exe /Online /Cleanup-image /Restorehealth
I also have gone to Control Panel -> Uninstall a Program -> Turn Windows features on or off then,
Removed Powershell, rebooted, then re-installed it.
After doing all of these steps I still am not able to run Powershell. ISE does not work either.
This may be related to Powershell logging settings. I had the exact same issue after implementing Powershell Module logging using the wrong path for ModuleNames.
Check the values set in HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ModuleLogging
In my case, I made the mistake of adding a value of ModuleNames set to * - not realizing it should be a KEY named ModuleNames - which resulted in an error
Windows PowerShell terminated with the following error:
Unable to cast object of type 'System.String' to type 'System.String[]'.
By adding the right path HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\PowerShell\ModuleLogging\ModuleNames\ with a REG_SZ value named * and with a value of * I got Powershell working again.
Note that if this setting comes from a GPO (Group Policy Object) in the domain, it will need to be fixed there, rather than in the registry. Otherwise, the GPO will just overwrite the local settings the next time it applies.
Whenever you run powershell, it loads the default modules present in the Modules directory. One of these modules (most likely a custom one you wrote) is causing errors and not allowing you to start.
Without knowing anything about the modules you have present in the directory located at: C:\windows\system32\windowspowershell\v1.0\Modules, it would be hard for anyone to tell you the solution.
Recommendation
Remove any custom modules you have in there and add each module you need one at a time to see which one breaks your powershell.exe. You will need to check each path you have defined for custom modules to be loaded as well.
Other way would be to clear out the PSModulePath from Environment variables and add one location at a time until you see which Modules directory is causing error.
NOTE: Write down the paths on a notepad somewhere before you clear it.
From the error it seems like a .net library class (dll) that is not correctly written.

Custom Action failing to execute during installation created with Installshield 16

I have run this custom action with Installshield Limited Edition for Visual Studio in the past and it has worked. But now when I try this with Installshield 2016, this custom action gives me a 1722 error and rolls back the installation. The log file doesn't give any more detail than "failed with error 1..." and the 1722 error.
My custom action setup via the Wizard -
Working Dir: InstallDir
FileName & Command Line: "[SystemFolder]cmd.exe" /c "[INSTALLDIR]somefilename.exe" "'%r' '%keyname=keyname' '%keydll=some.dll' '%appexe=[INSTALLDIR]differentfilename.exe'"
What this is supposed to do is run somefilename.exe from the command line, with parameters "%r", "%keyname", "%keydll" & "%appexe".
When I run it on the command line directly so -
"C:\Program Files (x86)"\somefilename.exe "%r" "%keyname=keyname" "%keydll=some.dll" "%appexe=C:\Program Files (x86)\differentfilename.exe" - it runs fine.
I think I am missing some quotes someplace and I have tried various combinations with no luck.
Any ideas what I am doing wrong?
Thanks in advance!
Thanks for the suggestions #PhilDW.
I could possibly take out the cmd jacket and just run the exe and try.
I finally got it working though, by changing some quotes etc. Here's what the final FileName & Command Line argument looks like:
"[SystemFolder]cmd.exe" /c start "" /d"C:\Program Files (x86)\foldername\" "somefile.exe" "%r" "%keyname=something" "%keydll=something.dll" "%appexe=C:\Program Files (x86)\otherfilename.exe"
Hope this helps someone.
A few suggestions:
You should post the verbose MSI log section relating to this because it should show the complete resolved command line, assuming that you have created a full verbose log and not a partial log.
It's not clear why you need to run this program with a cmd jacket. If it's a plain Windows program just run the executable as a custom action.
When you run from the interactive user explorer shell you get some infrastructure (such as working directory) that you do not get with a custom action started by an msiexec.exe process. This matters because you have not specified an explicit full path to some.dll, so it's not obvious it can find the file.
It might be useful to say something about where this custom action runs and its type. For example if it's turned into an immediate custom action (all VS custom actions are deferred) then it will fail because no files have yet been installed. Likewise, if it's deferred but somehow before the InstallFiles standard action it will fail.
All custom actions run by Visual Studio generated projects are deferred and run with the system account in a per-system "Everyone" install. If your custom action requires elevation then it must also be deferred and the MSI must show a UAC elevation dialog, because otherwise it may well run but fail with access errors. It may have become a non-elevated per user install.

VS error when creating new WebAPI project

When I create a new WebAPI project (MVC4) I get the following error.
EntityFramework.5.0.0: Failed to initialize the Powershell host. If your powershell execution policy setting is set to AllSigned, open the package manager console to initialize the host first.
jQuery.1.7.1.1: Failed to initialize the Powershell host. If your powershell execution policy setting is set to AllSigned, open the package manager console to initialize the host first.
After Googling I have found a few answers but nothing that works yet.
Error creating new MVC project - EF and JQuery
This answer seems like it should work for me as my last project was a 7z Command Line app and I might have done something daft with 7zip. But I copy pasted the 7-Zip directory from Program Files to Program Files (86) with no luck.
http://social.msdn.microsoft.com/Forums/en-US/vssetup/thread/c934fed4-e44e-4a06-9e3b-eccb9c8aa8d6
There is an answer here that might work (I haven't tried it) but even if it does work I wouldnt want to do this every time I create a new project.
Is anyone able to help me with this one?
I got around a similar error by running PowerShell as administrator with the command Set-ExecutionPolicy Unrestricted, restarting Visual Studio, and opening the Package Manager Console before what I wanted to do.
Make sure you understand the security implications of doing this first.
http://technet.microsoft.com/en-us/library/ee176961.aspx
Restricted - No scripts can be run. Windows PowerShell can be used only in interactive mode.
AllSigned - Only scripts signed by a trusted publisher can be run.
RemoteSigned - Downloaded scripts must be signed by a trusted publisher before they can be run.
Unrestricted - No restrictions; all Windows PowerShell scripts can be run.
I encountered this issue recently, after re-install VS and install the latest VS update 2, things go well. This works for me at least.

Permissions issue when building a project

I am trying to get a project to build on a machine but i get the following:
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppCommon.targets(744,5): warning MSB3075: The command "regsvr32 /s "C:\builds\working\\Win32\Debug\projx86.dll"" exited with code 5. Please verify that you have sufficient rights to run this command.
The previous error was converted to a warning because the task was called with ContinueOnError=true.
Build continuing because "ContinueOnError" on the task "Exec" is set to "true".
C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppCommon.targets(756,5): error MSB8011: Failed to register output. Please try enabling Per-user Redirection or register the component from a command prompt with elevated permissions.
The user account i am using is an Administrator on the machine so should that not have the highest privileges?
I can go to the startup and right click and run as administrator and that might sort it, but im trying to do an automated build and run of this project, so i cant use that method.
Anyone know how i might fix this?
Solution Explorer ->[YourProject]->Properties->Linker ->General->Per-user Redirection "TRUE"
I found this same issue when working on an C++ ATL project.
In my case, when I added a new ATL class I was missing a line in the resource file (.rc) that adds a resource type "registry" of the new class resource file (.rgs) . This creates the same permission issue stated above.
#LittleFairy answer is probably the best. But you could run Visual Studio as administrator.
Note: You need to explicitly start Visual Studio as administrator. Just the user account having admin rights isn't enough.

error MSB3216 when registering assembly

Here are the error details:
In the Error List:
Error 1 Cannot register assembly "C:\Users\cboardman\Documents\Visual Studio 2008\Projects\ExcelAddIn1\ExcelAddIn1\bin\Debug\ExcelAddIn1.dll" - access denied. Please make sure you're running the application as administrator. Access to the registry key 'HKEY_CLASSES_ROOT\ExcelAddIn1...' is denied. C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.Common.targets 3019 9 ExcelAddIn1
In the Build Output:
Target UnmanagedRegistration:
C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.Common.targets(3019,9): error MSB3216: Cannot register assembly "C:\Users\cboardman\Documents\Visual Studio 2008\Projects\ExcelAddIn1\ExcelAddIn1\bin\Debug\ExcelAddIn1.dll" - access denied. Please make sure you're running the application as administrator. Access to the registry key 'HKEY_CLASSES_ROOT\ExcelAddIn1...' is denied.
Done building target "UnmanagedRegistration" in project "ExcelAddIn1.csproj" -- FAILED.
From what I have found online, I need to be running Visual Studio as administrator. This seems like a big hammer for a small nail. Is there a way around this (like a way to run just the registration as administrator)?
Unfortunately there is not an easy way to do this. By default registering the components adds entries to protected keys in the registry (under HKLM in particular). This cannot be done without administrative rights.
It is technically possible to register COM components as a non-admin by using the equivalent keys under HKCU. However it is not a trivial change and I do not believe the .Net tools which register the assemblies can be configured to do this.
I think your best option is to disable registration during build. Then have a separate Admin window open where you can hand register the DLL From for debugging purposes. The re-registration is only really necessary if you change the COM related interfaces or location of the DLL so it doesn't have to be done for every F5.
Closing Visual Studio and re-opening right-clicking on it -> Run as Administrator solved the problem for me.
I had this same problem with Visual Studio 2017.
JaredPar's answer led me to this implementation:
Goto the project's properties
Select Build
Untick Register for COM interop screenshot
Select Build Events
add a Post-build event command line:
for /f %%a in ('dir %windir%\Microsoft.Net\Framework\regasm.exe /s /b') do set current_regasm="%%a"
set command=%current_regasm% $(TargetPath) /tlb:$(TargetDir)\$(TargetName).tlb /codebase ^; sleep 2
set elevated_command="Start-Process PowerShell.exe -Wait -ArgumentList \"-ExecutionPolicy Bypass -Command %command%\"
powershell -noprofile -ExecutionPolicy Bypass -Command %elevated_command% -Verb RunAs"
In the drop-down run the post-build event: select: On successful build screenshot
Run a build
At the end of the build you will see a powershell window run as administrator (depending on your settings you may have a User Account Control (UAC) popup asking you to confirm before it will run).
Note:
This will find the latest .NET framework version and use regasm from there (credit: Scott C).
Increase the ; sleep 2 if you want longer to look at the output to confirm the registration (or use ; pause instead).

Resources