Get an 'Access is denied' when creating a printer port using prnadmin.dll

I am developing a Visual C# class library (.dll) using Visual Studio 2005. I have successfully installed, registered, and referenced (within my C# class library project) the prnadmin.dll component. I wrote a function, using prnadmin.dll, which will dynamically create a printer port and printer on my local box.

Scenario 1: I create a windows application with a reference to my class library (.dll) containing the code below. The port is created successfuly by the windows application's call to create the port.

Scenario 2: I create a ASP.Net Web service application (published locally on localhost) which references the same dll containing the code below. The Web service call to create the port fails with the following error:

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

I tried impersonating, using my own Windows account with admin rights to my own box, within the Web services' Web.config file. The impersonation still gave me the 'Access denied' message.

I am thinking that the account IIS is using to run the class library function does not have the rights to use the Printer configuration services on my box. Is this a possibility?

Any suggestions on how I can get this to work would be helpful?
PRNADMINLib.PrintMasterClass g_objPrntMaster = new PRNADMINLib.PrintMasterClass();
PRNADMINLib.Port g_objPort = new PRNADMINLib.PortClass();
g_objPort.PortName = userPort; //userport = "C:\temp\temporary.prn"
g_objPort.PortType = 3;  
g_objPrntMaster.PortAdd(g_objPort);// fails here by a Web service call

Open in new window

Who is Participating?
mic138Connect With a Mentor Author Commented:
Since I could not get this to work I used another solution. Closiing question.
Nirmalan NagenthiranCommented:
Check  user ASPNET has not the right to access machine
mic138Author Commented:
I believe I discovered the problem but I am still trying to figure out a solution.

If I modify my local machine.config's <processModel> element to <processModel userName="System"> my web service successfully creates the port -- I do not received an 'Access denied' error. Thus the ASP.Net worker process needs to run under a system account in order to get the prnadmin.dll PortAdd function to work.

Unfortunately, this application will be deployed in an production environment containing other running 2.0 Framework Web applications. Hence, this change to the machine.config file is a no no.

How can I achieve these same system privileges at the application level without modifying this <processModel> element in machine.config? Keep in mind, I tried using the <identity impersonate..> setting in my web.config but for even impersonating my own user admin account I still receive a "Access denied" error.

mic138Author Commented:
Another thing which I discovered is when I add the ASPNET user account to the Administrator group (I do realize this is not a wise thing to do), and recycle IIS, my web service successfully creates the port.

I do not want ASPNET to have all the Administrator rights but how can I give this account the necessary permissions to manage printer ports and printers?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.