CreateFile in unmanaged code fails

U

Udo Hoerhold

Hi. I'm calling some legacy unmanaged code from a web service (but I can
duplicate the same problem from an IHttpModule). The unmanaged code calls
CreateFile to open an existing file. CreateFile always fails with LastError
== 5 (ERROR_ACCESS_DENIED).

If I try to call CreateFile directly from the web service, like this:
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr CreateFile(string lpFileName, uint
dwDesiredAccess,
uint dwShareMode, IntPtr lpSecurityAttributes, uint
dwCreationDisposition,
uint dwFlagsAndAttributes, IntPtr hTemplateFile);

IntPtr hFile = CreateFile( "c:\\tempdb\\file.txt",
0x80000000,
0,
IntPtr.Zero,
3,
0,
IntPtr.Zero );

This works fine.

Calling fopen() to open the file from the unmanaged code also works. Only
CreateFile in the unmanaged code fails.

I've tried adding the ASPNET user to the Administrators group. I've also
tried adding:
<processModel autoConfig="true"
userName="Administrator"
password="xxxxxx"/>
to the machine.config file.

Does anyone know why this particular scenario fails? I'm assuming it's a
permissions problem because of the ACCESS_DENIED error, but none of the
solutions for access problems are working, and it's strange that fopen() will
succeed when CreateFile() fails.

Anyone have any ideas?

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top