Copy result to clipboard - c#

Hi guys I write a­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­ console app:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
public static void Main(String[] args)
{
if (args.Length == 0)
{
Console.WriteLine("No file to upload...");
Environment.Exit(0);
}
else
Console.WriteLine("[~] Trying to upload: " + args[0]);
string name = Regex.Match(args[0], #"[^\\]*$").Value;
ftp ftpClient = new ftp(#"ftp://site.ru/", "dfgd", "QWERTY_123");
ftpClient.upload("www/site.ru/upload/" + name, args[0]);
Console.WriteLine("[+] Upload File Complete");
Console.ReadKey();
}
}
}
­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
How after Console.WriteLine("[+] Upload File Complete"); copy args[0] to clipboard?

First you must add a reference to System.Windows.Forms in your application.
Go to Project -> Add reference, select System.Windows.Forms from .NET tab in the window that just opened.
You must avoid the ThreadStateException by applying the STAThread attribute to your Main() function. Then you can use the Clipboard functions without any problems.
using System;
using System.Windows.Forms;
class Program
{
[STAThread]
static void Main(string[] args)
{
Clipboard.SetText("this is in clipboard now");
}
}
In case you dont want to use the reference to System.Windows.Forms, u can do it via P/Invoke
Platform Invoking the Clipboard APIs is a possible solution. Example:
using System.Runtime.InteropServices;
class Program
{
[DllImport("user32.dll")]
internal static extern bool OpenClipboard(IntPtr hWndNewOwner);
[DllImport("user32.dll")]
internal static extern bool CloseClipboard();
[DllImport("user32.dll")]
internal static extern bool SetClipboardData(uint uFormat, IntPtr data);
[STAThread]
static void Main(string[] args)
{
OpenClipboard(IntPtr.Zero);
var yourString = "Hello World!";
var ptr = Marshal.StringToHGlobalUni(yourString);
SetClipboardData(13, ptr);
CloseClipboard();
Marshal.FreeHGlobal(ptr);
}
}
This is just an example. Adding a little error handling around the code, like checking the return values of the P/Invoke functions would be a good addition.
SetClipboardData is the interesting bit, you also want to make sure you open and close the clipboard, too.
The 13 passed in as the first argument is the data format. 13 means unicode string.

The Marshal.StringToHGlobalUni function actually allocates memory in a fashion unsuitable for SetClipboardData (using LocalAlloc with LMEM_FIXED), which can cause crashes. (You wouldn't expect it given the method name, but stepping into the code e.g. using ReSharper reveals this.) SetClipboardData requires GlobalAlloc with GMEM_MOVABLE according to the docs: SetClipboardData on MSDN.
Here's an MIT licensed System.Windows.Forms alternative, tested and complete with error handling: Clippy
(the clipboard pushing code itself is to be found here: Clippy.cs

You need to use ClipBoard.SetText method
Clipboard.SetText(args[0], TextDataFormat.Text);

Related

AppContainer Unhandled Exception

I have entered this code in my Program.cs file.
AppContainer container = new AppContainer();
But when I try to debug it, it shows like this.
Anyone know how to solve this?
Consider using a nuget package to include the dependency for you.
Alternatively you could import the DLL manually using [DllImport("Win32Interop.User32").
Here's an excerpt from MSDN showing how to manually import and define methods to use from an imported DLL
using System;
using System.Runtime.InteropServices;
class Example
{
// Use DllImport to import the Win32 MessageBox function.
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);
static void Main()
{
// Call the MessageBox function using platform invoke.
MessageBox(new IntPtr(0), "Hello World!", "Hello Dialog", 0);
}
}

Why does my C# console application magically halt but resume upon pressing a key? [duplicate]

I have a very simple C# console application that displays some text and loops waiting for input until the escape key is pressed or the timeout period is served.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
namespace SampleApp
{
public static class Program
{
public static void Main (string [] args)
{
var key = new ConsoleKeyInfo();
var watch = Stopwatch.StartNew();
var timeout = TimeSpan.FromSeconds(5);
Console.WriteLine("Press escape to return to the previous screen...");
Console.WriteLine();
do
{
Console.WriteLine("This screen will automatically close in " + ((timeout + TimeSpan.FromSeconds(1)) - watch.Elapsed).ToString(#"hh\:mm\:ss") + ".");
if (Console.KeyAvailable) { key = Console.ReadKey(true); }
else { Thread.Sleep(TimeSpan.FromSeconds(0.10D)); }
}
while ((key.Key != ConsoleKey.Escape) && (timeout > (watch.Elapsed - TimeSpan.FromSeconds(0.5D))));
watch.Stop();
}
}
}
This works fine but if I click on the console app with the mouse (to gain focus for example), all activity on the screen freezes until I right click or press escape. During this time, the title of the console also changes to "Select AppName" assuming "AppName" was the title before.
If I right-click ion the console first, the do {...} while (); loop seems to go crazy and prints a lot of extra lines.
Since I am not aware of this behavior of the console, not sure what to ask. Is this to be expected? If so, can I change this behavior? If not, any suggestions for workarounds would be appreciated.
The issue was solved using Hans' comment above.
Sounds to me that you've activated the console window's Mark and Paste commands somehow. Normally activate through the system menu (Alt + Space, Edit, Mark/Paste). That doesn't have anything to do with this code of course.
Apparently, Quick Edit Mode was set in the console defaults (Alt + Space, Defaults, Options, Edit Options, Quick Edit Mode) for some reason. Unchecking that resolved the issue.
//call this class to disable quick edit mode.
public static void Main()
{
//disable console quick edit mode
DisableConsoleQuickEdit.Go();
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
static class DisableConsoleQuickEdit
{
const uint ENABLE_QUICK_EDIT = 0x0040;
// STD_INPUT_HANDLE (DWORD): -10 is the standard input device.
const int STD_INPUT_HANDLE = -10;
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll")]
static extern bool GetConsoleMode(IntPtr hConsoleHandle, out uint lpMode);
[DllImport("kernel32.dll")]
static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
internal static bool Go()
{
IntPtr consoleHandle = GetStdHandle(STD_INPUT_HANDLE);
// get current console mode
uint consoleMode;
if (!GetConsoleMode(consoleHandle, out consoleMode))
{
// ERROR: Unable to get console mode.
return false;
}
// Clear the quick edit bit in the mode flags
consoleMode &= ~ENABLE_QUICK_EDIT;
// set the new mode
if (!SetConsoleMode(consoleHandle, consoleMode))
{
// ERROR: Unable to set console mode
return false;
}
return true;
}
}

Loading a C++ DLL in C#

I am trying to use a DLL that was writen in C++ but my application is in C#
The DLL is from another company but they have supplied an SDK for their software.
They give an example of how to load their DLL in C++ but I need to adapt it to C#.
Below is their instructions of how to do it in C++
MarkEzd.dll file is Dynamic Link Library.
MarkEzdDll.h is header file of the exports function in MarkEzd.dll
The calling way of MarkEzd.dll is explicitly link. Developer needs to load and free MarkEzd.dll by calling Windows API function.
The steps are as follows.
Call Windows’ API function LoadLibrary() to load DLL dynamically;
Call Windows’ API function GetProcAddrress() to get the pointer of the functions in the DLL and use the function pointer to finish the work;
Call Windows’ API function FreeLibrary() to release library when you do not use DLL or the program ends.
Below is the example they have provided.
Step 2. Program software for calling markezd.dll.
a) First step : Dynamic Load MarkEzd.dll
HINSTANCE hEzdDLL = LoadLibrary(_T("MarkEzd.dll"));
b) Second step: get the pointer of the function to be called
lmc1_Initial=(LMC1_INITIAL)GetProcAddress(hEzdDLL, _T("lmc1_Initial"));
lmc1_Close=(LMC1_CLOSE)GetProcAddress(hEzdDLL, _T("lmc1_Close"));
lmc1_LoadEzdFile=(LMC1_LOADEZDFILE)GetProcAddress(hEzdDLL,_T("lmc1_LoadEzdFile"));
lmc1_Mark=(LMC1_MARK)GetProcAddress(hEzdDLL,_T("lmc1_Mark"));
c) Third step: Call the function
1) Initialization lmc1 board: lmc1_Initial().
2) Open test.ezd: lmc1_LoadEzdFile(_T(“test.ezd”)).
3) Call lmc1_Mark() for machining: lmc1_Mark().
4) Close lmc1 board: lmc1_Close().
d) Fourth step, Release markezd.dll: FreeLibrary(hEzdDLL)
Bellow is the descriptions of the commands.
lmc1_Initial
INTENTION: initialize lmc1 control board
DEFINITION: int lmc1_Initial(TCHAR* strEzCadPath, BOOL bTestMode, HWND hOwenWnd)
strEzCadPath: the full path where ezcad2.exe exists
bTestMode Whether in test mode or not
hOwenWnd: The window that has the focus. It is used to check the user’s stop messages.
DESCRIPTION: you must first call lmc1¬_Initial before other function in program.
RETURN VALUE: common error code
lmc1_Close
INTENTION: Close lmc1 board
DEFINITION: int lmc1_Close();
DESCRIPTION: you must call lmc1_Close to close the lmc1 board when exit program.
RETURN VALUE: common error code
lmc1_LoadEzdFile
INTENTION: open the appointed ezd file, and clear all the object in database.
DEFINITION: int lmc1_LoadEzdFile(TCHAR* strFileName);
DESCRIPTION: this function can open an ezd file that was build by user as a template. User need not set process parameters, because they will be loaded in from the template file.
RETURN VALUE: common error code
lmc1_Mark
INTENTION: mark all the data in database
DEFINITION: int lmc1_Mark(BOOL bFlyMark);
bFlyMark= TRUE // mark on fly
DISCRIPTION: Begin to mark by calling this function after loading ezd file using lmc1_LoadEzdFile. The function will not return back until marking complete.
RETURN VALUE: common error code
They also explain how to set up VS6.0
Choose “Microsoft Visual C++ 6.0” when install visual studio, and click “Change Option”.
Choose “VC++ MFC and Template Libraries” and click “Change Option”.
Choose “MS Foundation Class Libraries” and click “change option”.
Choose the options as following picture, and click “OK”.
Open the project, choose menu Project->Settings. Choose “C/C++”, add “UNICODE” and delete “MCBS” in “Preprocessor definitions”
Choose “Link”, select “Output” in Category, and add “wWinMainCRTStartup” in “Entry-point symbol”
Change all “char” to “TCHAR” in source code.
Change all character string included by double quotation marks “…” to _T(“…”)
Compile and link the project again.
most of the functions return an integer code of 0 for success.
Would this be correct?
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;
using System.Runtime.InteropServices;
namespace Start_Mark
{
public partial class Form1 : Form
{
[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("kernel32.dll")]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
[DllImport("kernel32.dll")]
public static extern bool FreeLibrary(IntPtr hModule);
[DllImport("MarkEzd.dll")]
[return: MarshalAs(UnmanagedType.I2)]
public static extern int lmc1_Initial(string strEzCadPath, bool bTestMode, IntPtr hOwenWnd);
[DllImport("MarkEzd.dll")]
[return: MarshalAs(UnmanagedType.I2)]
public static extern int lmc1_Close();
[DllImport("MarkEzd.dll")]
[return: MarshalAs(UnmanagedType.I2)]
public static extern int lmc1_LoadEzdFile(string strFileName);
[DllImport("MarkEzd.dll")]
[return: MarshalAs(UnmanagedType.I2)]
public static extern int lmc1_Mark(bool bFlyMark);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
IntPtr hEzdDLL = LoadLibrary("MarkEzd.dll");
IntPtr iplmc1_Initial = GetProcAddress(hEzdDLL, "lmc1_Initial");
IntPtr iplmc1_Close = GetProcAddress(hEzdDLL, "lmc1_Close");
IntPtr iplmc1_LoadEzdFile = GetProcAddress(hEzdDLL, "lmc1_LoadEzdFile");
IntPtr iplmc1_Mark = GetProcAddress(hEzdDLL, "lmc1_Mark");
int intlmc1_Initial=lmc1_Initial("c:\temp", false, hEzdDLL);
if (intlmc1_Initial > 0)
{
return;
}
int intlmc1_LoadEzdFile = lmc1_LoadEzdFile("c:\temp\test.ezd");
if (intlmc1_LoadEzdFile > 0)
{
return;
}
int intlmc1_Mark = lmc1_Mark(true);
if (intlmc1_Mark > 0)
{
return;
}
int intlmc1_Close = lmc1_Close();
if (intlmc1_Close > 0)
{
return;
}
FreeLibrary(hEzdDLL);
}
}
}
The correct syntax is as follows.
using System;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace Company.Group
{
public class FuncList
{
[DllImport("MarkEzd.dll", EntryPoint = "lmc1_Initial2", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
public static extern int Initialize(string PathName, bool TestMode);
}
}
Use P-Invoke to call native DLL. You might have to marshall some datatype in order to make it work.
http://msdn.microsoft.com/en-us/library/aa288468.aspx

How to access a window?

I'm trying to access a specific window using its handle (that is System.IntPtr value):
// Getting the process of Visual Studio program
var process = Process.GetProcessesByName("devenv")[0];
// Showing the handle we've got, we've no problem
MessageBox.Show(this, process.MainWindowHandle.ToString());
// Attempting to get the main window object by its handle
var wnd = NativeWindow.FromHandle(process.MainWindowHandle);
// always fails
if (wnd == null)
MessageBox.Show("Failed");
else
MessageBox.Show(wnd.ToString(), "Yeeeeees!!");
I have tried also to access another demo .net winforms application's main window, that I have made for this purpose, (i.e. I run the demo application, and attempted to access its main window by this application), and failed, too, although both the demo and this application are .NET applications. However, this successes:
var process2 = Process.GetCurrentProcess();
MessageBox.Show(this, process2.MainWindowHandle.ToString());
var wnd2 = NativeWindow.FromHandle(process2.MainWindowHandle);
if (wnd2 == null)
MessageBox.Show("Failed");
else
MessageBox.Show(wnd2.ToString(), "Yes");
I think this works because it is invoked from the same application. So, how can I access some another program's window object by its handle?
I thought it can work using C\C++ by using header file <windows.h> and then using a P\invoke.
If I can't, is there another way to access a window (i.e. rather than using handles)?
===================
EDIT
I want to deal with the entire window object and its own controls
Then, as Raymond suggested, why don't you try with Automation? Add a console project with references to UIAutomationClient and UIAutomationTypes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Automation;
using System.Diagnostics;
using System.Threading;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
var pInfo = new ProcessStartInfo("notepad");
var p = Process.Start(pInfo);
p.WaitForInputIdle();
AutomationElement installerEditorForm = AutomationElement.FromHandle(p.MainWindowHandle);
// menus
AutomationElementCollection menuBars = installerEditorForm.FindAll(TreeScope.Children, new PropertyCondition(
AutomationElement.ControlTypeProperty, ControlType.MenuBar));
var mainMenuItem = menuBars[0];
AutomationElementCollection menus = mainMenuItem.FindAll(TreeScope.Children, new PropertyCondition(
AutomationElement.ControlTypeProperty, ControlType.MenuItem));
var fileMenuItem = menus[0];
ExpandCollapsePattern fileMenuItemOpenPattern = (ExpandCollapsePattern)fileMenuItem.GetCurrentPattern(
ExpandCollapsePattern.Pattern);
fileMenuItemOpenPattern.Expand();
AutomationElement fileMenuItemNew = fileMenuItem.FindFirst(TreeScope.Children,
new AndCondition(
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.MenuItem),
new PropertyCondition(AutomationElement.NameProperty, "New")));
Console.Read();
}
}
}
reference
The documentation for NativeWindow.FromHandle explains why that function always returns null for you:
The handle must already be owned by another NativeWindow in the
current process; otherwise, null is returned.
But the window that you are targeting is in a different process. So you simply cannot use NativeWindow here. You will have to make do with the window handle as an IntPtr.
In your edit you state:
I want to deal with the entire window object and its own controls
That changes nothing. You can't use NativeWindow. You will have to deal with the raw Win32 API.
What do you want to access? You can get the title and text of windows in Windows. But you cant get a NativeWindow object of another application. You need to use the windows API to interact with other applications. I once hijacked an object in another app, but did so by knowing its class and discovering a hack to find its Idispatch pointer, you can look into it here. Below is how to get the title of the foreground window, hope this helps.
using System.Runtime.InteropServices;
using System.Text;
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
private string GetActiveWindowTitle()
{
const int nChars = 256;
IntPtr handle = IntPtr.Zero;
StringBuilder Buff = new StringBuilder(nChars);
handle = GetForegroundWindow();
if (GetWindowText(handle, Buff, nChars) > 0)
{
return Buff.ToString();
}
return null;
}
Thought I might add, if you're trying to subclass a window of another application, you should take a look at my link above. I believe the only way to do it is using DLL injection and windows hooks, exemplified in my example here.
Didn't get what you're really trying to do but maybe if you try...
public class ApiUtils
{
[DllImport("user32")]
public static extern int SetForegroundWindow(IntPtr hwnd);
[DllImport("user32.dll")]
public static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommand nCmdShow);
[DllImport("user32.dll")]
public static extern int GetForegroundWindow();
public static void ActiveWindow(IntPtr hwnd)
{
if ((IntPtr)GetForegroundWindow() != hwnd)
{
ShowWindow(hwnd, ShowWindowCommand.ShowMaximized);
}
}
}
now calling it...
Process p = Process.Start(new ProcessStartInfo() { FileName = "someApp.exe"});
ApiUtils.ShowWindow(p.MainWindowHandle, WindowShowStyle.ShowNormal);
If not sorry, didn't get the question really well.

Best practice to share a struct from a C# program to a C++ win32 DLL?

What is the best practice to share memory of a struct from a C# program to a C++ win32 DLL?
I've used structs in managed shared memory using Boost between two C++ programs and it worked great. I'm lost on the best way to accomplish this between where the struct gets populated in the C# program and the C++ DLL that is an SNMP subagent.
Here's the C++ DLL:
//==================== Code Excerpt from the main cpp file ======================
#include "stdafx.h"
//================= Here we are setting up the shared memory area =====================
#pragma data_seg (".SHAREDMEMORY")
struct sharedData {
int sharedA;
int sharedB;
};
static sharedData A;
#pragma data_seg()
#pragma comment(linker,"/SECTION:.SHAREDMEMORY,RWS")
BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved )
{
return TRUE;
}
//=============================================================================================
//====================== Here we are writing wrappers to the shared memory area ===========================
//=You must declare it as an Extern "C" to prevent name mangling. This is absolutely necessary in order to import it into c# =
//=============================================================================================
extern "C" __declspec(dllexport) sharedData __stdcall getMyData()
{
A.sharedA = 1237;
A.sharedB = 31337;
//return gshared_nTest;
return A;
}
extern "C" __declspec(dllexport) void __stdcall setMyData( sharedData buff )
{
A = buff;
}
Here's the calling C# function:
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;
using System.Runtime.InteropServices;
namespace sharedMemTestCS
{
public partial class frmSharedMemTestCS : Form
{
struct sharedData {
int sharedA;
int sharedB;
};
static sharedData A;
//============== here we are importing the methods from the win32 dll into the c# console application =================
[DllImport(#"C:\Documents and Settings\My Documents\Visual Studio 2010\Projects\sharedMemTestCPP\Debug\sharedMemTestCPP.dll")]
public static extern sharedData getMyData();
[DllImport(#"C:\Documents and Settings\My Documents\Visual Studio 2010\Projects\sharedMemTestCPP\Debug\sharedMemTestCPP.dll")]
public static extern void setMyData(int data);
public frmSharedMemTestCS()
{
InitializeComponent();
//============== here i am incrementing the value =================
//== i use a message box so that i can have multiple console applications running at once and it will pause at the messagebox (if i don't click ok)
//== i do this so i can see the values changing in the shared memory.
//MessageBox.Show( getMyData().ToString() );
getMyData();
//txtBoxA.Text = (getMyData().ToString() );
}
private void btnAdd_Click(object sender, EventArgs e)
{
//setMyData( getMyData() + 100 );
//txtBoxA.Text = (getMyData().ToString() );
}
}
}
The error message I get is:
Error 1 Inconsistent accessibility: return type
'sharedMemTestCS.frmSharedMemTestCS.sharedData' is less accessible
than method
'sharedMemTestCS.frmSharedMemTestCS.getMyData()' c:\documents and
settings\mconrad\my documents\visual studio
2010\Projects\sharedMemTestCS\sharedMemTestCS\Form1.cs 23 37 sharedMemTestCS
The best practice for sharing memory would be to use the MemoryMappedFile class in C# and CreateFileMapping/MapViewOfFile in C++.
First thing, you cannot straightway use Boost for data sharing. You need to have some well-defined data-structures that you share between the managed and unmanaged worlds.
You may start here
Well your actual problem is your p/invoke expression is public but your struct is private, which is what the error is telling you. Making your p/invoke expression private or your struct public will resolve the immediate issue.
As for the actual data sharing, I've never tried doing it quite like that so I can't tell you if it will or won't work. All the pieces I've worked with are marshalled back and forth. Looking at your example code, it's quite possible it could work. You'll probably want to either copy the data to a new struct for c# or pin your struct that you get back so the GC won't move it around in memory.

Categories

Resources