Saturday, August 6, 2011

How to register a com dll in the context of an administrator user in c#?

The below code sample can be used for registering a com dll from c# by using the regsvr32 command. Normally in a real-time environ the logged in windows ,will not be having enough privileges to carry out the dll registration .In this scenario the code we write should be capable enough to register the dll in some other users context (May be administration) for carrying out the registration process.


The below code registers the dll in the context of administration in order to overcome the privilege issues.


string Adminuser=”Administrator”;
string Password=”Password”;
string Domain=Domainname;
string File=”mydll.dll”;//dll to registrer
System.Diagnostics.ProcessStartInfo ProcessInfo;
ProcessInfo = new System.Diagnostics.ProcessStartInfo();  
ProcessInfo.UserName = @Adminuser;
ProcessInfo.Domain = Domain;
ProcessInfo.Password = SecurePassword;
ProcessInfo.FileName = "regsvr32";
ProcessInfo.UseShellExecute = false;
ProcessInfo.Arguments = File + " /s"; //Here /s is uesr to carring out the
//registration process silently with out showing any message
System.Diagnostics.Process.Start(ProcessInfo);


Hope the above code helps you all.




Happy programing…………………



No comments:

Post a Comment