Screen sharing with powershell - powershell

i am trying to make a screen viewer.
i have an C# server side that works good and i have C# client side that works good.
this is my C# client side code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.Drawing.Imaging;
using System.Runtime.Serialization.Formatters.Binary;
namespace WindowsFormsApp24
{
public partial class Form1 : Form
{
private readonly TcpClient client = new TcpClient();
private NetworkStream mainStream;
private int portNumber;
private static Image GrabDesktop()
{
Rectangle bound = Screen.PrimaryScreen.Bounds;
Bitmap screenshot = new Bitmap(bound.Width, bound.Height, PixelFormat.Format32bppArgb);
Graphics graphics = Graphics.FromImage(screenshot);
graphics.CopyFromScreen(bound.X, bound.Y, 0, 0, bound.Size, CopyPixelOperation.SourceCopy);
return screenshot;
}
private void SendDesktopImage()
{
BinaryFormatter binFormatter = new BinaryFormatter();
mainStream = client.GetStream();
binFormatter.Serialize(mainStream, GrabDesktop());
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
portNumber = 443;
try
{
string ip = "192.168.1.55";
client.Connect(ip, portNumber);
}
catch
{
}
timer1.Start();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void btnConnect_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
SendDesktopImage();
}
}
}
The thing i am trying to do, is to switch the client side from C# to powershell.
is that possible? i am searching for ideas on google for along time, I would be happy
to hear what you have to say.. many thanks.

C# and Powershell are both using some version of the Dot Net runtime so the same objects and properties should be available in both, just by a different syntax. The process for converting between the two is basically this:
First you need to load the required assemblies into powershell with Add-Type. You will require several assemblies such as System.Net, System.Drawing and System.Windows.Forms to get access to the Objects in your code. (this is essentially the same as adding references to a C# project). Example: Add-Type -AssemblyName System.Drawing; would load everything inside System.Drawing.dll
Next you need to convert the syntax from C# to powershell line by line:
Example
private readonly TcpClient client = new TcpClient();
Would become:
[System.Net.Sockets.TcpClient] $client = New-Object System.Net.Sockets.TcpClient
Mostly this process will be simplification and expanding names. Above [System.Net.Sockets.TcpClient] is the fully qualified type - ie the long form name of the C# Class (visible on the tooltips in Visual Studio). New-Object is equivalent to new
Assuming you just want the screenshot to be taken and sent each time the script is executed you dont need to worry about writing classes or functions in powershell you can just convert each functional line of c# into powershell in a text file saved with .ps1 extension.

Related

Powershell [System.Windows.Forms.SendKeys], send shift+windowsKey+rightArrow combination

In powershell I'm using [System.Windows.Forms.SendKeys] to send keystrokes. However I'm having trouble emulating the windows key. Since it isn't one of the keys I found in the list in the docs:
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.sendkeys?view=netcore-3.1
In this post I saw that they were emulating the windows keys using ctrl+esc however this doesn't seem to work.
Sending Windows key using SendKeys
Any idea how to accomplish this?
Try this solution. It has press down window key so that you can send combination key along with that.
**for me Win + Right Arrow key worked but shift doesn't have any effect on my machine. It might work for you.
$source = #"
using System;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace KeySends
{
public class KeySend
{
[DllImport("user32.dll")]
public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
private const int KEYEVENTF_EXTENDEDKEY = 1;
private const int KEYEVENTF_KEYUP = 2;
public static void KeyDown(Keys vKey)
{
keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY, 0);
}
public static void KeyUp(Keys vKey)
{
keybd_event((byte)vKey, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
}
}
"#
Add-Type -TypeDefinition $source -ReferencedAssemblies "System.Windows.Forms"
Function WinKey ($Key)
{
[KeySends.KeySend]::KeyDown("LWin")
[KeySends.KeySend]::KeyDown("ShiftKey")
[KeySends.KeySend]::KeyDown("$Key")
[KeySends.KeySend]::KeyUp("LWin")
[KeySends.KeySend]::KeyUp("ShiftKey")
}
WinKey({Right})
.NET System.Windows.Forms Keys list

How to send a message or event into Server Unity3D?

I am learning about Networking in Unity. I am confused on how can I send a message or event from client to server. I have these script already that successfully connect my client to server.
This is my server script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class ServerNetwork : MonoBehaviour {
void Start()
{
SetupServer();
}
// Create a server and listen on a port
public void SetupServer()
{
NetworkServer.Listen(4444);
NetworkServer.RegisterHandler(MsgType.Connect, OnClientConnected);
Debug.Log("Server is running");
}
void OnClientConnected(NetworkMessage netMsg)
{
Debug.Log("Client connected");
Debug.Log(netMsg.msgType);
}
}
This is my client Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class ClientNetwork : MonoBehaviour {
NetworkClient myClient;
public bool isClientConnected = false;
void Start()
{
SetupClient();
}
// Create a client and connect to the server port
public void SetupClient()
{
myClient = new NetworkClient();
myClient.RegisterHandler(MsgType.Connect, OnConnected);
myClient.Connect("127.0.0.1", 4444);
isClientConnected = true;
}
// client function
public void OnConnected(NetworkMessage netMsg)
{
Debug.Log("Connected to server");
}
}
The Server Script is attached to "NetworkManager" Object on ServerScene
The Client Script is attached to "NetworkManager" Object on ClientScene
I have build ClientScene alone to run as a client, and running ServerScene inside editor
With these script, I can already connect the client into the server. From this, how can I communicate from client to server ?
the purpose here is to send real time score into the server from client every second.
Thank You
You have to use [SyncVars] Attribute. In short what it does is it synchronize a variable from client to the server. Here is a simple example from Unity3d Documentation.
[SyncVar]
int health;
public void TakeDamage(int amount)
{
if (!isServer)
return;
health -= amount;
}
another way to do this is by using custom WebSocket implementation for Unity3D. There a few very good ones in GitHub.Also please take a look at the documentation Unity3d

multiply two input boxes and then show result time nine in third box

I am trying to find code to make a calculator of sorts. I need it to be three input boxes where you can type data in the first two boxes. First box divided by the second box and the result is then multiplied by 9 (the nine not shown) and the result or answer is shown in the third text box. With some sort of compute or calculate button.
This should do it:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace calculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int answer = (Convert.ToInt32(txt1.Text) / Convert.ToInt32(txt2.Text)) * 9 ;
txtAnswer.Text = Convert.ToString(answer);
}
}
}

MEF giving CompositionException

I am new to MEF and I am trying this following program.
The class library whose dll I am making has following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.Composition;
namespace MefTestsCore
{
[Export (typeof(MefTestsCore.IFace))]
public class CoreClass1 : IFace
{
public String getName()
{
return "CoreClass1";
}
}
interface IFace
{
String getName();
}
}
The program which wants to access the dll has following code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using MefTestsCore;
namespace MefTests
{
class Program
{
[Import (typeof(MefTestsCore.IFace), AllowRecomposition=true)]
public IFace iFaceObj;
static void Main(string[] args)
{
Program p = new Program();
p.Method();
}
public void Method()
{
DirectoryCatalog catalog = new DirectoryCatalog(#"C:\Users\username");
CompositionContainer container = new CompositionContainer(catalog);
container.ComposeParts(this);
Console.WriteLine(iFaceObj.getName());
Console.ReadKey();
}
}
}
I have the MEFLibrary.dll (dll containing MefTestsCore namespace, the first part of above code) at C:\Users\username. When I run the program, it throws CompositionException with following details
The composition produced a single composition error.
The root cause is provided below. Review the CompositionException.Errors property for more detailed information.
1) The export 'MefTestsCore.CoreClass1 (ContractName="MefTestsCore.IFace")' is not assignable to type 'MefTestsCore.IFace'.
Resulting in: Cannot set import 'MefTests.Program.iFaceObj (ContractName="MefTestsCore.IFace")' on part 'MefTests.Program'.
Element: MefTests.Program.iFaceObj (ContractName="MefTestsCore.IFace") --> MefTests.Program
In your test program you are using MefTestsCore. So I assume you referenced that assembly from your test program. But then you also load a copy from c:\Users\username. Now they types don't match because they come from different assemblies.
You have to put the interface into it's own assembly so that you then can reference it from your program and from MefTestsCore.

Error when trying to populate listbox

Hi I am a student I am getting this error:
forreach statement cannot operate on
variables of type 'object' because
'object' does not contain a public
definition for 'GetEnumerator'
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string result = " ";
foreach (string activity in listBox1.SelectedItems)
{
result += activity + " ";
}
this.textBox1.Text = result;
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add(textBox2.Text);
textBox2.Clear();
}
}
}
Pl. help me
Your code works fine for me. However, your code will break if you've added something other than a string to your ListBox's Items collection (although this will produce a different error than what you're apparently seeing).
Can you add the code you use to populate this ListBox?