Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Visual J# / May 2005

Tip: Looking for answers? Try searching our database.

running external Dos commands or programs

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dubh - 18 Apr 2005 22:39 GMT
I have come up with a problem which I can't seem to be able to resolve. I
want to be able to map a drive but haven't been able to get the
java.lang.runtime to do this for me. To check where the problem might lie, I
called the notepad program instead. Now when I check the Processes under Task
Manager I find that there is a NOTEPAD.EXE process running but there is no
window for the process nor a corresponding application. Can anyone help me as
I am completely baffled. I'm using what I think is standard java code:

Process p = Runtime.getRuntime().exec(command);

Brian
Lars-Inge Tønnessen [VJ# MVP] - 19 Apr 2005 21:52 GMT
Hello Brian,

Please see my sample for how to connect a network drive, and how to open
notepad in J#.

I'm using the .NET API.

public class Class1
{

/** @attribute System.Runtime.InteropServices.DllImportAttribute("mpr.dll")
*/
public static native System.UInt32 WNetConnectionDialog( System.IntPtr
hwnd, int dwType);

public Class1()
{
 // Connect a network drive
 System.IntPtr pt = new System.IntPtr();
 this.WNetConnectionDialog( pt, 1 );

 // Open Notepad
 System.Diagnostics.Process proc = new System.Diagnostics.Process();
 proc.Start("notepad.exe");
}

/** @attribute System.STAThread() */
public static void main(String[] args)
{
 new Class1();
}
}

Regards,
Lars-Inge Tønnessen
Dubh - 19 Apr 2005 22:27 GMT
Lars,

I tried System.Diagnostics.Process as well and the same thing happened. A
notepad process starts but there is no corresponding window. could it be a
limitation of forms? I'm using a web form!

Brían
Lars-Inge Tønnessen [VJ# MVP] - 19 Apr 2005 23:12 GMT
From a ASP:NET application I would guess you would have to run the
application as a user with more rights than the standard "asp.net user". In
the web.config file do:

<identity impersonate="true" username="login" password="password">

Regards,
Lars-Inge Tønnessen
Dubh - 20 Apr 2005 17:13 GMT
OK, I'll do that but I will do the impersonation in code rather that the
web.config. I didn't get a chance to try the other code you sent yet due to
more pressing *issues*.

Thanks

Brían

> From a ASP:NET application I would guess you would have to run the
> application as a user with more rights than the standard "asp.net user". In
[quoted text clipped - 4 lines]
> Regards,
> Lars-Inge Tønnessen
Dubh - 20 Apr 2005 18:12 GMT
Using the folowing impersonation code:

        System.Security.Principal.WindowsImpersonationContext impersonationContext;
        impersonationContext
            ((System.Security.Principal.WindowsIdentity)get_User().get_Identity()).Impersonate();

//MY CODE HERE

impersonationContext.Undo();

Results are the same. NOTEPAD.EXE process starts but no output. I noticed on
the browser window that the status bar dispays "Downloading..."  and the
progress bar is stuck at 50%.

I will try the other code now.

Brían

> OK, I'll do that but I will do the impersonation in code rather that the
> web.config. I didn't get a chance to try the other code you sent yet due to
[quoted text clipped - 12 lines]
> > Regards,
> > Lars-Inge Tønnessen
Lars-Inge Tønnessen [VJ# MVP] - 20 Apr 2005 21:47 GMT
> Results are the same. NOTEPAD.EXE process starts but no output. I noticed
> on

What kind of output do you expect from Notepad in an ASP.NET application on
a server running as the "ASP.NET" user? (The ASP.NET user is not the logged
on user your using.)

What kind of an application do you build?  What is the goal of your
web-application?

Regards,
Lars-Inge Tønnessen
Dubh - 21 Apr 2005 19:14 GMT
Not looking for output, just using this as a test to check why drive isn't
mapping. I'll try the code you sent and see if that works. My application
needs to give the client the ability to map to a Netware server, and rename a
file in that mapping that another esternal application process checks. That
process then does something depending on the existence or not of the
named/renamed file.
As I have over 250 netware servers I need a web application tha does
monitoring as well as other routine tasks such as this.

Brían

> > Results are the same. NOTEPAD.EXE process starts but no output. I noticed
> > on
[quoted text clipped - 8 lines]
> Regards,
> Lars-Inge Tønnessen
Lars-Inge Tønnessen [VJ# MVP] - 19 Apr 2005 22:58 GMT
I just had to test another solution too. This one can connect the network
drive without showing the dialog window. Please see "example 2" in my code.

package J_MapDriveNotepad;

/** @attribute
System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)
*/
public class structure
{
public int Scope;
public int Type;
public int DisplayType;
public int Usage;
public System.String LocalName;
public System.String RemoteName;
public System.String Comment;
public System.String Provider;
}

public class Class1
{
/** @attribute System.Runtime.InteropServices.DllImportAttribute("mpr.dll")
*/
public static native System.UInt32 WNetAddConnection2A(
 structure lpNetResource,
 System.String lpPassword,
 System.String UserName,
 int dwFlags);

/** @attribute System.Runtime.InteropServices.DllImportAttribute("mpr.dll")
*/
public static native System.UInt32 WNetConnectionDialog( System.IntPtr
hwnd, int dwType);

public Class1()
{
 // EXAMPLE 1
 // Connect a network drive
 System.IntPtr pt = new System.IntPtr();
 this.WNetConnectionDialog( pt, 1 );

 // EXAMPLE 2
 // Connect a network drive without the dialog window
 structure str = new structure();
 str.Type = 1;
 int dwFlags = 1;
 str.LocalName = "Z:";
 str.RemoteName = "\\\\10.0.0.2\\share";
 str.Provider = null;
 int ret = (int)this.WNetAddConnection2A( str, "password", "login",
dwFlags );

  // "login" could be "domain\login" and so on...

 // Open Notepad
 System.Diagnostics.Process proc = new System.Diagnostics.Process();
 proc.Start("notepad.exe");
}

/** @attribute System.STAThread() */
public static void main(String[] args)
{
 new Class1();
}
}

Regards,
Lars-Inge Tønnessen
Dubh - 28 Apr 2005 13:52 GMT
Lars,

this doesn't seem top want to work in j# .NET. I'm getting the following error

@dll.import required for native method

Brían
Lars-Inge Tønnessen [VJ# MVP] - 30 Apr 2005 10:04 GMT
Sorry for late reply. :o)
Did you forget this line ?

** @attribute System.Runtime.InteropServices.DllImportAttribute("mpr.dll")
*/
public static native System.UInt32 WNetConnectionDialog( System.IntPtr
hwnd, int dwType);

Regards,
Lars-Inge Tønnessen
Lars-Inge Tønnessen [VJ# MVP] - 30 Apr 2005 10:10 GMT
Sorry... I ment this line:

/** @attribute System.Runtime.InteropServices.DllImportAttribute("mpr.dll")
*/
public static native System.UInt32 WNetAddConnection2A(
 structure lpNetResource,
 System.String lpPassword,
 System.String UserName,
 int dwFlags);

And this struct/structure:

/** @attribute
System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)
*/
public class structure
{
public int Scope;
public int Type;
public int DisplayType;
public int Usage;
public System.String LocalName;
public System.String RemoteName;
public System.String Comment;
public System.String Provider;
}

Regards,
Lars-Inge Tønnessen
Dubh - 06 May 2005 20:32 GMT
Lars,

I just can't get this to work. I want to be able to do it from a web form
where the server name is entered in a text box and then a submit button maps
the drive. I then need to be able to rename a file in that drive mapping. Can
you recommend a good J#.NET book that might be of help?

Thanks

Brian

> Sorry... I ment this line:
>
[quoted text clipped - 25 lines]
> Regards,
> Lars-Inge Tønnessen
Lars-Inge Tønnessen [VJ# MVP] - 07 May 2005 11:49 GMT
Hi Brian,

This J# ASP.NET web app is working for me.

Please try to trace the returned value from the :
int ret = (int)this.WNetAddConnection2A( str, "password", "myUsername",
dwFlags );

And look it up in this error code list:
http://msdn.microsoft.com/library/en-us/debug/base/system_error_codes.asp

What error (code) do you get?

Sorry, I don't know of any J# books describing anything like this. This MSDN
page is probably the closest to any docs on the drive map api we get.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wnet/wnet/wneta
ddconnection2.asp


The  DllImportAttribute("mpr.dll") is P/Invoke:

http://msdn.microsoft.com/msdnmag/issues/03/07/NET

The "WebForm1.aspx.jsl" fil:

package MapDrive;

import System.Collections.*;
import System.ComponentModel.*;
import System.Data.*;
import System.Drawing.*;
import System.Web.*;
import System.Web.SessionState.*;
import System.Web.UI.*;
import System.Web.UI.WebControls.*;
import System.Web.UI.HtmlControls.*;

public class WebForm1 extends System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox t_server;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.ListBox list_files;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.TextBox t_newFileName;
protected System.Web.UI.WebControls.Button Button_renameFile;
protected System.Web.UI.WebControls.Button Button_mapDrive;

private void Page_Load(Object sender, System.EventArgs e)
{
 // Put user code to initialize the page here
}

#region Web Form Designer generated code
protected void OnInit(System.EventArgs e)
{
 //
 // CODEGEN: This call is required by the ASP.NET Web Form Designer. Donot
remove this.
 //
 InitializeComponent();
 super.OnInit(e);
}

private void InitializeComponent()
{
 this.Button_mapDrive.add_Click( new
System.EventHandler(this.Button_mapDrive_Click) );
 this.add_Load( new System.EventHandler(this.Page_Load) );

}
#endregion

/** @attribute System.Runtime.InteropServices.DllImportAttribute("mpr.dll")
*/
public static native System.UInt32 WNetAddConnection2A(
 structure lpNetResource,
 System.String lpPassword,
 System.String UserName,
 int dwFlags);

/**
 * Maps the drive and lists the folder.
 */
private void Button_mapDrive_Click (Object sender, System.EventArgs e)
{
 this.list_files.get_Items().Clear();

 structure str = new structure();
 str.Type = 1;
 int dwFlags = 1;
 str.LocalName = "Z:";
 str.RemoteName = this.t_server.get_Text();
 str.Provider = null;
 int ret = (int)this.WNetAddConnection2A( str, "password", "myUsername",
dwFlags );

 this.Label1.set_Text( "" + ret );

 System.IO.DirectoryInfo  dir = new System.IO.DirectoryInfo("Z:\\");
 System.IO.FileInfo[] files = dir.GetFiles();

 for ( int counter = 0; counter < files.length; counter++ )
 {
  System.Web.UI.WebControls.ListItem _li = new
System.Web.UI.WebControls.ListItem();
  _li.set_Text( files[counter].get_Name() );
  this.list_files.get_Items().Add( _li );
 }

}

}

/** @attribute
System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)
*/
public class structure
{
public int Scope;
public int Type;
public int DisplayType;
public int Usage;
public System.String LocalName;
public System.String RemoteName;
public System.String Comment;
public System.String Provider;
}

The "WebForm1.aspx" file:
\\10.0.0.2\ShareServer and map point: (\\10.0.0.2\share)
<%@ Page language="VJ#" Codebehind="WebForm1.aspx.jsl"
AutoEventWireup="false" Inherits="MapDrive.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
 <title>WebForm1</title>
 <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
 <meta content="VJ#" name="CODE_LANGUAGE">
 <meta content="JavaScript" name="vs_defaultClientScript">
 <meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
 <form id="Form1" method="post" runat="server">
  <asp:textbox id="t_server" style="Z-INDEX: 101; LEFT: 32px; POSITION:
absolute; TOP: 80px" runat="server"
   Width="384px">\\10.0.0.2\Share</asp:textbox><asp:label id="Label1"
style="Z-INDEX: 102; LEFT: 32px; POSITION: absolute; TOP: 56px"
runat="server"
   Width="376px">Server and map point:
(\\10.0.0.2\share)</asp:label><asp:listbox id="list_files" style="Z-INDEX:
103; LEFT: 32px; POSITION: absolute; TOP: 184px"
   runat="server" Width="384px" Height="296px"></asp:listbox><asp:button
id="Button_mapDrive" style="Z-INDEX: 108; LEFT: 32px; POSITION: absolute;
TOP: 112px"
   runat="server" Text="Map drive"></asp:button></form>
</body>
</HTML>

Best Regards,
Lars-Inge Tønnessen
Dubh - 10 May 2005 15:50 GMT
I was hoping to reply with a positive result today after some testing
yesterday. Here were my results yesterday afternoon.

Mapping is working. However no matter which server name I put in after the
initial mapping I get the same listing in the listbox. I added some code to
clear this but the same still happens. I also noticed that there is no
corresponding z: mapping showing in "Explorer".

This morning I am back to square one.  Stack trace as follows.

[DirectoryNotFoundException: Could not find a part of the path "Z:\".]
  System.IO.__Error.WinIOError(Int32 errorCode, String str) +287
  System.IO.Directory.InternalGetFileDirectoryNames(String fullPath, String
userPath, Boolean file) +229
  System.IO.Directory.InternalGetFiles(String path, String userPath, String
searchPattern) +24
  System.IO.DirectoryInfo.GetFiles(String searchPattern) +378
  System.IO.DirectoryInfo.GetFiles() +13
  NWportal.MapDrive.Button_mapDrive_Click(Object sender, EventArgs e) in
E:\Inetpub\wwwroot\NWportal\MapDrive.aspx.jsl:82
  System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
 
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
  System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +18
  System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
  System.Web.UI.Page.ProcessRequestMain() +1277


I did some trouble shooting by substituting this line:

System.IO.DirectoryInfo  dir = new System.IO.DirectoryInfo("Z:\\");

with

System.IO.DirectoryInfo  dir = new System.IO.DirectoryInfo(any local drive);

and I get a listing for that drive. However if I manually map to Z: or any
other letter regardless of th eOS of the target map I get the error listed
above in the stack trace. Strange. I also loaded the "Remote.Name" string
manually with the share and still no joy although I'm not surprised as the
manually mapped letter didn't work also.

It seems to be that the application can read local drives but not network
drives. I'll do some more troubleshooting to see if I can pin down the
problem. I'ts interesting that it worked (to a fashion) yesterday for a
while. I rebooted the server since and thta is the only thing changed.

Brian

> Hi Brian,
>
[quoted text clipped - 157 lines]
> Best Regards,
> Lars-Inge Tønnessen
Dubh - 11 May 2005 09:30 GMT
I managed to get a directory listing again.

First of all I confirmed that the only way to list a remote share was by
inserting  some impersonation code. However I alos had to sue the full path
instead of a drive mapping letter. It remains to be seen whether I will still
be able to rename a file using the UNC but I think I will.

Heres the snippet of code:

    System.Security.Principal.WindowsImpersonationContext impersonationContext;
    impersonationContext =
    ((System.Security.Principal.WindowsIdentity)get_User().get_Identity()).Impersonate();

this.list_files.get_Items().Clear();

structure str = new structure();
str.Type = 1;
int dwFlags = 1;
str.LocalName = "Z:";
str.RemoteName = this.t_server.get_Text();
str.Provider = null;
int ret = (int)this.WNetAddConnection2A( str, "myPassword",
".username.context",
dwFlags );

   
this.Label1.set_Text( "" + ret );

System.IO.DirectoryInfo  dir = new
System.IO.DirectoryInfo(t_server.get_Text());
Dubh - 16 May 2005 21:36 GMT
Lars,

You're a genius. I finally solved it and it was staring me in the face all
along. I changed the following piece:

str.Provider = null;

to

str.Provider = "NetWare";

and suddenly everything worked beautifully. I had narrowed the problem down
as I thought to ASP.NET security as running from a browser on the hosting
server would work but not on a remote client. I tried everything in security
to no avail. Then I tried to change the provider order in the advanced
settings of network properties but that didn't work either. Finally I decided
to include the NetWare string in the above parameter fully expecting it to
fail and that it couldn't be that simple! Wow.

Thanks for all the help. THe links you sent were also excellent as I
couldn't track these down myself either.

Brian

> Hi Brian,
>
[quoted text clipped - 157 lines]
> Best Regards,
> Lars-Inge Tønnessen
Lars-Inge Tønnessen [VJ# MVP] - 18 May 2005 17:48 GMT
YEEAAHHH Brian!!

I have been out of town for a few days. Extremely good news you have for
us!!!!  =:o)

Best Regards,
Lars-Inge Tønnessen

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.