Advice On DllImport

  • Thread starter Thread starter A_StClaire_
  • Start date Start date
A

A_StClaire_

hi guys,

I am working on a C# app that needs to create directories on the fly.

security settings enforced by our ISP do not allow us to use the
standard .NET calls, so I am forced to resort to a DllImport of the COM
library 'msvcrt.dll'. however I am not familiar with calling COM
objects so I have questions...

1) how does the "destroy every COM object you create" rule apply here?
with the code below am I creating something that needs to be
destroyed?

2) if this function is called often what are the performance
consequences?


[DllImport("msvcrt.dll", SetLastError=true)]
static extern int _mkdir(string path);

/// <summary>
/// This function should provide safe substitude for
Directory.CreateDirectory()
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
static DirectoryInfo CreateDirectory(string sPath)
{
int iCode = _mkdir(sPath);

if (iCode != 0)
{
throw new ApplicationException("Error calling [msvcrt.dll]:_mkdir(" +
sPath + "), error code: " + iCode.ToString());
}

return new DirectoryInfo(sPath);
}


thx a million
 
What do you mean that your ISP doesn't allow these calls? Are you
running this on ASP.NET? If this is the case, then there is no way that
these calls are going to work either.

It most likely is a permissions issue, not you accessing parts of the
framework that are supposedly "off-limits".
 
I am working on a C# app that needs to create directories on the fly.

security settings enforced by our ISP do not allow us to use the
standard .NET calls, so I am forced to resort to a DllImport of the COM
library 'msvcrt.dll'. however I am not familiar with calling COM
objects so I have questions...

1) how does the "destroy every COM object you create" rule apply here?
with the code below am I creating something that needs to be
destroyed?

2) if this function is called often what are the performance
consequences?

msvcrt.dll is not a COM DLL but a Win32 DLL.

I find it hard to believe that your ISP does not
allow Directory.CreateDirectory but allow usage
of msvcrt.dll - it does not make any sense.

Arne
 
hi guys,

I am working on a C# app that needs to create directories on the fly.

security settings enforced by our ISP do not allow us to use the
standard .NET calls, so I am forced to resort to a DllImport of the COM
library 'msvcrt.dll'. however I am not familiar with calling COM
objects so I have questions...

1) how does the "destroy every COM object you create" rule apply here?
with the code below am I creating something that needs to be
destroyed?

You are not creating a COM object here so there is nothing to destroy.
2) if this function is called often what are the performance
consequences?

None. But as other's have said I'd imagine this aint gunna work either.

Michael
 
Hi,

I find extremely weird that. In case it's true it sounds more like a
misconfiguration than anything else.

Why don't you post the code that is giving you error?
 
Nicholas said:
What do you mean that your ISP doesn't allow these calls? Are you
running this on ASP.NET? If this is the case, then there is no way that
these calls are going to work either.

It most likely is a permissions issue, not you accessing parts of the
framework that are supposedly "off-limits".


Nicholas,

I used MapPath() to find the root directory of our webspace, then
uploaded a webpage that attempted a simple CreateDirectory() there.
that produced a 'DirectoryNotFoundException - Could not find a part of
the path...' message.

when contacted our service provider actually pointed me to a "common
workaround" mentioned often on the net:

"This is a relatively known .NET bug or "feature"...

Both Directory.CreateDirectory(path) and
DirectoryInfo.CreateSubdirectory(path) require user to have Read access
to
the drive's root directory (i.e. <Drive>:\).

Many ASP.NET hosting providers (especially those running Windows 2003
Server) will not allow user running ASP.NET working process read access
to
the root folder, so CreateDirectory will always fail. You can not blame
hosting providers - they do right thing, securing shared environment
from
users with malicious intents.

The only workaround I have found is to replace call to
Directory.CreateDirectory() with call to unmanaged code, like msvcrt's
_mkdir(char*):"

I then implemented this and testing confirms it works.
 
Arne said:
msvcrt.dll is not a COM DLL but a Win32 DLL.

sorry, I guess you are right.
I find it hard to believe that your ISP does not
allow Directory.CreateDirectory but allow usage
of msvcrt.dll - it does not make any sense.

Arne

yes I was somewhat surprised to find the DllImport was allowed, but it
was.
 
Michael said:
You are not creating a COM object here so there is nothing to destroy.


None. But as other's have said I'd imagine this aint gunna work either.

Michael

thx for your answers.
 
Ignacio said:
Hi,

I find extremely weird that. In case it's true it sounds more like a
misconfiguration than anything else.

Why don't you post the code that is giving you error?




--

I did this, Ignacio:

try
{
System.IO.DirectoryInfo oDI = new
DirectoryInfo(@"D:\WWWRoot\ourdomain.com");
ShowResult("DirectoryInfo succeeded");
System.IO.Directory.CreateDirectory(@"D:\WWWRoot\ourdomain.com\stclaire");
ShowResult("CreateDirectory succeeded");
}
catch(Exception ex)
{
ShowResult(ex.Message + "<BR><BR>" + ex.StackTrace, true);
}

I ran this page on our hosted server and got a
DirectoryNotFoundException. switching code lines allowed me to verify
that both the DirectoryInfo() constructor and the CreateDirectory()
method result in this same exception.

the only thing ShowResult() does is display text on a label.
 
Whatever method you use (calling the framework class methods or DllImport
functions) you won't be able to create directories if your ISP has set an
ACL that prohibits this from your asp account.

Willy.

| Ignacio Machin ( .NET/ C# MVP ) wrote:
| > Hi,
| >
| > I find extremely weird that. In case it's true it sounds more like a
| > misconfiguration than anything else.
| >
| > Why don't you post the code that is giving you error?
| >
| >
| >
| >
| > --
| > --
| > Ignacio Machin,
| > ignacio.machin AT dot.state.fl.us
| > Florida Department Of Transportation
|
| I did this, Ignacio:
|
| try
| {
| System.IO.DirectoryInfo oDI = new
| DirectoryInfo(@"D:\WWWRoot\ourdomain.com");
| ShowResult("DirectoryInfo succeeded");
| System.IO.Directory.CreateDirectory(@"D:\WWWRoot\ourdomain.com\stclaire");
| ShowResult("CreateDirectory succeeded");
| }
| catch(Exception ex)
| {
| ShowResult(ex.Message + "<BR><BR>" + ex.StackTrace, true);
| }
|
| I ran this page on our hosted server and got a
| DirectoryNotFoundException. switching code lines allowed me to verify
| that both the DirectoryInfo() constructor and the CreateDirectory()
| method result in this same exception.
|
| the only thing ShowResult() does is display text on a label.
|
 
Hi,
{
System.IO.DirectoryInfo oDI = new
DirectoryInfo(@"D:\WWWRoot\ourdomain.com");
ShowResult("DirectoryInfo succeeded");
System.IO.Directory.CreateDirectory(@"D:\WWWRoot\ourdomain.com\stclaire");
ShowResult("CreateDirectory succeeded");
}
catch(Exception ex)
{
ShowResult(ex.Message + "<BR><BR>" + ex.StackTrace, true);
}

That code is not a good code by far, you are assuming that the website sits
in D:\WWWRoot\ourdomain.com\stclaire which is unlikely

instead use something like


System.IO.DirectoryInfo oDI = new
DirectoryInfo( Server.MapPath( "/") ) ; // change the root to where you
prefer
ShowResult("DirectoryInfo succeeded");
System.IO.Directory.CreateDirectory( Server.MapPath( "/") + @"\stclaire");
 
Back
Top