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

Posted on 2009-04-23
Last Modified: 2013-12-17
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

Question by:mic138
    LVL 12

    Expert Comment

    by:Nirmalan Nagenthiran
    Check  user ASPNET has not the right to access machine

    Author Comment

    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.


    Author Comment

    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?

    Accepted Solution

    Since I could not get this to work I used another solution. Closiing question.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now