473,385 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Advice On DllImport

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

Sep 12 '06 #1
10 2286
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 Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<A_*********@hotmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
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

Sep 12 '06 #2
A_*********@hotmail.com wrote:
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
Sep 12 '06 #3
<A_*********@hotmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
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
Sep 12 '06 #4
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
Sep 12 '06 #5
Nicholas Paldino [.NET/C# MVP] wrote:
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 Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

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.

Sep 12 '06 #6
Arne Vajhøj wrote:
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.

Sep 12 '06 #7

Michael C wrote:
<A_*********@hotmail.comwrote in message
news:11**********************@p79g2000cwp.googlegr oups.com...
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
thx for your answers.

Sep 12 '06 #8
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\o urdomain.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.

Sep 12 '06 #9
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.

<A_*********@hotmail.comwrote in message
news:11*********************@i3g2000cwc.googlegrou ps.com...
| 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\o urdomain.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.
|
Sep 12 '06 #10
Hi,
{
System.IO.DirectoryInfo oDI = new
DirectoryInfo(@"D:\WWWRoot\ourdomain.com");
ShowResult("DirectoryInfo succeeded");
System.IO.Directory.CreateDirectory(@"D:\WWWRoot\o urdomain.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");
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Sep 12 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Jim | last post by:
I am extremely frustrated. I am building c# application for a call center and am using a third party API to access some hardware. When develop and test my class using the windows console the...
9
by: Ole Christensen | last post by:
I'm trying to make a sort of conditional compilation in my C# code because my app is intended to run on both a Pocket PC and on a normal desktop PC. My code uses a call to an API function that on...
3
by: Mark Jerde | last post by:
I'm sill learning VS .NET 2003, not an expert yet. I'm calling an unmanaged C++ DLL from C# using . When the whole project is done I will be calling a total of 5 C++ DLLs from C#. All the DLLs...
1
by: Brian Anderson | last post by:
Hello, I have a native, C++ console app that uses ~26MB of RAM when it runs. It uses quite a lot of RAM to make some math but will never xceed 26MB. Now I've made a dll out of this code and...
2
by: Brian Anderson | last post by:
Hello, is it possible to use DllImport to call a DLL in ASP.NET ? Or is it necessarry that my DLL has to be copied into \System32 ? My DLL is a native C++ 7.1 DLL (not managed, no COM, no...
4
by: Web_PDE_Eric | last post by:
I don't know where to go, or what to buy, so plz re-direct me if I'm in the wrong place. I want to do high performance integration of partial differential eqns in n dimensions (n=0,1,2,3..etc) I...
2
by: Ed | last post by:
Hello, dear all, I often see these two import usage in the code. Both are the interface to use the Dll library. I think they are the same. Normally P/Invoke means using the to import the dll....
1
by: kardon33 | last post by:
Let me explain my problem, Im currently trying to use a Perl module that was built for a Windows OS that uses a .dll and .lib file. I have obtained the c header files that the modules were built...
1
by: elke | last post by:
Hi, I want to use an unmanaged dll in C# .net and I'm having some troubles witch a function that should return an array. I'm new at this, so I don't know what I'm doing wrong. Here is some...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.