IIS Hosted WCF Service permission to use interop - wcf

Is it possible to execute the following code, in any way through an IIS hosted wcf service? Any way I can give the appropriate permissions to the appPool? This code simply changes global printer settings:
public class PrinterSettings
{
#region "Private Variables"
private IntPtr hPrinter = new System.IntPtr();
private PRINTER_DEFAULTS PrinterValues = new PRINTER_DEFAULTS();
private PRINTER_INFO_2 pinfo = new PRINTER_INFO_2();
private DEVMODE dm;
private IntPtr ptrDM;
private IntPtr ptrPrinterInfo;
private int sizeOfDevMode = 0;
private int lastError;
private int nBytesNeeded;
private long nRet;
private int intError;
private System.Int32 nJunk;
private IntPtr yDevModeData;
#endregion
#region "Win API Def"
[DllImport("kernel32.dll", EntryPoint = "GetLastError", SetLastError = false,
ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern Int32 GetLastError();
[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true,
ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern bool ClosePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "DocumentPropertiesA", SetLastError = true,
ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern int DocumentProperties(IntPtr hwnd, IntPtr hPrinter,
[MarshalAs(UnmanagedType.LPStr)] string pDeviceNameg,
IntPtr pDevModeOutput, ref IntPtr pDevModeInput, int fMode);
[DllImport("winspool.Drv", EntryPoint = "GetPrinterA", SetLastError = true,
CharSet = CharSet.Ansi, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
private static extern bool GetPrinter(IntPtr hPrinter, Int32 dwLevel,
IntPtr pPrinter, Int32 dwBuf, out Int32 dwNeeded);
/*[DllImport("winspool.Drv", EntryPoint="OpenPrinterA",
SetLastError=true, CharSet=CharSet.Ansi,
ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter,
out IntPtr hPrinter, ref PRINTER_DEFAULTS pd)
[ DllImport( "winspool.drv",CharSet=CharSet.Unicode,ExactSpelling=false,
CallingConvention=CallingConvention.StdCall )]
public static extern long OpenPrinter(string pPrinterName,
ref IntPtr phPrinter, int pDefault);*/
/*[DllImport("winspool.Drv", EntryPoint="OpenPrinterA",
SetLastError=true, CharSet=CharSet.Ansi,
ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter,
out IntPtr hPrinter, ref PRINTER_DEFAULTS pd);
*/
[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA",
SetLastError = true, CharSet = CharSet.Ansi,
ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern bool
OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter,
out IntPtr hPrinter, ref PRINTER_DEFAULTS pd);
[DllImport("winspool.drv", CharSet = CharSet.Ansi, SetLastError = true)]
private static extern bool SetPrinter(IntPtr hPrinter, int Level, IntPtr
pPrinter, int Command);
/*[DllImport("winspool.drv", CharSet=CharSet.Ansi, SetLastError=true)]
private static extern bool SetPrinter(IntPtr hPrinter, int Level, IntPtr
pPrinter, int Command);*/
// Wrapper for Win32 message formatter.
/*[DllImport("kernel32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)]
private unsafe static extern int FormatMessage( int dwFlags,
ref IntPtr pMessageSource,
int dwMessageID,
int dwLanguageID,
ref string lpBuffer,
int nSize,
IntPtr* pArguments);*/
#endregion
#region "Data structure"
[StructLayout(LayoutKind.Sequential)]
public struct PRINTER_DEFAULTS
{
public int pDatatype;
public int pDevMode;
public int DesiredAccess;
}
/*[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public struct PRINTER_DEFAULTS
{
public int pDataType;
public IntPtr pDevMode;
public ACCESS_MASK DesiredAccess;
}*/
[StructLayout(LayoutKind.Sequential)]
private struct PRINTER_INFO_2
{
[MarshalAs(UnmanagedType.LPStr)]
public string pServerName;
[MarshalAs(UnmanagedType.LPStr)]
public string pPrinterName;
[MarshalAs(UnmanagedType.LPStr)]
public string pShareName;
[MarshalAs(UnmanagedType.LPStr)]
public string pPortName;
[MarshalAs(UnmanagedType.LPStr)]
public string pDriverName;
[MarshalAs(UnmanagedType.LPStr)]
public string pComment;
[MarshalAs(UnmanagedType.LPStr)]
public string pLocation;
public IntPtr pDevMode;
[MarshalAs(UnmanagedType.LPStr)]
public string pSepFile;
[MarshalAs(UnmanagedType.LPStr)]
public string pPrintProcessor;
[MarshalAs(UnmanagedType.LPStr)]
public string pDatatype;
[MarshalAs(UnmanagedType.LPStr)]
public string pParameters;
public IntPtr pSecurityDescriptor;
public Int32 Attributes;
public Int32 Priority;
public Int32 DefaultPriority;
public Int32 StartTime;
public Int32 UntilTime;
public Int32 Status;
public Int32 cJobs;
public Int32 AveragePPM;
}
private const short CCDEVICENAME = 32;
private const short CCFORMNAME = 32;
[StructLayout(LayoutKind.Sequential)]
public struct DEVMODE
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCDEVICENAME)]
public string dmDeviceName;
public short dmSpecVersion;
public short dmDriverVersion;
public short dmSize;
public short dmDriverExtra;
public int dmFields;
public short dmOrientation;
public short dmPaperSize;
public short dmPaperLength;
public short dmPaperWidth;
public short dmScale;
public short dmCopies;
public short dmDefaultSource;
public short dmPrintQuality;
public short dmColor;
public short dmDuplex;
public short dmYResolution;
public short dmTTOption;
public short dmCollate;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCFORMNAME)]
public string dmFormName;
public short dmUnusedPadding;
public short dmBitsPerPel;
public int dmPelsWidth;
public int dmPelsHeight;
public int dmDisplayFlags;
public int dmDisplayFrequency;
}
#endregion
#region "Constants"
private const int DM_DUPLEX = 0x1000;
private const int DM_IN_BUFFER = 8;
private const int DM_OUT_BUFFER = 2;
private const int PRINTER_ACCESS_ADMINISTER = 0x4;
private const int PRINTER_ACCESS_USE = 0x8;
private const int STANDARD_RIGHTS_REQUIRED = 0xF0000;
private const int PRINTER_ALL_ACCESS =
(STANDARD_RIGHTS_REQUIRED | PRINTER_ACCESS_ADMINISTER
| PRINTER_ACCESS_USE);
#endregion
#region "Function to change printer settings"
public bool ChangePrinterSetting(string PrinterName, PrinterData PS)
{
if (((int)PS.Duplex < 1) || ((int)PS.Duplex > 3))
{
throw new ArgumentOutOfRangeException("nDuplexSetting",
"nDuplexSetting is incorrect.");
}
else
{
dm = this.GetPrinterSettings(PrinterName);
dm.dmDefaultSource = (short)PS.source;
dm.dmOrientation = (short)PS.Orientation;
dm.dmPaperSize = (short)PS.Size;
dm.dmDuplex = (short)PS.Duplex;
Marshal.StructureToPtr(dm, yDevModeData, true);
pinfo.pDevMode = yDevModeData;
pinfo.pSecurityDescriptor = IntPtr.Zero;
/*update driver dependent part of the DEVMODE
1 = DocumentProperties(IntPtr.Zero, hPrinter, sPrinterName, yDevModeData
, ref pinfo.pDevMode, (DM_IN_BUFFER | DM_OUT_BUFFER));*/
try
{
Marshal.StructureToPtr(pinfo, ptrPrinterInfo, false);
}
catch
{
}
lastError = Marshal.GetLastWin32Error();
nRet = Convert.ToInt16(SetPrinter(hPrinter, 2, ptrPrinterInfo, 0));
if (nRet == 0)
{
//Unable to set shared printer settings.
lastError = Marshal.GetLastWin32Error();
//string myErrMsg = GetErrorMessage(lastError);
throw new Win32Exception(Marshal.GetLastWin32Error());
}
if (hPrinter != IntPtr.Zero)
ClosePrinter(hPrinter);
return Convert.ToBoolean(nRet);
}
}
private DEVMODE GetPrinterSettings(string PrinterName)
{
PrinterData PData = new PrinterData();
DEVMODE dm;
const int PRINTER_ACCESS_ADMINISTER = 0x4;
const int PRINTER_ACCESS_USE = 0x8;
const int PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED |
PRINTER_ACCESS_ADMINISTER | PRINTER_ACCESS_USE);
PrinterValues.pDatatype = 0;
PrinterValues.pDevMode = 0;
PrinterValues.DesiredAccess = PRINTER_ALL_ACCESS;
nRet = Convert.ToInt32(OpenPrinter(PrinterName,
out hPrinter, ref PrinterValues));
if (nRet == 0)
{
lastError = Marshal.GetLastWin32Error();
throw new Win32Exception(Marshal.GetLastWin32Error());
}
GetPrinter(hPrinter, 2, IntPtr.Zero, 0, out nBytesNeeded);
if (nBytesNeeded <= 0)
{
throw new System.Exception("Unable to allocate memory");
}
else
{
// Allocate enough space for PRINTER_INFO_2...
{// ptrPrinterIn fo =
Marshal.AllocCoTaskMem(nBytesNeeded);
};
ptrPrinterInfo = Marshal.AllocHGlobal(nBytesNeeded);
// The second GetPrinter fills in all the current settings, so all you
// need to do is modify what you're interested in...
nRet = Convert.ToInt32(GetPrinter(hPrinter, 2,
ptrPrinterInfo, nBytesNeeded, out nJunk));
if (nRet == 0)
{
lastError = Marshal.GetLastWin32Error();
throw new Win32Exception(Marshal.GetLastWin32Error());
}
pinfo = (PRINTER_INFO_2)Marshal.PtrToStructure(ptrPrinterInfo,
typeof(PRINTER_INFO_2));
IntPtr Temp = new IntPtr();
if (pinfo.pDevMode == IntPtr.Zero)
{
// If GetPrinter didn't fill in the DEVMODE, try to get it by calling
// DocumentProperties...
IntPtr ptrZero = IntPtr.Zero;
//get the size of the devmode structure
sizeOfDevMode = DocumentProperties(IntPtr.Zero, hPrinter,
PrinterName, ptrZero, ref ptrZero, 0);
ptrDM = Marshal.AllocCoTaskMem(sizeOfDevMode);
int i;
i = DocumentProperties(IntPtr.Zero, hPrinter, PrinterName, ptrDM,
ref ptrZero, DM_OUT_BUFFER);
if ((i < 0) || (ptrDM == IntPtr.Zero))
{
//Cannot get the DEVMODE structure.
throw new System.Exception("Cannot get DEVMODE data");
}
pinfo.pDevMode = ptrDM;
}
intError = DocumentProperties(IntPtr.Zero, hPrinter,
PrinterName, IntPtr.Zero, ref Temp, 0);
//IntPtr yDevModeData = Marshal.AllocCoTaskMem(i1);
yDevModeData = Marshal.AllocHGlobal(intError);
intError = DocumentProperties(IntPtr.Zero, hPrinter,
PrinterName, yDevModeData, ref Temp, 2);
dm = (DEVMODE)Marshal.PtrToStructure(yDevModeData, typeof(DEVMODE));
//nRet = DocumentProperties(IntPtr.Zero, hPrinter, sPrinterName, yDevModeData
// , ref yDevModeData, (DM_IN_BUFFER | DM_OUT_BUFFER));
if ((nRet == 0) || (hPrinter == IntPtr.Zero))
{
lastError = Marshal.GetLastWin32Error();
//string myErrMsg = GetErrorMessage(lastError);
throw new Win32Exception(Marshal.GetLastWin32Error());
}
return dm;
}
#endregion
}
}
However, I cannot get this code to execute in the IIS hosted wcf service. I've tried creating an external program and then use Process.start to execute a console app that runs the code shown above - but still won't execute.

Related

Implementing IServiceProvider and IInternetSecurityManager for WebBrowser control

I am trying to implement the IInternetSecurityManager on a WebBrowser control in order to override some IE security restrictions.
I have extended the WebBrowser control (call it MyWebBrowser) and have subclassed the WebBrowserSite class (call it ExtendedWebBrowserSite), and I've overridden the CreateWebBrowserSiteBase method on MyWebBrowser to return an instance of ExtendedWebBrowserSite.
class MyWebBrowser : System.Windows.Forms.WebBrowser
{
protected override WebBrowserSiteBase CreateWebBrowserSiteBase()
{
return new ExtendedWebBrowserSite(this);
}
}
My ExtendedWebBrowserSite class implements IServiceProvider and IInternetSecurityManager interfaces:
class ExtendedWebBrowserSite : WebBrowserSite, IServiceProvider, IInternetSecurityManager
{
// constructors omitted...
// IServiceProvider implementation
public int QueryService(ref Guid guidService, ref Guid riid, [MarshalAs(UnmanagedType.Interface)] out object ppvObject)
{
if (guidService == SID_IInternetSecurityManager && riid == SID_IInternetSecurityManager)
{
ppvObject = this;
return HRESULTS.S_OK; // = 0x0
}
ppvObject = IntPtr.Zero;
return HRESULTS.E_NOINTERFACE; // 0x80004002
}
// IInternetSecurityManager implementation ommitted...
}
COM Interface Definitions
[ComImport, GuidAttribute("6D5140C1-7436-11CE-8034-00AA006009FA")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IServiceProvider
{
int QueryService(ref Guid guidService, ref Guid riid [MarshalAs(UnmanagedType.Interface)] out object ppvObject);
}
[ComImport, Guid("79EAC9EE-BAF9-11CE-8C82-00AA004BA90B")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IInternetSecurityManager
{
[return: MarshalAs(UnmanagedType.I4)][PreserveSig]
int SetSecuritySite([In] IntPtr pSite);
[return: MarshalAs(UnmanagedType.I4)][PreserveSig]
int GetSecuritySite([Out] IntPtr pSite);
[return: MarshalAs(UnmanagedType.I4)][PreserveSig]
int MapUrlToZone([In,MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, out UInt32 pdwZone, UInt32 dwFlags);
[return: MarshalAs(UnmanagedType.I4)][PreserveSig]
int GetSecurityId([MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, [MarshalAs(UnmanagedType.LPArray)] byte[] pbSecurityId, ref UInt32 pcbSecurityId, uint dwReserved);
[return: MarshalAs(UnmanagedType.I4)][PreserveSig]
int ProcessUrlAction([In,MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, UInt32 dwAction, out byte pPolicy, UInt32 cbPolicy, byte pContext, UInt32 cbContext, UInt32 dwFlags, UInt32 dwReserved);
[return: MarshalAs(UnmanagedType.I4)][PreserveSig]
int QueryCustomPolicy([In,MarshalAs(UnmanagedType.LPWStr)] string pwszUrl, ref Guid guidKey, ref byte ppPolicy, ref UInt32 pcbPolicy, ref byte pContext, UInt32 cbContext, UInt32 dwReserved);
[return: MarshalAs(UnmanagedType.I4)][PreserveSig]
int SetZoneMapping(UInt32 dwZone, [In,MarshalAs(UnmanagedType.LPWStr)] string lpszPattern, UInt32 dwFlags);
[return: MarshalAs(UnmanagedType.I4)][PreserveSig]
int GetZoneMappings(UInt32 dwZone, out IEnumString ppenumString, UInt32 dwFlags);
}
Results
The QueryService method is called when the WebBrowser is initialized. However, right after the QueryService method finishes and returns HRESULTS.E_NOINTERFACE, an AccessViolationException is thrown:
AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.Windows.Forms.UnsafeNativeMethods.IOleObject.SetClientSite(IOleClientSite pClientSite)
at System.Windows.Forms.WebBrowserBase.TransitionFromLoadedToRunning()
at System.Windows.Forms.WebBrowserBase.TransitionUpTo(AXState state)
at System.Windows.Forms.WebBrowser.get_AxIWebBrowser2()
at System.Windows.Forms.WebBrowser.set_ScriptErrorsSuppressed(Boolean value)
Note: None of the IInternetSecurityManager methods get called, because QueryService never gets the chance to assign an implementation of it. So I omitted the IInernetSecurityManager methods for brevity.
Anyone have any ideas as to why this could be happening?

Impersonating user in asp.net core

I have the following piece of code from a regular mvc app which uploads a file by impersonating a user
public class PublicController : Controller
{
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;
WindowsImpersonationContext impersonationContext;
[DllImport("advapi32.dll")]
public static extern int LogonUserA(String lpszUserName,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int DuplicateToken(IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool RevertToSelf();
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);
public SomeActionMethod(model containing file)
{
if (ImpersonateValidUser(userName: "someuserwithpowertoupload", domain: "", password: "somepassword"))
{
path = "Somepath";
file.SaveAs(path);
}
}
private bool ImpersonateValidUser(String userName, String domain, String password)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if (RevertToSelf())
{
if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, ref token) != 0)
{
if (DuplicateToken(token, impersonationLevel: 2, hNewToken: ref tokenDuplicate) != 0)
{
using (tempWindowsIdentity = new WindowsIdentity(tokenDuplicate))
{
this.impersonationContext = tempWindowsIdentity.Impersonate();
if (this.impersonationContext != null)
{
CloseHandle(token);
CloseHandle(tokenDuplicate);
return true;
}
}
}
}
}
if (token != IntPtr.Zero)
{
CloseHandle(token);
}
if (tokenDuplicate != IntPtr.Zero)
{
CloseHandle(tokenDuplicate);
}
return false;
}
Problem here is that WindowsImpersonationContext doesnt exist in .net core. Can anyone provide a code snippet which impersonates a user? Microsoft docs here https://learn.microsoft.com/en-us/dotnet/standard/security/impersonating-and-reverting isnt very helpful.
Thank you.
From the docs:
ASP.NET Core doesn't implement impersonation. Apps run with the app's identity for all requests, using app pool or process identity. If the app should perform an action on behalf of a user, use WindowsIdentity.RunImpersonated or RunImpersonatedAsync in a terminal inline middleware in Startup.Configure.
app.Run(async (context) =>
{
try
{
var user = (WindowsIdentity)context.User.Identity;
await context.Response
.WriteAsync($"User: {user.Name}\tState: {user.ImpersonationLevel}\n");
WindowsIdentity.RunImpersonated(user.AccessToken, () =>
{
var impersonatedUser = WindowsIdentity.GetCurrent();
var message =
$"User: {impersonatedUser.Name}\t" +
$"State: {impersonatedUser.ImpersonationLevel}";
var bytes = Encoding.UTF8.GetBytes(message);
context.Response.Body.Write(bytes, 0, bytes.Length);
});
}
catch (Exception e)
{
await context.Response.WriteAsync(e.ToString());
}
});

Setter methods do not work whereas Getter methods work fine

The error is always that the dino cannot be converted into type string
however im struggling to understand why the compiler would think of trying to
converting dino into ints or strings from the first method
public static void Tyrannosaurus()
{
String DinoName = Name();
Dinosaur Tyrannosaurus = new Dinosaur();
Tyrannosaurus.name = DinoName;
Tyrannosaurus = setDiet(Tyrannosaurus, DinoDiet[0]);
Tyrannosaurus = setHP(Tyrannosaurus, 100);
Tyrannosaurus = setDamage(Tyrannosaurus, 200);
DaysLoop(DinoName);
return;
}
Here is the first getter method being used for the Dinosaur record and Tyrannosaurus instance above
public static String getName (Dinosaur dino)
{
return Tyrannosaurus.name;
}
public static String getDiet (Dinosaur dino)
{
return Tyrannosaurus.diet;
}
public static int getHP (Dinosaur dino)
{
return dino.HP;
}
public static int getDamage (Dinosaur dino)
{
return dino.damage;
}
setter method which does not work is done from here, I do see in other java setters people use this. but I havent quite grasped that concept yet
public static String setDiet (Dinosaur dino, String TyranDiet)
{
dino.diet = TyranDiet;
return dino;
}
public static int setHP (Dinosaur dino, int TyranHP)
{
dino.HP = TyranHP;
return dino;
}
public static int setDamage (Dinosaur dino, int TyranDamage)
{
dino.damage = TyranDamage;
return dino;
}
//////////////////////////////////
You return dino as string and dino as an int in below methods.
public static String setDiet (Dinosaur dino, String TyranDiet);
public static int setHP (Dinosaur dino, int TyranHP);
public static int setDamage (Dinosaur dino, int TyranDamage);
that's why its pops error like that
basically setters not returns anything. remove return and re-write code like this
public static void setDiet (Dinosaur dino, String TyranDiet)
{
dino.diet = TyranDiet;
}
public static void setHP (Dinosaur dino, int TyranHP)
{
dino.HP = TyranHP;
}
public static void setDamage (Dinosaur dino, int TyranDamage)
{
dino.damage = TyranDamage;
}

Embedding Selenium ChromeDriver in Windows Form

I'd like to embed the Selenium ChromeDriver within a Windows Form panel. I've successfully followed this example for embedding notepad in a Windows Form. I think the problem where I am having trouble is getting the MainWindowHandle from the ChromeDriver. Here's what I've got so far: (pastebin link of full code: https://pastebin.com/RNv2vbJ1)
//----------------------TEST---------------------
ChromeDriverService service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;
ChromeDriver driver = new ChromeDriver(service);
Console.WriteLine(driver.CurrentWindowHandle);
Process chromeDriverProcess = Process.GetProcessById(service.ProcessId);
//EmbeddedWindowHandle = driver.CurrentWindowHandle;
//----------------------TEST---------------------
ProcessStartInfo processInfo = new ProcessStartInfo("notepad.exe");
Process p = Process.Start(processInfo);
p.WaitForInputIdle();
EmbeddedWindowHandle = p.MainWindowHandle;
SetParent(EmbeddedWindowHandle, panel1.Handle);
MoveWindow(EmbeddedWindowHandle, 0, 0, panel1.Width, panel1.Height, true);
SetWindowLong(EmbeddedWindowHandle, GWL_STYLE, WS_VISIBLE);
So embedding a console app is somewhat similar to your approach but it is not the same. I found lot of threads which said it is not possible or it doesn't work
How to embed a Console application inside a Winforms application
Embedding a DOS console in a windows form
Embedding a DOS console in a windows form with Visual Basic
And there is the below link to create your own control as a CMD
https://www.codeproject.com/Articles/335909/Embedding-a-Console-in-a-C-Application
But I finally managed to get the below code working
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private IntPtr cmdPtr;
private int ConsoleWidth;
private IntPtr folderViewPtr;
private const int SWP_NOOWNERZORDER = 0x200;
private const int SWP_NOREDRAW = 0x8;
private const int SWP_NOZORDER = 0x4;
private const int SWP_SHOWWINDOW = 0x0040;
private const int WS_EX_MDICHILD = 0x40;
private const int SWP_FRAMECHANGED = 0x20;
private const int SWP_NOACTIVATE = 0x10;
private const int SWP_ASYNCWINDOWPOS = 0x4000;
private const int SWP_NOMOVE = 0x2;
private const int SWP_NOSIZE = 0x1;
private const int GWL_STYLE = (-16);
private const int GWL_EXSTYLE = (-20);
private const int WS_VISIBLE = 0x10000000;
private const int WM_CLOSE = 0x10;
private const int WS_CHILD = 0x40000000;
public abstract class WS
{
public const uint WS_OVERLAPPED = 0x00000000;
public const uint WS_POPUP = 0x80000000;
public const uint WS_CHILD = 0x40000000;
public const uint WS_MINIMIZE = 0x20000000;
public const uint WS_VISIBLE = 0x10000000;
public const uint WS_DISABLED = 0x08000000;
public const uint WS_CLIPSIBLINGS = 0x04000000;
public const uint WS_CLIPCHILDREN = 0x02000000;
public const uint WS_MAXIMIZE = 0x01000000;
public const uint WS_CAPTION = 0x00C00000; /* WS_BORDER | WS_DLGFRAME */
public const uint WS_BORDER = 0x00800000;
public const uint WS_DLGFRAME = 0x00400000;
public const uint WS_VSCROLL = 0x00200000;
public const uint WS_HSCROLL = 0x00100000;
public const uint WS_SYSMENU = 0x00080000;
public const uint WS_THICKFRAME = 0x00040000;
public const uint WS_GROUP = 0x00020000;
public const uint WS_TABSTOP = 0x00010000;
public const uint WS_MINIMIZEBOX = 0x00020000;
public const uint WS_MAXIMIZEBOX = 0x00010000;
public const uint WS_TILED = WS_OVERLAPPED;
public const uint WS_ICONIC = WS_MINIMIZE;
public const uint WS_SIZEBOX = WS_THICKFRAME;
public const uint WS_TILEDWINDOW = WS_OVERLAPPEDWINDOW;
// Common Window Styles
public const uint WS_OVERLAPPEDWINDOW =
(WS_OVERLAPPED |
WS_CAPTION |
WS_SYSMENU |
WS_THICKFRAME |
WS_MINIMIZEBOX |
WS_MAXIMIZEBOX);
public const uint WS_POPUPWINDOW =
(WS_POPUP |
WS_BORDER |
WS_SYSMENU);
public const uint WS_CHILDWINDOW = WS_CHILD;
//Extended Window Styles
public const uint WS_EX_DLGMODALFRAME = 0x00000001;
public const uint WS_EX_NOPARENTNOTIFY = 0x00000004;
public const uint WS_EX_TOPMOST = 0x00000008;
public const uint WS_EX_ACCEPTFILES = 0x00000010;
public const uint WS_EX_TRANSPARENT = 0x00000020;
//#if(WINVER >= 0x0400)
public const uint WS_EX_MDICHILD = 0x00000040;
public const uint WS_EX_TOOLWINDOW = 0x00000080;
public const uint WS_EX_WINDOWEDGE = 0x00000100;
public const uint WS_EX_CLIENTEDGE = 0x00000200;
public const uint WS_EX_CONTEXTHELP = 0x00000400;
public const uint WS_EX_RIGHT = 0x00001000;
public const uint WS_EX_LEFT = 0x00000000;
public const uint WS_EX_RTLREADING = 0x00002000;
public const uint WS_EX_LTRREADING = 0x00000000;
public const uint WS_EX_LEFTSCROLLBAR = 0x00004000;
public const uint WS_EX_RIGHTSCROLLBAR = 0x00000000;
public const uint WS_EX_CONTROLPARENT = 0x00010000;
public const uint WS_EX_STATICEDGE = 0x00020000;
public const uint WS_EX_APPWINDOW = 0x00040000;
public const uint WS_EX_OVERLAPPEDWINDOW = (WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE);
public const uint WS_EX_PALETTEWINDOW = (WS_EX_WINDOWEDGE | WS_EX_TOOLWINDOW | WS_EX_TOPMOST);
//#endif /* WINVER >= 0x0400 */
//#if(_WIN32_WINNT >= 0x0500)
public const uint WS_EX_LAYERED = 0x00080000;
//#endif /* _WIN32_WINNT >= 0x0500 */
//#if(WINVER >= 0x0500)
public const uint WS_EX_NOINHERITLAYOUT = 0x00100000; // Disable inheritence of mirroring by children
public const uint WS_EX_LAYOUTRTL = 0x00400000; // Right to left mirroring
//#endif /* WINVER >= 0x0500 */
//#if(_WIN32_WINNT >= 0x0500)
public const uint WS_EX_COMPOSITED = 0x02000000;
public const uint WS_EX_NOACTIVATE = 0x08000000;
//#endif /* _WIN32_WINNT >= 0x0500 */
}
public Form1()
{
InitializeComponent();
this.Width = 700;
this.ConsoleWidth = this.Width;
folderViewPtr = FindWindow("Progman", null);
folderViewPtr = FindWindowEx(folderViewPtr, IntPtr.Zero, "SHELLDLL_DefView", null);
folderViewPtr = FindWindowEx(folderViewPtr, IntPtr.Zero, "SysListView32", "FolderView");
//SetWindowLong(this.Handle, -8, (int)folderViewPtr); //GWL_HWNDPARENT
Process p = Process.Start("cmd.exe", "/k cd %userprofile%\\desktop");
Thread.Sleep(500); // Allow the process to open it's window
cmdPtr = p.MainWindowHandle;
SetParent(cmdPtr, this.Handle);
SetWindowLong(this.Handle, -20, (int)(GetWindowLong(this.Handle, -20) | WS.WS_CLIPCHILDREN ));
// Remove border and whatnot
SetWindowLong(cmdPtr, GWL_STYLE, (int)(WS.WS_VISIBLE ));
long exStyle = GetWindowLong(p.MainWindowHandle, GWL_EXSTYLE);
exStyle &= ~(WS.WS_EX_APPWINDOW | WS.WS_EX_CLIENTEDGE);
SetWindowLong(p.MainWindowHandle, GWL_EXSTYLE, (int)0);
MoveWindow(cmdPtr, -2, -2, this.ConsoleWidth, this.Height, true);
SetWindowPos(this.Handle, (IntPtr)1, this.Left, this.Top, this.Width, this.Height, 0);
timer1.Start();
}
protected override void WndProc(ref Message message)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_MOVE = 0xF010;
const int WM_WINDOWPOSCHANGING = 0x0046;
switch (message.Msg)
{
case WM_SYSCOMMAND:
int command = message.WParam.ToInt32() & 0xfff0;
if (command == SC_MOVE)
{
//return;
}
break;
case WM_WINDOWPOSCHANGING:
SetWindowPos(this.Handle, (IntPtr)1, this.Left, this.Top, this.Width, this.Height, 0x0004);
Debug.Print("WM_WINDOWPOSCHANGING");
break;
}
base.WndProc(ref message);
}
protected override void OnResize(EventArgs e)
{
if (this.cmdPtr != IntPtr.Zero)
{
MoveWindow(cmdPtr, 0, 0, this.ConsoleWidth, this.Height, true);
}
base.OnResize(e);
}
protected override void OnHandleDestroyed(EventArgs e)
{
if (cmdPtr != IntPtr.Zero)
{
PostMessage(cmdPtr, 0x0010, 0, 0);
}
base.OnHandleDestroyed(e);
}
[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll", SetLastError = true)]
static extern UInt32 GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("User32.dll")]
static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool repaint);
[DllImport("User32.dll")]
static extern bool PostMessage(IntPtr hWnd, uint message, int wparam, int lparam);
[DllImport("User32.dll")]
static extern void SetActiveWindow(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindow(IntPtr hWnd);
[DllImport("user32.dll")]
static extern void SetWindowPos(IntPtr hWnd, IntPtr hWndNew, int x, int y, int cx, int cy, UInt32 uFlags);
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
[DllImport("user32.dll")]
static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
static extern IntPtr WindowFromPoint(int xPoint, int yPoint);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern IntPtr FindWindowEx(IntPtr hWnd, IntPtr hWndA, string lpClassName, string lpWindowName);
private void Form1_Load(object sender, EventArgs e)
{
this.Left = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Right/2 - this.Width/2;
this.Top = 0;
}
private void timer1_Tick(object sender, EventArgs e)
{
//check if cmd still exists
if (!IsWindow(cmdPtr))
{
Application.Exit();
}
}
}
}
PS: Credits to https://github.com/KillPinguin/CommandLineWidget

Options for HIDAPI on OS X

I have been working with HIDAPI with C and trying to get Mono to communicate with the HIDAPI using interop. I have done a lot of searching and have not been able to find anyone who has gotten HIDAPI to work with Mono on OS X.
Does anyone know if I can redirect the output from a HID device using HIDAPI to a local virtual serial port and then have Mono just read from the serial port?
Another option, would anyone know if I could use something like Arduino leonardo or Circuits#Home USB Host Shield?
At least until I can sort out PInvoke on Mono.
Thanks
I found and adapted this code from elsewhere, though I can't for the life of me find the source. If someone knows, please let me know so I can properly attribute it and link to it. It's been working well for me on both Windows and OS X. You do have to build hidapi for each platform, obviously.
using System;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
namespace HidApiCommunicationLayer
{
internal class HidApiInteropCommLayer
{
#region Interop
#if WINDOWS
private const string HIDAPI_DLL = "hidapi.dll";
#else
private const string HIDAPI_DLL = "hidapi.dylib";
#endif
protected IntPtr _device;
private Object _locker = new object();
public bool IsOpen()
{
return _device != IntPtr.Zero;
}
public void Open(ushort vid, ushort hid, string serial)
{
if (_device != IntPtr.Zero) throw new Exception("a device is already opened; close it first.");
IntPtr ret = hid_open(vid, hid, serial);
_device = ret;
//if (_device != IntPtr.Zero)
// hid_set_nonblocking(_device, true);
}
public int Read(byte[] buffer, int length)
{
lock (_locker)
{
AssertValidDev();
int ret = hid_read_timeout(_device, buffer, (uint)length, 1);
if (ret < 0)
throw new Exception("Failed to Read.");
return ret;
}
}
public void Close()
{
AssertValidDev();
hid_close(_device);
_device = IntPtr.Zero;
}
public int ExitHidAPI()
{
return hid_exit();
}
public String GetProductString()
{
AssertValidDev();
byte[] buf = new byte[1000];
int ret = HidApiInteropCommLayer.hid_get_product_string(_device, buf, (uint)(buf.Length / 4) - 1);
if (ret < 0)
throw new Exception("failed to receive product string");
return EncodeBuffer(buf);
}
public String GetManufacturerString()
{
AssertValidDev();
byte[] buf = new byte[1000];
int ret = HidApiInteropCommLayer.hid_get_manufacturer_string(_device, buf, (uint)(buf.Length / 4) - 1);
if (ret < 0)
throw new Exception("failed to receive manufacturer string");
return EncodeBuffer(buf);
}
public int GetFeatureReport(byte[] buffer, int length)
{
AssertValidDev();
int ret = hid_get_feature_report(_device, buffer, (uint)length);
if (ret < 0)
throw new Exception("failed to get feature report");
return ret;
}
public int SendFeatureReport(byte[] buffer)
{
int ret = hid_send_feature_report(_device, buffer, (uint)buffer.Length);
//if (ret < 0)
// throw new Exception ("failed to send feature report");
return ret;
}
public int Write(byte[] buffer)
{
lock (_locker)
{
AssertValidDev();
int ret = hid_write(_device, buffer, HID_MAX_PACKET_SIZE + 1);
//if (ret < 0)
// Custom logging
return ret;
}
}
public String Error()
{
AssertValidDev();
IntPtr ret = hid_error(_device);
return Marshal.PtrToStringAuto(ret);
}
public string GetIndexedString(int index)
{
AssertValidDev();
byte[] buf = new byte[1000];
int ret = HidApiInteropCommLayer.hid_get_indexed_string(_device, index, buf, (uint)(buf.Length / 4) - 1);
if (ret < 0)
throw new Exception("failed to receive indexed string");
return EncodeBuffer(buf);
}
public string GetSerialNumberString()
{
AssertValidDev();
byte[] buf = new byte[1000];
int ret = HidApiInteropCommLayer.hid_get_serial_number_string(_device, buf, (uint)(buf.Length / 4) - 1);
if (ret < 0)
throw new Exception("failed to receive serial number string");
return EncodeBuffer(buf);
}
private string EncodeBuffer(byte[] buffer)
{
return Encoding.Unicode.GetString(buffer).Trim('\0');
}
private void AssertValidDev()
{
if (_device == IntPtr.Zero) throw new Exception("No device opened");
}
#region DllImports
[DllImport(HIDAPI_DLL)]
private static extern int hid_read(IntPtr device, [Out, MarshalAs(UnmanagedType.LPArray)] byte[] data, uint length);
[DllImport(HIDAPI_DLL)]
private static extern int hid_read_timeout(IntPtr device, [Out, MarshalAs(UnmanagedType.LPArray)] byte[] data, uint length, int timeout);
[DllImport(HIDAPI_DLL)]
private static extern IntPtr hid_open(ushort vid, ushort pid, [MarshalAs(UnmanagedType.LPWStr)] string serial);
[DllImport(HIDAPI_DLL)]
private static extern void hid_close(IntPtr device);
[DllImport(HIDAPI_DLL)]
private static extern int hid_init();
[DllImport(HIDAPI_DLL)]
private static extern int hid_exit();
[DllImport(HIDAPI_DLL)]
private static extern int hid_get_product_string(IntPtr device, [Out] byte[] _string, uint length);
[DllImport(HIDAPI_DLL)]
private static extern int hid_get_manufacturer_string(IntPtr device, [Out] byte[] _string, uint length);
[DllImport(HIDAPI_DLL)]
private static extern int hid_get_feature_report(IntPtr device, [Out, MarshalAs(UnmanagedType.LPArray)] byte[] data, uint length);
[DllImport(HIDAPI_DLL)]
private static extern int hid_get_serial_number_string(IntPtr device, [Out] byte[] serial, uint maxlen);
[DllImport(HIDAPI_DLL)]
private static extern int hid_get_indexed_string(IntPtr device, int string_index, [Out] byte[] _string, uint maxlen);
[DllImport(HIDAPI_DLL)]
private static extern IntPtr hid_error(IntPtr device);
[DllImport(HIDAPI_DLL)]
private static extern int hid_send_feature_report(IntPtr device, [In, MarshalAs(UnmanagedType.LPArray)] byte[] data, uint length);
[DllImport(HIDAPI_DLL)]
private static extern int hid_set_nonblocking(IntPtr device, [In, MarshalAs(UnmanagedType.SysInt)] bool nonblock);
[DllImport(HIDAPI_DLL)]
private static extern int hid_write(IntPtr device, [In, MarshalAs(UnmanagedType.LPArray)] byte[] data, uint length);
[DllImport(HIDAPI_DLL)]
private static extern IntPtr hid_open_path([In, MarshalAs(UnmanagedType.LPStr)] string path);
#endregion DllImports
#endregion Interop
#region Constructors
public static HidApiInteropCommLayer GetDevice(ushort vid, ushort pid)
{
try
{
HidApiInteropCommLayer layer = new HidApiInteropCommLayer();
layer.Open(vid, pid, null);
return layer._device == IntPtr.Zero ? null : layer;
}
catch (System.BadImageFormatException fx)
{
//Custom logging
return null;
}
catch (Exception ex)
{
//Custom logging
return null;
}
}
#endregion Constructors
private const int HID_MAX_PACKET_SIZE = 1024;
#region ICommunicationLayer
public void Init()
{
try
{
if (IsOpen())
{
ContinueReadProcessing = true;
ReadThread = new Thread(new ThreadStart(ReadLoop));
ReadThread.Name = "HidApiReadThread";
ReadThread.Start();
}
else
{
Disconnect();
}
}
catch (Exception ex)
{
//Custom logging
throw;
}
}
public bool SendData(byte[] data)
{
try
{
MemoryStream stream = new MemoryStream(HID_MAX_PACKET_SIZE + 1);
stream.WriteByte(0);
stream.Write(data, 0, HID_MAX_PACKET_SIZE);
int ret = Write(stream.ToArray());
if (ret >= 0)
return true;
else
{
return false;
}
}
catch (Exception ex)
{
//Custom logging
return false;
}
}
public event EventHandler<DataEventArgs> DataReceived;
public event EventHandler Disconnected;
public void Start()
{
ContinueReadProcessing = true;
}
public void Stop()
{
Disconnect();
}
#endregion ICommunicationLayer
private Thread ReadThread = null;
protected volatile bool ContinueReadProcessing = true;
private void ReadLoop()
{
var culture = CultureInfo.InvariantCulture;
Thread.CurrentThread.CurrentCulture = culture;
Thread.CurrentThread.CurrentUICulture = culture;
Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;
while (ContinueReadProcessing)
{
try
{
byte[] report = new byte[HID_MAX_PACKET_SIZE];
var result = Read(report, HID_MAX_PACKET_SIZE);
if (result > 0)
{
DataReceived(this, new DataEventArgs(report));
}
else if (result < 0)
{
Disconnect();
}
}
catch (Exception ex)
{
Disconnect();
}
Thread.Sleep(1);
}
}
private void Disconnect()
{
ContinueReadProcessing = false;
Disconnected(this, EventArgs.Empty);
}
#region IDisposable Members
public void Dispose()
{
ContinueReadProcessing = false;
ReadThread.Join(500);
if (ReadThread.IsAlive)
{
ReadThread.Abort();
}
if (IsOpen())
Close();
int res = ExitHidAPI();
}
#endregion IDisposable Members
}
internal class Utf32Marshaler : ICustomMarshaler
{
private static Utf32Marshaler instance = new Utf32Marshaler();
public static ICustomMarshaler GetInstance(string s)
{
return instance;
}
public void CleanUpManagedData(object o)
{
}
public void CleanUpNativeData(IntPtr pNativeData)
{
Marshal.FreeHGlobal(pNativeData);
//UnixMarshal.FreeHeap(pNativeData);
}
public int GetNativeDataSize()
{
return IntPtr.Size;
}
public IntPtr MarshalManagedToNative(object obj)
{
string s = obj as string;
if (s == null)
return IntPtr.Zero;
return Marshal.StringToHGlobalAuto(s);
}
public object MarshalNativeToManaged(IntPtr pNativeData)
{
return Marshal.PtrToStringAuto(pNativeData);
}
}
public class DataEventArgs : EventArgs
{
public DataEventArgs(byte[] data)
{
Data = data;
}
public byte[] Data { get; private set; }
}
}
Arduino Leonardo can't act as a USB Host.
In principle though, yes, you could make a device act as a CDC Serial Port on the device side, and have it act as Host USB to a HID device.
Alternatively, you could skip the HIDAPI step entirely and use HidSharp. :) It'll P/Invoke directly into the MacOS X native APIs.
Hope this helps
James