Run command line program silently with c# on wince - c#

I once again need some help.
I'm using the .net Compact Framework and the programming language C# to develop for mobile devices that are running WinCE 5.0.
What I want to accomplish is to programmatically mount a network drive. To do so, the app runs the following code in a background thread:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "net";
startInfo.UseShellExecute = false;
startInfo.Arguments = #"use logs \\server\logs /user:dom\uname /password:pw";
Process p = Process.Start(startInfo);
p.WaitForExit(5000);
Now my problem is, that this code will display a console in the foreground and writes the command to it and the answer from the command as well. Also, the console won't disappear anymore.
The parameter 'UseShellExecute' doesn't seem to show any effect.
I've read about the parameter 'CreateNoWindow', but it doesn't exist in the compact framework.
So folks, is there a possibility to run net-commands in the background, the user shouldn't notice and certainly not see the command including the password in plain text.
I hope you get the idea.
Many thanks in advance
Toby

Thank you very much Shaihi,
you set me on the right track.
The code and links you provided got me finally to the following solution that works fine for me:
[DllImport("coredll.dll")]
private static extern int WNetAddConnection3(IntPtr hWndOwner,
ref NetResource lpNetResource, string lpPassword, string lpUserName, int dwFlags);
[DllImport("coredll.dll")]
static extern int WNetCancelConnection2(string lpName, Int32 dwFlags, bool bForce);
...
try
{
NetResource logsResource = new NetResource();
logsResource.lpLocalName = "logs";
logsResource.lpRemoteName = #"\\server\logs";
logsResource.dwType = 0x1; //const int RESOURCETYPE_DISK = 0x1
logsResource.dwScope = 0;
logsResource.dwUsage = 0;
logsResource.dwDisplayType = 0;
//try to connect the network resource
WNetAddConnection3(new IntPtr(0), ref logsResource, #"pass", #"dom\user", 0);
//copy files to the server
string[] logfiles = Directory.GetFiles(#"\System\Logs\");
foreach (string logfile in logfiles)
{
File.Copy(logfile, #"\network\logs\" +
logfile.Substring(logfile.LastIndexOf(#"\") + 1), true);
}
}
catch
{
}
finally
{
//try to disconnect network resource
WNetCancelConnection2("logs", 0, false);
}
The two WNET function calls return an integer value. If this value equals to 0 the operation finished successfully. Common codes I experienced are 53 and 85. Refer to this list to get a clue what the numbers mean!

You can use WNetAddConnetion3 by P/Invoking it (here is the declaration).Here is the declaration for the NetResource struct:
[StructLayout(LayoutKind.Sequential)]
internal struct NetResource
{
public uint dwScope;
public uint dwType;
public uint dwDisplayType;
public uint dwUsage;
[MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)]
public string lpLocalName;
[MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)]
public string lpRemoteName;
[MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)]
public string lpComment;
[MarshalAs(UnmanagedType.LPWStr, SizeConst = 64)]
public string lpProvider;
}

Create a Windows Form Application instead of Console Application and replace all the code in Main method of program.cs with
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "net";
startInfo.UseShellExecute = false;
startInfo.Arguments = #"use logs \\server\logs /user:dom\uname /password:pw";
Process p = Process.Start(startInfo);
p.WaitForExit(5000);
Delete Form1.cs

Related

get data from printer local port

I have done a virtual printer by using some sample code. But I don't know how to get data from it when it starts printing.
When it starts printing It gives notification that ERROR IN PRITNING.
And Why there is "C:/MyLocalPort.txt".
Anyone Help me to get data this virtual printer sets to print.
Here is the Sample Code:
using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Management;
namespace virtualPrinter
{
public static class PrinterClass // class which carries SetDefaultPrinter function
{
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool SetDefaultPrinter(string Printer);
}
class myPrinterClass
{
public static void getPrinterNames()
{
foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
{
MessageBox.Show(printer);
}
}
public static void installPrinter(string printerName) //works on win 7,8,8.1,10 on both x84 and x64
{
Winspool.AddLocalPort(#"C:\MyLocalPort.txt");
//https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/rundll32-printui
// /if Installs a printer by using an .inf file.
// /b[name] Specifies the base printer name.
// /#[file] Specifies a command-line argument file and directly inserts the text in that file into the command line.
// /f[file] Species the Universal Naming Convention (UNC) path and name of the .inf file name or the output file name, depending on the task that you are performing. Use /F[file] to specify a dependent .inf file.
// /r[port] Specifies the port name.
// /m[model] Specifies the driver model name. (This value can be specified in the .inf file.)
string arg;
arg = "printui.dll , PrintUIEntry /if /b " + "\"" + printerName + "\"" + #" /f C:\Windows\inf\ntprint.inf /r " + "\"" + #"C:\MyLocalPort.txt" + "\"" + " /m " + "\"" + "Generic / Text Only" + "\""; //initial arg
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = "rundll32.exe";
p.Arguments = arg;
p.WindowStyle = ProcessWindowStyle.Hidden;
try
{
Process.Start(p);
MessageBox.Show(printerName + " installed succesfully!");
}
catch (Exception ex)
{
MessageBox.Show("Something went wrong. Try again!" );
}
}
public static bool printerExists(string printerName)
{
bool res = false;
foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
{
if (printer == printerName)
{
res = true;
}
}
return res;
}
public static void uninstallPrinter(string printerName)
{
string arg;
ProcessStartInfo p = new ProcessStartInfo();
arg = "printui.dll, PrintUIEntry /dl /n " + "\"" + printerName + "\"";
if (printerExists(printerName))
{
p.FileName = "rundll32.exe";
p.Arguments = arg;
p.WindowStyle = ProcessWindowStyle.Hidden;
try
{
Process.Start(p);
MessageBox.Show(printerName + " unistalled successfully");
}
catch (Exception ex)
{
MessageBox.Show(ex.InnerException.ToString());
}
p = null;
}
}
public static string GetLocalIPAddress() //erxomeno feature
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
throw new Exception("No network adapters with an IPv4 address in the system!");
}
public static class Winspool
{
[StructLayout(LayoutKind.Sequential)]
private class PRINTER_DEFAULTS
{
public string pDatatype;
public IntPtr pDevMode;
public int DesiredAccess;
}
[DllImport("winspool.drv", EntryPoint = "XcvDataW", SetLastError = true)]
private static extern bool XcvData(
IntPtr hXcv,
[MarshalAs(UnmanagedType.LPWStr)] string pszDataName,
IntPtr pInputData,
uint cbInputData,
IntPtr pOutputData,
uint cbOutputData,
out uint pcbOutputNeeded,
out uint pwdStatus);
[DllImport("winspool.drv", EntryPoint = "OpenPrinterA", SetLastError = true)]
private static extern int OpenPrinter(
string pPrinterName,
ref IntPtr phPrinter,
PRINTER_DEFAULTS pDefault);
[DllImport("winspool.drv", EntryPoint = "ClosePrinter")]
private static extern int ClosePrinter(IntPtr hPrinter);
public static int AddLocalPort(string portName)
{
PRINTER_DEFAULTS def = new PRINTER_DEFAULTS();
def.pDatatype = null;
def.pDevMode = IntPtr.Zero;
def.DesiredAccess = 1; //Server Access Administer
IntPtr hPrinter = IntPtr.Zero;
int n = OpenPrinter(",XcvMonitor Local Port", ref hPrinter, def);
if (n == 0)
return Marshal.GetLastWin32Error();
if (!portName.EndsWith("\0"))
portName += "\0"; // Must be a null terminated string
// Must get the size in bytes. Rememeber .NET strings are formed by 2-byte characters
uint size = (uint)(portName.Length * 2);
// Alloc memory in HGlobal to set the portName
IntPtr portPtr = Marshal.AllocHGlobal((int)size);
Marshal.Copy(portName.ToCharArray(), 0, portPtr, portName.Length);
uint needed; // Not that needed in fact...
uint xcvResult; // Will receive de result here
XcvData(hPrinter, "AddPort", portPtr, size, IntPtr.Zero, 0, out needed, out xcvResult);
ClosePrinter(hPrinter);
Marshal.FreeHGlobal(portPtr);
return (int)xcvResult;
}
}
}
}
Ok, I Can't do this that way.
First I have to create a port monitor I have used mfilemon.dll
Then I created a port with that port monitor
Then Pasted All Driver files in System Driver Dictionary.
Then finally Added Printer And Done.
You can refer this
https://github.com/Gohulan/Virtual-Printer/blob/main/PrinterSetup/SpoolerHelper.cs
This contains all the steps to create a virtual printer to make a print to pdf virtual printer

Error when exporting data to excel in c# (win forms)

When trying to export data from DataGridView to excel System.ComponentModel.Win32Exception: 'The specified executable is not a valid application for this OS platform.' exception is thrown, funny thing is that file is saved like supposed to.
I fought that this is because I reinstalled packages for entire solution, but it's not the case.
Exception occurs when trying to call Process.Start(fileName) method, where the value of fileName is: C:\\Users\\net\\Desktop\\Excel TESTING\\OperatorStatisticsData.xlsx
Note that my application is running on any CPU( 32 or 64 bit), and I am currently running win10 64 bit operating system.
Any suggestion how to fix this?
Try to call EXCEL.EXE directly (change path if need to):
string filePath = #"c:\Temp\export.XLSX";
// For me this generates: C:\\Program Files\\Microsoft Office\\Office14\\EXCEL.EXE
var exec = System.IO.Path.Combine(
Environment.GetEnvironmentVariable("ProgramW6432"),
"Microsoft Office", "Office14", "EXCEL.EXE");
Process.Start(fileName: exec, arguments: filePath);
UPDATE
You can obtain executable file by using Shell AssocQueryString function, which retrieves (amongst other properties) executable file associated with extension. Here's the method on C# which makes use of it:
[Flags]
public enum AssocF
{
Init_NoRemapCLSID = 0x1,
Init_ByExeName = 0x2,
Open_ByExeName = 0x2,
Init_DefaultToStar = 0x4,
Init_DefaultToFolder = 0x8,
NoUserSettings = 0x10,
NoTruncate = 0x20,
Verify = 0x40,
RemapRunDll = 0x80,
NoFixUps = 0x100,
IgnoreBaseClass = 0x200
}
public enum AssocStr
{
Command = 1,
Executable,
FriendlyDocName,
FriendlyAppName,
NoOpen,
ShellNewValue,
DDECommand,
DDEIfExec,
DDEApplication,
DDETopic
}
public static class FileAssocHelper
{
[DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern uint AssocQueryString(AssocF flags, AssocStr str, string pszAssoc, string pszExtra, [Out] StringBuilder pszOut, [In][Out] ref uint pcchOut);
public static string FileExtensionInfo(AssocStr assocStr, string doctype)
{
uint pcchOut = 0;
AssocQueryString(AssocF.Verify, assocStr, doctype, null, null, ref pcchOut);
StringBuilder pszOut = new StringBuilder((int)pcchOut);
AssocQueryString(AssocF.Verify, assocStr, doctype, null, pszOut, ref pcchOut);
return pszOut.ToString();
}
}
Now we can retrieve executable file and open Excel file:
string filePath = #"c:\Temp\Results.xlsx";
string exec = FileAssocHelper.FileExtensionInfo(AssocStr.Executable, ".xlsx");
Process.Start(exec, filePath);

Get icon from Process List instead of File

How would I be able to extract the icon from processlist instead of filenames as of currently? As of now the this works by opening Form Dialog, they click on a file, then it adds it into listView with icon. How to do I just get the processes icon's and display them int he listView?
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
public IntPtr hIcon;
public IntPtr iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
class Win32
{
public const uint SHGFI_ICON = 0x100;
public const uint SHGFI_LARGEICON = 0x0; // 'Large icon
public const uint SHGFI_SMALLICON = 0x1; // 'Small icon
[DllImport("shell32.dll")]
public static extern IntPtr SHGetFileInfo(string pszPath,
uint dwFileAttributes,
ref SHFILEINFO psfi,
uint cbSizeFileInfo,
uint uFlags);
}
private int nIndex = 0;
private void materialFlatButton13_Click_1(object sender, EventArgs e)
{
IntPtr hImgSmall; //the handle to the system image list
IntPtr hImgLarge; //the handle to the system image list
string fName; // 'the file name to get icon from
SHFILEINFO shinfo = new SHFILEINFO();
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\temp\\";
openFileDialog1.Filter = "All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
listView1.SmallImageList = imageList1;
listView1.LargeImageList = imageList1;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
fName = openFileDialog1.FileName;
//Use this to get the small Icon
hImgSmall = Win32.SHGetFileInfo(fName, 0, ref shinfo,
(uint)Marshal.SizeOf(shinfo),
Win32.SHGFI_ICON |
Win32.SHGFI_SMALLICON);
System.Drawing.Icon myIcon =
System.Drawing.Icon.FromHandle(shinfo.hIcon);
imageList1.Images.Add(myIcon);
//Add file name and icon to listview
listView1.Items.Add(fName, nIndex++);
}
You can find processes information using a WMI query on Win32_Process and use ExecutablePath to find executable path of the process. Then you can use Icon.ExtractAssociatedIcon to extract the associated icon of the process:
Example
Drop an ImageList on the form and set its ColorDepth to Depth32Bit and its ImageSize to 32,32. The drop a ListView on the form and set its LargImageList to imageList1 which you created in the first step.
Add reference to System.Management.dll and add using System.Management; and use below code to fill listView1 with icons:
var query = "SELECT ProcessId, Name, ExecutablePath FROM Win32_Process";
using (var searcher = new ManagementObjectSearcher(query))
using (var results = searcher.Get())
{
var processes = results.Cast<ManagementObject>().Select(x => new
{
ProcessId = (UInt32)x["ProcessId"],
Name = (string)x["Name"],
ExecutablePath = (string)x["ExecutablePath"]
});
foreach (var p in processes)
{
if (System.IO.File.Exists(p.ExecutablePath))
{
var icon = Icon.ExtractAssociatedIcon(p.ExecutablePath);
var key = p.ProcessId.ToString();
this.imageList1.Images.Add(key, icon.ToBitmap());
this.listView1.Items.Add(p.Name, key);
}
}
}
Then you will have such result:
Solution for 32-bit AND 64-bit processes
(with only System.Diagnostics)
This will get the icon of the main module (The exe file). It will also work if the architecture of the accessing and the targeted process differs:
var icon = Process.GetProcessById(1234).GetIcon()
With the extension methods:
public static class ProcessExtensions {
[DllImport("Kernel32.dll")]
private static extern uint QueryFullProcessImageName([In] IntPtr hProcess, [In] uint dwFlags, [Out] StringBuilder lpExeName, [In, Out] ref uint lpdwSize);
public static string GetMainModuleFileName(this Process process, int buffer = 1024) {
var fileNameBuilder = new StringBuilder(buffer);
uint bufferLength = (uint)fileNameBuilder.Capacity + 1;
return QueryFullProcessImageName(process.Handle, 0, fileNameBuilder, ref bufferLength) != 0 ?
fileNameBuilder.ToString() :
null;
}
public static Icon GetIcon(this Process process) {
try {
string mainModuleFileName = process.GetMainModuleFileName();
return Icon.ExtractAssociatedIcon(mainModuleFileName);
}
catch {
// Probably no access
return null;
}
}
}
See here: C#: How to get the full path of running process?
foreach(var process in Process.GetProcesses()){
string fullPath = process.MainModule.FileName;
// do your stuff here
}
You can substitute this into your existing code. Or, tailor it to what you're doing now.
Edit: added the code to get processes

How can i get application name which is currently focused [duplicate]

This question already has answers here:
C#: Detecting which application has focus
(3 answers)
Closed 6 years ago.
I am using windows forms and was wondering whether a user can get the name of other application, that get focused, or not.
e.g if i run an application then i should get the name of that application that recently started and is currently on focus.
i searched on google but didn't find any appropriate solution.
Thanks!
[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
public string GetActiveWindowTitle()
{
var handle = GetForegroundWindow();
string fileName = "";
string name = "";
uint pid = 0;
GetWindowThreadProcessId(handle, out pid);
Process p = Process.GetProcessById((int)pid);
var processname = p.ProcessName;
switch (processname)
{
case "explorer": //metro processes
case "WWAHost":
name = GetTitle(handle);
return name;
default:
break;
}
string wmiQuery = string.Format("SELECT ProcessId, ExecutablePath FROM Win32_Process WHERE ProcessId LIKE '{0}'", pid.ToString());
var pro = new ManagementObjectSearcher(wmiQuery).Get().Cast<ManagementObject>().FirstOrDefault();
fileName = (string)pro["ExecutablePath"];
// Get the file version
FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(fileName);
// Get the file description
name = myFileVersionInfo.FileDescription;
if (name == "")
name = GetTitle(handle);
return name;
}
public string GetTitle(IntPtr handle)
{
string windowText = "";
const int nChars = 256;
StringBuilder Buff = new StringBuilder(nChars);
if (GetWindowText(handle, Buff, nChars) > 0)
{
windowText = Buff.ToString();
}
return windowText;
}

I can access a file on a network path, but I cannot save to the same path (C#) (asp.net)

This declaration has no problem accessing the file in my network.
MyApp = new Excel.Application();
MyApp.Visible = false;
MyBook = MyApp.Workbooks.Open("//NetworkFolderPath/File.xlsx"); //This line
MySheet = (Excel.Worksheet)MyBook.Sheets[1];
The problem arises when I am done editing my file and I try to save it, using the "SaveCopyAs()" method.
MyBook.SaveCopyAs("//NetworkFolderPath/File2.xlsx");
MyBook.Close(0);
The Exception I get is
An exception of type 'System.Runtime.InteropServices.COMException' occurred in Application.dll but was not handled in user code
Additional information: Microsoft Excel cannot access the file '//NetworkFolderPath/File2.xlsx'. There are several possible reasons:
1. The file name or path does not exist.
2. The file is being used by another program.
3. The workbook you are trying to save has the same name as a currently open workbook.
For Number 1: The file does exist inside the folderm and I accessed the file via the path, so I have ruled out number 1.
For Number 2: I am not sure if it could be this reason, some explanation would be nice
For Number 3: I have given the workbook a different name, but I am not sure if this will solve the problem.
I am really not sure where to go from here. All help is appreciated.
I've seen this path does not exist error when Offline Files are turned on and there is corruption in the cache. Try resetting the cache using the instructions.
Try this, it saves my 2 weeks effort and the application is saving the pdf on sahredpath with secured id (Credential)
if you have any question please comment below !happy to help you
============================
public class Impersonate
{
[DllImport("advapi32.dll", SetLastError = true)]
private static extern int LogonUser(string lpszUsername, string lpszDomain, string lpszPassword,
int dwLogonType, int dwLogonProvider, out int phToken);
[DllImport("kernel32.dll")]
private static extern int FormatMessage(int dwFlags, string lpSource, int dwMessageId, int dwLanguageId,
StringBuilder lpBuffer, int nSize, string[] Arguments);
private const int LOGON32_LOGON_NETWORK_CLEARTEXT = 8;
private const int LOGON32_PROVIDER_DEFAULT = 0;
private const int FORMAT_MESSAGE_FROM_SYSTEM = 0x1000;
private static WindowsImpersonationContext winImpersonationContext = null;
public static void ImpersonateUser(string domain, string userName, string password)
{
//Benutzer einloggen
int userToken = 0;
bool loggedOn = (LogonUser(userName, domain, password, LOGON32_LOGON_NETWORK_CLEARTEXT,
LOGON32_PROVIDER_DEFAULT, out userToken) != 0);
if (loggedOn == false)
{
int apiError = Marshal.GetLastWin32Error();
StringBuilder errorMessage = new StringBuilder(1024);
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, null, apiError, 0, errorMessage, 1024, null);
throw new Exception(errorMessage.ToString());
}
WindowsIdentity identity = new WindowsIdentity((IntPtr)userToken);
winImpersonationContext = identity.Impersonate();
}
public static void UndoImpersonation()
{
if (winImpersonationContext != null)
{
winImpersonationContext.Undo();
}
}
}
2. Call Impersonate
Impersonate.ImpersonateUser("domain", "user name", "password");
//Your Code as the new User
DirectoryInfo _dirInfo = new DirectoryInfo(#"file path");
FileInfo[] _files = FileExtension.GetFilesByExtensions(_dirInfo, ".xls", ".xlsx").ToArray();
Impersonate.UndoImpersonation();
==============================
Ref:- https://forums.asp.net/t/2126720.aspx?Access+Denied+while+reading+writing+directory+in+Network+File+Share+through+Service+Account

Categories