# Windows API error 5: “Access is denied” when trying to compile TikZ picture

I installed PGF but are unable to compile the following document:

\documentclass[10pt]{article}
\usepackage{pgf,tikz}
\begin{document}
\begin{tikzpicture}
\draw (0,0) --(1,2);
\end{tikzpicture}
\end{document}


This is the console output:

initexmf.exe: Windows API error 5: Access is denied.

initexmf.exe: Data: C:\
texify.exe: The operation failed for some reason.
texify.exe: Data: C:\Program Files\MiKTeX 2.9\miktex\bin\initexmf.exe


What to do?

-
Welcome to TeX.sx! I improved the title a little to make it more descriptive. This will help people with the same issue to find it. I hope you don't mind. – Martin Scharrer Apr 13 '12 at 17:38
This seems to be an issue with your MikTeX installation rather with PGF/TikZ. Make sure that the output PDF is not already open, e.g. from an earlier compilation run. The Access is denied might be caused by that. Did you tried to reboot and try it again? Under Windows this is always good advice. – Martin Scharrer Apr 13 '12 at 17:41
After looking at docs.miktex.org/manual/initexmf.html, I think this might be because of Windows restrictions. Try to run MikTeX package manager and update the DB, at best using admin rights. Alternative try the initexmf --admin --update-fndb in the console as mentioned in the link. – Martin Scharrer Apr 13 '12 at 17:43

You have to give yourself the permissions to read and write in MiKTeX 2.9 folder.

Go to Program Files and right click MiKTeX 2.9 folder. Then click Properties > Security Tab > Edit Button. Select Users (Bla/Bla) from the list and check all grants.

If that is not enough, click Advanced button on the Security Tab. A window named Advanced Security Settings for Folder Name pops up. Then click Auditing Tab > Continue Button > Add Button. Type Users under "Enter the object names to select" and click OK.

Or, simply don't put your MiKTeX 2.9 folder into Program Files.

-
This solved my problem with installing or updating packages that I had after a fresh install of MiKTeX 2.9 64 bit to Program Files. Thanks! – doncherry Dec 22 '12 at 0:28
I've been looking for hours and hours for this answer! – Jeroen Nov 28 '15 at 18:51

This seems to be some form of conflict between MiKTeX and another software, maybe Adobe Reader.

I looked around and found the following related post with a solution. It is about the installation, not compilation but the reason and fix should be identical:
http://entangled.wordpress.com/2009/09/18/installation-of-miktex-2-8-failed/

A possible solution was posted as a comment there:

I have got the real solution :)
Run regedt32.exe, then navigate to HKEY_CLASSES_ROOT\.pdf
Rightclick .pdf, click permissions, and grant the administrator full rights.

-

I found another potential solution to this problem: make sure that your TMP environment variable points to a writable source. For instance, I changed my TEMP and TMP environment variables (System -> Advanced -> Env. Variables) to a drive other than C:, but I accidentally had the TMP instance point to the D: drive instead of E: -- the former was unwritable. The API Access Error 5 for the MikTeX installation/removal was resolved when I fixed the TMP path.

Hope that helps someone.

-

Apparently you need Administrator rights. What is important is how you installed miktex. If you are asked wether you want to install miktex just for the current user or for all, I reccommend selecting install for all. If you select just for me, it is installed without admin rights, even if the logged in user is in fact an Administrator.

In short: uninstall and install with option "install" for all users" fixed it for me

-
I in opposition recommend for the single user installation, if you’re the only (TeX) user on your system, cf. Difference between administrative and user mode of MiKTeX. – Speravir Dec 26 '12 at 3:20
The default installation directory is program files, which causes trouble when installing in user mode. When one is experienced enough to change it to the %appdata% folder, that could work. Haven't tried it. – Nino Dec 26 '12 at 17:13
1.) The user must have access to his/her own user profile (where %appdata% is in). Otherwise the whole Windows installation would be defunct, so the issue with MiKTeX considered harmless. 2.) The single user installation should be best done outside of C:\Program Files, e.g. put everything LaTeX related into C:\LaTeX. 3.) You should add a link to Relocate MiKTeX 2.9? in your answer. – Speravir Dec 26 '12 at 21:01

I found this error as I was trying to install MiKTeX from the downloaded folder. I copied the file to a new folder and it worked properly.

-

The problem is that in Windows-MiKTeX the following code:

(attributes & FILE_ATTRIBUTE_READONLY) != 0


(search for it in *.cpp files) does not function correctly (e.g. install MikTeX to the program folder and then try to run it without admin rights). After replacing the above code with:

true


the compiled MiKTeK does run without admin rights. Of course, there should be a better solution, replacing the code with something that supplies the correct answer to the read-only question.

-

I got the same error during MiKTeX's installation. To solve this, I right clicked on the downloaded MiKTeX installation file, "Run as administrator", and marked the "Install for all users" box. Everything worked after that.

-

In my case, I closed Kile, simply deleted every file, except the BIB and the TEX files, and re-ran PDFLaTeX. That solved it.

-

I experienced that this problem arrises when a new package is installed and I press 'stop' at the "play/stop" button (to generate a PDF) too soon. When I press too soon the package is not installed correctly and it will not work, giving acces denied. In my case I saved my tex code file in a .txt file and deleted the entire editing program from my program files (Texworks, MIkTex). Make sure to first shut down your computer, because all relating programs have to be closed (best achieved by just shutting down the computer). Then re-install your program again and make sure to not click the "play/stop" button too soon again when a package is installed. It worked for me so I figured why not share it ;)!

-
Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. A suggestion: Do us a favour and change your username to something more telling than "user1234". – Martin Schröder Oct 12 '14 at 12:11

Just wanted to mention a scenario where I got error 5 'access denied'.

I run lualatex from an ASP.NET web site under impersonation. According to this http://support.microsoft.com/en-us/kb/317012 an impersonating user actually uses Default User directory and runs as NETWORK SERVICE. I might be getting it wrong, but the solution is anyway,

to create MikTex folder under Default User\Application Data give NETWORK SERVICE Write/Modify access to that folder. Then I had to give NETWORK SERVICE write/modify access to the folder where the web page dumped the .tex files to be compiled.

Hoping that helps someone.

-

Right click and "Run as an administrator" will resolve the issue.

-
That may have been a "solution" in 2005, but in 2015 that's very bad advice. – Martin Schröder Feb 8 '15 at 1:17