473,811 Members | 2,981 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("msvc rt.dll", SetLastError=tr ue)]
static extern int _mkdir(string path);

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

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

return new DirectoryInfo(s Path);
}
thx a million

Sep 12 '06 #1
10 2325
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.co m

<A_*********@ho tmail.comwrote in message
news:11******** **************@ p79g2000cwp.goo glegroups.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("msvc rt.dll", SetLastError=tr ue)]
static extern int _mkdir(string path);

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

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

return new DirectoryInfo(s Path);
}
thx a million

Sep 12 '06 #2
A_*********@hot mail.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.Creat eDirectory but allow usage
of msvcrt.dll - it does not make any sense.

Arne
Sep 12 '06 #3
<A_*********@ho tmail.comwrote in message
news:11******** **************@ p79g2000cwp.goo glegroups.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
misconfiguratio n 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.co m

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 'DirectoryNotFo undException - 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.Creat eDirectory(path ) and
DirectoryInfo.C reateSubdirecto ry(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.Creat eDirectory() 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.Creat eDirectory 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_*********@ho tmail.comwrote in message
news:11******** **************@ p79g2000cwp.goo glegroups.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
misconfiguratio n 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.Direc toryInfo oDI = new
DirectoryInfo(@ "D:\WWWRoot\our domain.com");
ShowResult("Dir ectoryInfo succeeded");
System.IO.Direc tory.CreateDire ctory(@"D:\WWWR oot\ourdomain.c om\stclaire");
ShowResult("Cre ateDirectory succeeded");
}
catch(Exception ex)
{
ShowResult(ex.M essage + "<BR><BR>" + ex.StackTrace, true);
}

I ran this page on our hosted server and got a
DirectoryNotFou ndException. 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_*********@ho tmail.comwrote in message
news:11******** *************@i 3g2000cwc.googl egroups.com...
| Ignacio Machin ( .NET/ C# MVP ) wrote:
| Hi,
| >
| I find extremely weird that. In case it's true it sounds more like a
| misconfiguratio n 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.Direc toryInfo oDI = new
| DirectoryInfo(@ "D:\WWWRoot\our domain.com");
| ShowResult("Dir ectoryInfo succeeded");
| System.IO.Direc tory.CreateDire ctory(@"D:\WWWR oot\ourdomain.c om\stclaire");
| ShowResult("Cre ateDirectory succeeded");
| }
| catch(Exception ex)
| {
| ShowResult(ex.M essage + "<BR><BR>" + ex.StackTrace, true);
| }
|
| I ran this page on our hosted server and got a
| DirectoryNotFou ndException. 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

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

Similar topics

15
4469
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 application runs flawlessly, but once I call the class from inside a c# windows application the program freezes, crashes etc... there must be a way to make this work inside a windows app as it run great as a command line app.
9
4976
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 the PPC is located in coredll.dll and on the desktop in kernel32.dll. I could make two versions of the DllImported funktions but then I would have to ask for the OS version in every call to the function. This however causes the code to look messy...
3
3463
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 have the same signature, and I never have to call more than one DLL in the same program. (The DLLs parse and validate 5 different binary formats.) Originally I was expecting to have 5 different C# programs as the constants are different for each...
1
1726
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 use it with DllImport in a C# console application. Basically I just access main() and pass my parameters. w00t! RAM used by the C# app is now 228 MB !
2
10483
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 regsvr32) and uses Assembler to make some math in SSE. The result is given out as an Int. Using a C# console app (code below) it work fine & fast.
4
2856
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 want to do (fast) graphic output of the results. I used to use C. I want to upgrade my computer. Do I get dual core? Does C# support dual-core? Is C# as fast as the old C? Is there a new C? (or is C# the new version of C?) Is Visual Studio...
2
3672
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. But I have some confusion about them. 1. Is the "_declspec(dllimport)" also one of the forms of P/Invoke? 2. From performance aspect, which way is more better? 3. What's the difference of them.
1
4283
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 with. I am trying to use SWIG to write my Perl mod with those files but I getting a bunch of errors with the "extern "C" __declspec(dllimport)" command kinda know that basics of the command, its used to declare vars from a .dll. If im wrong please...
1
5801
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 code: #define USERINT_FUNC __cdecl #ifdef __cplusplus extern "C" { #endif
0
9734
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10416
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9215
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7676
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5567
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5702
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4357
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3881
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3028
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.