Inserting uris into Gtk.Clipboard with vala - linux

I am currently trying to implement copy and paste for my application, the problem is that i can only plaintext or images to the clipboard according to the documentation of Gtk.Clipboard: https://valadoc.org/gtk+-3.0/Gtk.Clipboard.html set_text / set_image.
But then there is also this method https://valadoc.org/gtk+-3.0/Gtk.Clipboard.set_with_data.html set_with_data, which i think i can use for adding a uri or an array of uris. But i can't figure out how and didn't find any good examples either.
UPDATE
Using the given answer i can fill the clipboard with an array of uris, but i can read them, when i try it just calls the get_func again and refills it.
CTRL C pressed
clipboard get_func called
Received: file:///home/marcel/Downloads/.gitignore
CTRL V pressd
clipboard get_func called
Received: file:///home/marcel/Downloads
Try Pasting: file:///home/marcel/Downloads
This is the code i use for testing CTRL + V:
print ("\nCTRL V pressd\n");
clipboard.request_uris ((clipboard, uris) => {
foreach ( string content in uris ) {
print ("Try Pasting: ");
print (content);
print ("\n");
}
});
and this is the relevant part of the get_func for CTRL + C:
clipboard.set_with_owner (
clipboard_targets,
(clipboard, selection_data, info, user_data_or_owner) => {
print ("clipboard get_func called\n");
var w = user_data_or_owner as Window;
File[] files = { w.get_selected_file () };
switch ( info ) {
case ClipboardProtocol.TEXT_URI_LIST:
print ("Received: ");
string[] uris = {};
foreach ( var file in files ) {
print (file.get_uri ());
print ("\n");
uris += file.get_uri ();
}
selection_data.set_uris (uris);
break;
As you can see in the terminal output above, it just refills the clipboard, throwing away its previous values.

As requested I am providing both an example for writing URIs to clipboard and getting URIs from clipboard. These examples are basically command line programs that get / set the clipboard immediately. In an actual GUI application you would probably react to a button press or, to catch CtrlC / CtrlV events, use Gtk.Widget.add_events() and get / set the clipboard when handling the Gtk.Widget.event signal.
Getting the clipboard
You can request URIs from the X11 clipboard using Gtk.Clipboard.request_uris (). This function takes a callback that will be called once the URIs are available.
Example:
public void main (string[] args) {
Gtk.init (ref args);
Gdk.Display display = Gdk.Display.get_default ();
Gtk.Clipboard clipboard = Gtk.Clipboard.get_for_display (display, Gdk.SELECTION_CLIPBOARD);
clipboard.request_uris (recieved_func);
Gtk.main ();
}
/* Gtk.ClipboardURIRecievedFunc */
private void recieved_func (Gtk.Clipboard clipboard, string[] uris) {
foreach (var uri in uris) {
print (uri + "\n");
}
Gtk.main_quit ();
}
To be compiled with valac clipget.vala --pkg=gtk+-3.0
Setting the clipboard
Theory:
From the Qt4 documentation:
Since there is no standard way to copy and paste files between
applications on X11, various MIME types and conventions are currently
in use. For instance, Nautilus expects files to be supplied with a
x-special/gnome-copied-files MIME type with data beginning with the
cut/copy action, a newline character, and the URL of the file.
Gtk.Clipboard does not pre-implement setting the clipboard for copying / cutting files. As you said, there is no such Gtk.Clipboard.set_uris().
Instead, you should set the clipboard by providing a callback that X11 gets the clipboard contents from once requested.
These are the steps required:
Create a bunch of Gtk.TargetEntrys that specify what clipboard protocols your app can handle. You'll want to handle the protocolstext/uri-list, x-special/gnome-copied-files and UTF8_STRING. Each TargetEntry is identified by its info field, so that number should be unique (see enum ClipboardProtocol in the example below)
Implement a method of the type Gtk.ClipboardGetFunc. This method should fill the Gtk.SelectionData object that is passed with the file paths to copy / cut. Check for the info parameter to set the SelectionData argument according to the protocol specified.
Register the callback and the protocols implemented to X11 using Gtk.Clipboard.set_with_owner or Gtk.Clipboard.set_with_data
Example:
enum ClipboardProtocol {
TEXT_URI_LIST,
GNOME_COPIED_FILES,
UTF8_STRING
}
public void main (string[] args) {
Gtk.init (ref args);
Gdk.Display display = Gdk.Display.get_default ();
Gtk.Clipboard clipboard = Gtk.Clipboard.get_for_display (display, Gdk.SELECTION_CLIPBOARD);
var clipboard_targets = new Gtk.TargetEntry[3];
Gtk.TargetEntry target_entry = { "text/uri-list", 0, ClipboardProtocol.TEXT_URI_LIST };
clipboard_targets[0] = target_entry;
target_entry = { "x-special/gnome-copied-files", 0, ClipboardProtocol.GNOME_COPIED_FILES };
clipboard_targets[1] = target_entry;
target_entry = { "UTF8_STRING", 0, ClipboardProtocol.UTF8_STRING };
clipboard_targets[2] = target_entry;
var owner = new Object ();
var rc = clipboard.set_with_owner (
clipboard_targets,
get_func,
clear_func,
owner
);
assert (rc);
clipboard.store ();
Gtk.main ();
}
/* Gtk.ClipboardGetFunc */
private void get_func (
Gtk.Clipboard clipboard,
Gtk.SelectionData selection_data,
uint info,
void* user_data_or_owner
) {
print ("GET FUNC!\n");
File my_file = File.new_for_path ("/home/lukas/tmp/test.txt");
File my_2nd_file = File.new_for_path ("/home/lukas/tmp/test2.txt");
File[] files = { my_file, my_2nd_file };
switch (info) {
case ClipboardProtocol.TEXT_URI_LIST:
string[] uris = {};
foreach (var file in files) {
uris += file.get_uri ();
}
selection_data.set_uris (uris);
break;
case ClipboardProtocol.GNOME_COPIED_FILES:
var prefix = "copy\n";
//var prefix = "cut\n";
/* use one of the above */
var builder = new StringBuilder (prefix);
for (int i = 0; i < files.length; i++) {
builder.append (files[i].get_uri ());
/* dont put the newline if this is the last file */
if (i != files.length - 1)
builder.append_c ('\n');
}
selection_data.set (
selection_data.get_target (),
8,
builder.data
);
break;
case ClipboardProtocol.UTF8_STRING:
var builder = new StringBuilder ();
foreach (var file in files) {
builder.append (file.get_parse_name ());
}
builder.append_c ('\n');
selection_data.set_text (builder.str, -1);
break;
default:
assert_not_reached ();
}
Gtk.main_quit ();
}
/* Gtk.ClipboardClearFunc */
private void clear_func (Gtk.Clipboard clipboard, void* data) {
;
}
To be compiled with valac clipset.vala --pkg=gtk+-3.0
A couple of notes:
In my example, I could only test x-special/gnome-copied-files since I only have Nautilus installed at the moment. I adapted all of the protocols from the Thunar source code (see sources below) but they might still require troubleshooting*
If you do not want to go through the trouble of implementing this yourself, you could also use the xclip command line tool: https://askubuntu.com/a/210428/345569 However, IMHO implementing this yourself is a little more elegant.
Sources:
Article from the Ubuntu Forums: https://ubuntuforums.org/archive/index.php/t-2135919.html
Thunar source code (especially thunar/thunar/thunar-clipboard-manager.c): https://github.com/xfce-mirror/thunar/blob/3de231d2dec33ca48b73391386d442231baace3e/thunar/thunar-clipboard-manager.c
Qt4 documentation: http://doc.qt.io/archives/qt-4.8/qclipboard.html

Related

WebView2 - Selecting text and copying to clipboard

With the CHtmlView class I was able to select all the text and copy to the clipboard like this:
void CChristianLifeMinistryHtmlView::CopyToClipboard()
{
ExecWB(OLECMDID_COPY, OLECMDEXECOPT_DONTPROMPTUSER, nullptr, nullptr);
}
void CChristianLifeMinistryHtmlView::SelectAll()
{
ExecWB(OLECMDID_SELECTALL, OLECMDEXECOPT_DONTPROMPTUSER, nullptr, nullptr);
}
I am trying to find out how to do the same with the new WebView2 API.
Update
The WebView2 control supports by design:
CTRL + A to select all the content.
CTRL + C to copy the selected text to the clipboard.
I found a VB.NET solution to programatically copy all the page to the clipboard:
Sub Async GetText()
v = Await wv.ExecuteScriptAsync("document.body.innerText")
End Sub
But I am using Visual C++ and I do not see this method exposed. Also, I am not sure it is what I want because I do not want to copy as plain text data but HTML data (suitable for pasting in Word) like with the hotkeys. I have made a GitHub issue for this too.
Update
So I have now tried this code but it does not appear to do anything:
void CWebBrowser::CopyToClipboard()
{
if (m_pImpl->m_webView != nullptr)
{
m_pImpl->m_webView->ExecuteScript(_T("document.body.innerText"), nullptr);
}
}
Update
According to this article it states:
Alternately, for ICoreWebView2::ExecuteScript, you provide an instance that has an Invoke method that provides you with the success or failure code of the ExecuteScript request. Also provide the second parameter that is the JSON of the result of running the script.
Trying to find example.
Update
I now understand that I need to do something like this:
void CWebBrowser::CopyToClipboard()
{
if (m_pImpl->m_webView != nullptr)
{
m_pImpl->m_webView->ExecuteScript(L"document.body.innerText",
Callback<ICoreWebView2ExecuteScriptCompletedHandler>(
[](HRESULT error, PCWSTR result) -> HRESULT
{
if (error != S_OK) {
ShowFailure(error, L"ExecuteScript failed");
}
SetClipboardText(result);
AfxMessageBox(L"HTML copied to clipboard!", MB_OK);
return S_OK;
}).Get());
}
}
The variable result has the contents of the HTML page. But it does not like my call to SetClipboardText now. This is the error:
This is the function:
void CWebBrowser::SetClipboardText(CString strText)
{
BYTE* pbyText;
TCHAR* pcBuffer;
HANDLE hText;
UINT uLength;
//USES_CONVERSION ;
if (::OpenClipboard(nullptr))
{
// Empty it of all data first.
::EmptyClipboard();
// Replace previous text contents.
uLength = strText.GetLength();
//pcBuffer = T2A( (LPTSTR)(LPCTSTR)strText);
pcBuffer = strText.GetBuffer(uLength);
if (pcBuffer != nullptr)
{
hText = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (uLength + 1) * sizeof(TCHAR));
if (hText != nullptr)
{
pbyText = (BYTE*)::GlobalLock(hText);
if (pbyText != nullptr)
{
// Deliberately not _tcscpy().
//strcpy_s( (char *)pbyText, uLength+1, pcBuffer);
_tcscpy_s((TCHAR*)pbyText, uLength + 1, pcBuffer);
::GlobalUnlock(hText);
::SetClipboardData(CF_UNICODETEXT, hText);
// Don't free this memory, clipboard owns it now.
}
}
}
::CloseClipboard();
strText.ReleaseBuffer(uLength);
}
}
This is the only way I know (to date) to copy to clipboard. So I have two issues here:
It will not let me use this function.
I expect it will copy just text (CF_UNICODETEXT) and I don't know if that is sufficient for pasting the data as HTML into Word?
Concerning issue 1, I needed to make the method static. Then it compiled and copied the information to the clipboard.
Concerning issue 2, as expected, the pasted data was stripped HTML and I was left with just the text. And the new line characters were displayed as "\n" in the text. It was one huge paragraph. So I do indeed need the CF_HTML format.
It is a pity that the WebView2 does not expose the copy to clipboard feature that it already has functional.
Update
This is the clipboard method that I found on the internet for CF_HTML:
// CopyHtml() - Copies given HTML to the clipboard.
// The HTML/BODY blanket is provided, so you only need to
// call it like CopyHtml("<b>This is a test</b>");
void CopyHTML(char* html)
{
// Create temporary buffer for HTML header...
char* buf = new char[400 + strlen(html)];
if (!buf) return;
// Get clipboard id for HTML format...
static int cfid = 0;
if (!cfid) cfid = RegisterClipboardFormat("HTML Format");
// Create a template string for the HTML header...
strcpy(buf,
"Version:0.9\r\n"
"StartHTML:00000000\r\n"
"EndHTML:00000000\r\n"
"StartFragment:00000000\r\n"
"EndFragment:00000000\r\n"
"<html><body>\r\n"
"<!--StartFragment -->\r\n");
// Append the HTML...
strcat(buf, html);
strcat(buf, "\r\n");
// Finish up the HTML format...
strcat(buf,
"<!--EndFragment-->\r\n"
"</body>\r\n"
"</html>");
// Now go back, calculate all the lengths, and write out the
// necessary header information. Note, wsprintf() truncates the
// string when you overwrite it so you follow up with code to replace
// the 0 appended at the end with a '\r'...
char* ptr = strstr(buf, "StartHTML");
wsprintf(ptr + 10, "%08u", strstr(buf, "<html>") - buf);
*(ptr + 10 + 8) = '\r';
ptr = strstr(buf, "EndHTML");
wsprintf(ptr + 8, "%08u", strlen(buf));
*(ptr + 8 + 8) = '\r';
ptr = strstr(buf, "StartFragment");
wsprintf(ptr + 14, "%08u", strstr(buf, "<!--StartFrag") - buf);
*(ptr + 14 + 8) = '\r';
ptr = strstr(buf, "EndFragment");
wsprintf(ptr + 12, "%08u", strstr(buf, "<!--EndFrag") - buf);
*(ptr + 12 + 8) = '\r';
// Now you have everything in place ready to put on the clipboard.
// Open the clipboard...
if (OpenClipboard(0))
{
// Empty what's in there...
EmptyClipboard();
// Allocate global memory for transfer...
HGLOBAL hText = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, strlen(buf) + 4);
// Put your string in the global memory...
char* ptr = (char*)GlobalLock(hText);
strcpy(ptr, buf);
GlobalUnlock(hText);
::SetClipboardData(cfid, hText);
CloseClipboard();
// Free memory...
GlobalFree(hText);
}
// Clean up...
delete[] buf;
}
But it is not compatible with the PCWSTR variable.
Update
I have now realised after doing debugging that ExecuteScript does return the actual HTML data if I use document.body.innerText. The resulting string is just text with \n characters for the new lines. It is not in HTML format. So I am back to square one.
Just use ICoreWebView2::ExecuteScript.
I am showing this as an alternative because the current approach does not include the actual HTML content. The information I found on the internet was misleading about using document.body.innerText.
One solution to this is to use the document.execCommand() route. I added the following methods to my browser class:
void CWebBrowser::SelectAll()
{
if (m_pImpl->m_webView != nullptr)
{
m_pImpl->m_webView->ExecuteScript(L"document.execCommand(\"SelectAll\")", nullptr);
}
}
void CWebBrowser::CopyToClipboard2()
{
if (m_pImpl->m_webView != nullptr)
{
m_pImpl->m_webView->ExecuteScript(L"document.execCommand(\"Copy\")", nullptr);
}
}
Then I added the following button handlers to my dialog to test:
void CMFCTestWebView2Dlg::OnBnClickedButtonSelectAll()
{
if (m_pWebBrowser != nullptr)
{
m_pWebBrowser->SelectAll();
}
}
void CMFCTestWebView2Dlg::OnBnClickedButtonCopyToClipboard()
{
if (m_pWebBrowser != nullptr)
{
m_pWebBrowser->CopyToClipboard2();
}
}
This does work and appears to be the simplest solution for now. I see references to the Calendar API as an alternative but I am not sure yet how to implement that approach.
My only concern with execCommand is that I have seen it documented as deprecated.

How to hide the cursor in Awesomium

I tried this:
<awe:WebControl x:Name="webBrowser" Cursor="None" Source="http://example.com/"/>
but the cursor still shows.
I figured that I could alter the CSS of the page by adding the following line:
*{
cursor: none;
}
But, is there a solution for when I don't have the access to the actual page that I'm showing?
You can use a ResouceInterceptor and manipulate the page on the fly to insert custom CSS.
EDIT:
The following implementation should do the job. (It assumes there is a text.css file)
class ManipulatingResourceInterceptor : IResourceInterceptor
{
public ResourceResponse OnRequest(ResourceRequest request)
{
Stream stream = null;
//do stream manipulation
if (request.Url.ToString() == "http://your.web.url/test.css")
{
WebRequest myRequest;
myRequest = WebRequest.Create(request.Url);
Stream webStream = myRequest.GetResponse().GetResponseStream();
StreamReader webStreamReader = new StreamReader(webStream);
string webStreamContent = webStreamReader.ReadToEnd();
stream = webStream;
string extraContent = "*{cursor: none;}";
webStreamContent += extraContent;
byte[] responseBuffer = Encoding.UTF8.GetBytes(webStreamContent);
// Initialize unmanaged memory to hold the array.
int responseSize = Marshal.SizeOf(responseBuffer[0]) * responseBuffer.Length;
IntPtr pointer = Marshal.AllocHGlobal(responseSize);
try
{
// Copy the array to unmanaged memory.
Marshal.Copy(responseBuffer, 0, pointer, responseBuffer.Length);
return ResourceResponse.Create((uint)responseBuffer.Length, pointer, "text/css");
}
finally
{
// Data is not owned by the ResourceResponse. A copy is made
// of the supplied buffer. We can safely free the unmanaged memory.
Marshal.FreeHGlobal(pointer);
stream.Close();
}
}
return null;
}
public bool OnFilterNavigation(NavigationRequest request)
{
return false;
}
}

wxDirDialog Returns the Wrong Directory on Vista

I recently ported the following code to wx3.0 under visual studio 2013:
void PanelFileControl::on_retrieve_clicked(wxCommandEvent &event)
{
if(!chosen_files.empty())
{
Csi::ModalCounter counter;
wxDirDialog query(
this,
make_wxString(my_strings[strid_choose_retrieve_dir]),
make_wxString(wxGetApp().get_config()->get_last_prog_dir()));
int rcd;
query.CentreOnParent();
rcd = query.ShowModal();
if(rcd == wxID_OK)
{
// we need to generate an operation for every selected file.
StrAsc path(make_StrAsc(query.GetPath()));
DlgFileControl::operations_type operations;
if(path.last() != Csi::FileSystemObject::dir_separator())
path.append(Csi::FileSystemObject::dir_separator());
for(files_type::iterator fi = chosen_files.begin(); fi != chosen_files.end(); ++fi)
{
file_type const &file(*fi);
StrAsc file_path(path + file.get_file_name());
bool use_file(true);
if(Csi::file_exists(file_path.c_str()))
{
OwxStringStream message;
message << boost::format(my_strings[strid_overwrite_file_confirm].c_str()) %
file_path;
wxMessageDialog overwrite_query(
this,
message.str(),
wxT(""),
wxYES_NO | wxNO_DEFAULT | wxICON_QUESTION);
int rcd;
overwrite_query.CentreOnParent();
rcd = overwrite_query.ShowModal();
if(rcd != wxID_YES)
use_file = false;
}
if(use_file)
operations.push_back(new ReceiveFileOperation(file, file_path));
}
// we can now display the operation dialogue
if(!operations.empty())
{
DlgFileControl dialogue(this, device_name, operations);
dialogue.show_modal();
}
}
}
} // on_retrieve_clicked
Following this change (which didn't require any changes to the code above), I have received complaints that, if the user selects the desktop and then double clicks on a directory on the desktop, that the file save operation fails. This appears to be a result of the path produced by the wxDirDialog::GetPath() and has only been seen under windows vista. I have followed up some testing and I find that, under Vista, the last path component is mentioned twice in the string returned by GetPath().
Has anyone seen this issue? Are there any work arounds?
I found that I can address the issue by preventing the wxDirDialog from using the IFILEDIALOG interface from being used. My ShowModal() method now looks like this:
int wxDirDialog::ShowModal()
{
WX_HOOK_MODAL_DIALOG();
wxWindow* const parent = GetParent();
WXHWND hWndParent = parent ? GetHwndOf(parent) : NULL;
// Use IFileDialog under new enough Windows, it's more user-friendly.
int rc;
#if wxUSE_IFILEDIALOG
if ( wxGetWinVersion() > wxWinVersion_Vista )
{
rc = ShowIFileDialog(hWndParent);
}
else
{
rc = wxID_NONE;
}
if ( rc == wxID_NONE )
#endif // wxUSE_IFILEDIALOG
{
rc = ShowSHBrowseForFolder(hWndParent);
}
// change current working directory if asked so
if ( rc == wxID_OK && HasFlag(wxDD_CHANGE_DIR) )
wxSetWorkingDirectory(m_path);
return rc;
}

Console application: How to update the display without flicker?

Using C# 4 in a Windows console application that continually reports progress how can I make the "redraw" of the screen more fluid?
I'd like to do one of the following:
- Have it only "redraw" the part of the screen that's changing (the progress portion) and leave the rest as is.
- "Redraw" the whole screen but not have it flicker.
Currently I re-write all the text (application name, etc.). Like this:
Console.Clear();
WriteTitle();
Console.WriteLine();
Console.WriteLine("Deleting:\t{0} of {1} ({2})".FormatString(count.ToString("N0"), total.ToString("N0"), (count / (decimal)total).ToString("P2")));
Which causes a lot of flickering.
Try Console.SetCursorPosition. More details here: How can I update the current line in a C# Windows Console App?
static void Main(string[] args)
{
Console.SetCursorPosition(0, 0);
Console.Write("################################");
for (int row = 1; row < 10; row++)
{
Console.SetCursorPosition(0, row);
Console.Write("# #");
}
Console.SetCursorPosition(0, 10);
Console.Write("################################");
int data = 1;
System.Diagnostics.Stopwatch clock = new System.Diagnostics.Stopwatch();
clock.Start();
while (true)
{
data++;
Console.SetCursorPosition(1, 2);
Console.Write("Current Value: " + data.ToString());
Console.SetCursorPosition(1, 3);
Console.Write("Running Time: " + clock.Elapsed.TotalSeconds.ToString());
Thread.Sleep(1000);
}
Console.ReadKey();
}
I know this question is a bit old but I found if you set Console.CursorVisible = false then the flickering stops as well.
Here's a simple working demo that shows multi-line usage without flickering. It shows the current time and a random string every second.
private static void StatusUpdate()
{
var whiteSpace = new StringBuilder();
whiteSpace.Append(' ', 10);
var random = new Random();
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var randomWord = new string(Enumerable.Repeat(chars, random.Next(10)).Select(s => s[random.Next(s.Length)]).ToArray());
while (true)
{
Console.SetCursorPosition(0, 0);
var sb = new StringBuilder();
sb.AppendLine($"Program Status:{whiteSpace}");
sb.AppendLine("-------------------------------");
sb.AppendLine($"Last Updated: {DateTime.Now}{whiteSpace}");
sb.AppendLine($"Random Word: {randomWord}{whiteSpace}");
sb.AppendLine("-------------------------------");
Console.Write(sb);
Thread.Sleep(1000);
}
}
The above example assumes your console window is blank to start. If not, make sure to use Console.Clear() first.
Technical Note:
SetCursorPosition(0,0) places the cursor back to the top (0,0) so the next call to Console.Write will start from line 0, char 0. Note, it doesn't delete the previous content before writing. As an example, if you write "asdf" over a previous line such as "0123456", you'll end up with something like "asdf456" on that line. For that reason, we use a whiteSpace variable to ensure any lingering characters from the previous line are overwritten with blank spaces. Adjust the length of the whiteSpace variable to meet your needs. You only need the whiteSpace variable for lines that change.
Personal Note:
For my purposes, I wanted to show the applications current status (once a second) along with a bunch of other status information and I wanted to avoid any annoying flickering that can happen when you use Console.Clear(). In my application, I run my status updates behind a separate thread so it constantly provides updates even though I have numerous other threads and long running tasks going at the same time.
Credits:
Thanks to previous posters and dtb for the random string generator used in the demo.
How can I generate random alphanumeric strings in C#?
You could try to hack something together using the core libraries.
Rather than waste your time for sub-standard results, I would check out this C# port of the ncurses library (which is a library used for formatting console output):
Curses Sharp
I think you can use \r in Windows console to return the beginning of a line.
You could also use SetCursorPosition.
I would recommend the following extension methods. They allow you to use a StringBuilder to refresh the console view without any flicker, and also tidies up any residual characters on each line
The Problem: The following demo demonstrates using a standard StringBuilder, where updating lines that are shorter than the previously written line get jumbled up. It does this by writing a short string, then a long string on a loop:
public static void Main(string[] args)
{
var switchTextLength = false;
while(true)
{
var sb = new StringBuilder();
if (switchTextLength)
sb.AppendLine("Short msg");
else
sb.AppendLine("Longer message");
sb.UpdateConsole();
switchTextLength = !switchTextLength;
Thread.Sleep(500);
}
}
Result:
The Solution: By using the extension method provided below, the issue is resolved
public static void Main(string[] args)
{
var switchTextLength = false;
while(true)
{
var sb = new StringBuilder();
if (switchTextLength)
sb.AppendLineEx("Short msg");
else
sb.AppendLineEx("Longer message");
sb.UpdateConsole();
switchTextLength = !switchTextLength;
Thread.Sleep(500);
}
}
Result:
Extension Methods:
public static class StringBuilderExtensions
{
/// <summary>
/// Allows StrinbBuilder callers to append a line and blank out the remaining characters for the length of the console buffer width
/// </summary>
public static void AppendLineEx(this StringBuilder c, string msg)
{
// Append the actual line
c.Append(msg);
// Add blanking chars for the rest of the buffer
c.Append(' ', Console.BufferWidth - msg.Length - 1);
// Finish the line
c.Append(Environment.NewLine);
}
/// <summary>
/// Combines two StringBuilders using AppendLineEx
/// </summary>
public static void AppendEx(this StringBuilder c, StringBuilder toAdd)
{
foreach (var line in toAdd.ReadLines())
{
c.AppendLineEx(line);
}
}
/// <summary>
/// Hides the console cursor, resets its position and writes out the string builder
/// </summary>
public static void UpdateConsole(this StringBuilder c)
{
// Ensure the cursor is hidden
if (Console.CursorVisible) Console.CursorVisible = false;
// Reset the cursor position to the top of the console and write out the string builder
Console.SetCursorPosition(0, 0);
Console.WriteLine(c);
}
}
I actually had this issue so I made a quick simple method to try and eliminate this.
static void Clear(string text, int x, int y)
{
char[] textChars = text.ToCharArray();
string newText = "";
//Converts the string you just wrote into a blank string
foreach(char c in textChars)
{
text = text.Replace(c, ' ');
}
newText = text;
//Sets the cursor position
Console.SetCursorPosition(x, y);
//Writes the blank string over the old string
Console.WriteLine(newText);
//Resets cursor position
Console.SetCursorPosition(0, 0);
}
It actually worked surprisingly well and I hope it may work for you!
Naive approach but for simple applications is working:
protected string clearBuffer = null; // Clear this if window size changes
protected void ClearConsole()
{
if (clearBuffer == null)
{
var line = "".PadLeft(Console.WindowWidth, ' ');
var lines = new StringBuilder();
for (var i = 0; i < Console.WindowHeight; i++)
{
lines.AppendLine(line);
}
clearBuffer = lines.ToString();
}
Console.SetCursorPosition(0, 0);
Console.Write(clearBuffer);
Console.SetCursorPosition(0, 0);
}
Console.SetCursorPosition(0, 0); //Instead of Console.Clear();
WriteTitle();
Console.WriteLine();
Console.WriteLine("Deleting:\t{0} of {1} ({2})".FormatString(count.ToString("N0")

How to modify exe file's entry point?

I'm trying to add a jmp instruction at the end of text section in the calc.exe for windows XP, and I've added it and modified the entry point to start from that address and modified the virtual size of the text section so that it can handle the added instruction, but the result exe didn't work. so am I missing any thing here?
here is the C# code I've wrote to handle those things:
public static void inject()
{
StreamReader sr = new StreamReader("C:\\calc.EXE");
BinaryReader br = new BinaryReader(sr.BaseStream);
List<byte> bytesList = new List<byte>();
for (long i = 0; i < br.BaseStream.Length; i++)
{
bytesList.Add(br.ReadByte());
}
{
// updating the entry point
bytesList[280] = 176;
bytesList[281] = 42;
bytesList[282] = 1;
bytesList[283] = 0;
}
{
bytesList[496] = 192;
}
{
// second jmp
bytesList.RemoveRange(76464, 5);
byte[] injectedBytes = { 233, 255, 255, 249, 192 };
bytesList.InsertRange(76464, injectedBytes);
}
StreamWriter sw = new StreamWriter("C:\\calc2.EXE");
BinaryWriter bw = new BinaryWriter(sw.BaseStream);
bw.Write(bytesList.ToArray());
bw.Close();
}
and thanks in advance
When you change (in your case Add/Enlarge) a Section (in your case the Code Section, aka the so-called text Section), you MUST also tell the Windows Loader to map this additional part into Memory by modifying the Section's associated Header (descriptor). In your case, it looks like you did not make this and the Loader won't load the new code (and in your case, it won't even load/start the application). Modifying Image Files is cool, but takes time to master.
Portable Executable File Format – A Reverse Engineer View shows how it works.

Resources