I have a c# program1 that starts up another c# program2.exe and executes methods in it. I can't figure out how to debug it. Here is the code from the calling program1:
Task = new Process () ;
Task.StartInfo.FileName = Executable ;
Task.StartInfo.Arguments = AddQuotes (Msg) ;
if (GeneralRegistry.GetWaitForExit ())
{ State.WaitForExit () ;
}
bool OK = Task.Start () ;
Task.StartInfo.Filename is the name of the program2.exe that is executed.
Task.StartInfo.Arguments is xml that includes the name of another xml "model" file. The model defines variables, equations, and data. Program2.exe parses the xml model and executes it.
I'm using VS12 Express. I tried Debug>Attach to Process and attached to Program2.exe after it starts up. I click the "Go" menu item in Program2.exe to start the analysis. Now what? How do I set breakpoints and step through Program2.exe? I do not have the source code for it.
However, I do have source code for Program3. That program is somehow embedded in Program2.exe. Program2.exe has the UI and inputs but the core underlying work is done by Program3.
If both these programs are in the same solution, you can attach the exe to the debugger and step into program2 dll from program 1. Do you initialize program 2's object in program 1?
Related
I'm trying to set the Text value of the Clipboard to the value of a string variable in a WPF application on .NET 4.7.2. If I debug the application and stop the execution right after the clipboard value has been saved then the value is there in clipboard and I can paste it as expected. If I continue execution then the value is wiped of sorts. For example, this is code that I can use to test with:
public void CopyToClipboard()
{
string temp = "test\ntest\ntest";
System.Windows.Clipboard.SetText(temp);
}
As you can see there are three lines that should be pasted. When I stop execution right after saving to the clipboard I will see an output like so:
test
test
test
If I continue execution (by hitting F5 or the "Continue" button in Visual Studio) then when I paste I get three lines of '\t' tabs. So, my three lines still exist, but the text has been erased.
Can anyone help me to get this resolved. What is causing me to loose the data in the clipboard?
I wrote a quick Linqpad script that just consists of the following line:
System.Windows.Clipboard.SetText("Hello");
And my clipboard is successfully set to "Hello" and stays that way after the program exits. Is there something else in your application that is modifying the clipboard?
Check this
https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.clipboard.settext?redirectedfrom=MSDN&view=netframework-4.8#System_Windows_Forms_Clipboard_SetText_System_String_
public String SwapClipboardHtmlText(String replacementHtmlText)
{
String returnHtmlText = null;
if (Clipboard.ContainsText(TextDataFormat.Html))
{
returnHtmlText = Clipboard.GetText(TextDataFormat.Html);
Clipboard.SetText(replacementHtmlText, TextDataFormat.Html);
}
return returnHtmlText;
}
The Clipboard class can only be used in threads set to single thread
apartment (STA) mode. To use this class, ensure that your Main method
is marked with the STAThreadAttribute attribute.
I need to make the primary .exe unrunnable from it (When you try to start it directly ,you get a message : Cannot start directly,if it runs from the secondary exe (only it,must have a crc verification i think) then start .
Hope i make myself clear
First .exe can't start directly
Second .exe can start the first exe (only)
Make the first exe a DLL. Then the second program can use it but a user won't be able to run it directly.
Set up the EXE that can't be started directly to accept a parameter, such as a SHA-256 hash of some unique data from the one that's supposed to start it. If that parameter doesn't exist or is not what's expected, display an error and exit.
EDIT:
static class Program
{
static void Main(params string[] args) //<- first needed change
{
if(args.Length == 0 || args[0] != "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
{
Console.WriteLine("Cannot execute this program directly.")
return;
}
... //rest of main function
}
}
The simplest way to do that is to have command line parameters, or beter still, set an environment variable and run it, so theres little way to trace the requirements from a "can I get round the fact you want me to use your app to run it". However, I would say a DLL would be the way to go really.
I am not sure if the process name (Process.GetCurrentProcess().ProcessName) would give you the 1st or the second EXE name but you can make the 1st EXE as a DLL and the second as an EXE.
Okay so for example I have 2 projects, Form1.exe & console1.exe, what I want is to execute console1.exe within form1.exe, so I used,
if (isLogin(true))
{
MessageBox.Show($"Welcome!");
Process.Start("console1.exe");
Now the problem with this is, in order to execute console1.exe the user must store that file locally, now the issue with that is, he/she can execute console1.exe without executing form1.exe
So what I did was used ILMerge to merge the .exe's as you would a .dll but it's unable to now find console1.exe, so merging didn't work.
So now my conclusion was to reopen visual studio & create a reference .dll out of console1.exe.
Problem with this is executing the .dll as if it were still a .exe without calling methods separately, so to execute the file as if it were one method.
So basically executing the parent class, which will then follow up with the sub classes.
Now is there anyway I can reference console1.exe and merge it to form1.exe preventing users from running it as a solo application, or is it possible to set rules on console1.exe that it may not execute unless called from form1.exe, or the second conclusion to run it as a .dll reference library in one command not multiple sub classes.
You have two options if you need to start it as a process. Both of which are not fool proof because they use a simple technique of obfuscation.
You change the file extension of the console1.exe to something like console1.notAnExe. You can still start this as an application using the Process.Start() method. You just need to create a ProcessStartInfo object and set the UseShellExecute property to false. Beware because this option means anyone with access to change the extension back to .exe can run the application.
private static void StartAProcess(string proc)
{
var ps = new ProcessStartInfo(proc);
ps.UseShellExecute = false;
var p = Process.Start(ps);
p.WaitForExit();
}
You use a simple technique within your console1.exe start up method Main(string[] args) that reads in a special command line parameter. If this command line parameter exists, you continue processing. If the command line parameter does not exists, you just return. Beware because anyone that knows the command line parameter can still start your console1.exe and pass that command line parameter within a shortcut.
static void Main(string[] args)
{
if (args == null ||
args.Length <= 0 ||
!args[0].Equals(
"/SpecialParameter",
StringComparison.OrdinalIgnoreCase))
{
return;
}
// Do something if the /SpecialParameter exists.....
}
private static void StartAProcess(string proc)
{
var ps = new ProcessStartInfo(proc);
ps.UseShellExecute = false;
ps.Arguments = "/SpecialParameter";
var p = Process.Start(ps);
p.WaitForExit();
}
I've created a simple program for moving files around in a system running win 7 embedded. I've run into a strange "bug" with my software and the way it handles startups.
static void Main(string[] args)
{
if (Flagger.GetFlag().Contains("Processing") || args.Contains("batch"))
{
Run();
}
return;
}
The way I've chosen to handle the different ways this program will be executed, I've created a simple way of seeing whether it is being executed as a part of the systems startup procedure, or called by a batch file.
The batch file is meant to be called by a trigger in the SQL-server and run a handful of programs for logging and such. While testing this on my workstation it executes and passes the parameters like it is supposed to do, but in the embedded system, no parameters is given to the program through the batch file.
start Pack.exe -batch
exit
I have tried several different methods of writing the batch file(with/without citation marks, start-exit) but to no avail. What could be causing the batch file to not pass the arguments to the Packer?
Starts a separate window to run a specified program or command.
START ["title"] [/Dpath] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
[/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
[/WAIT] [/B] [command/program]
[parameters]
Could it be, that you're passing qouted string as the first parameter to start command? If so, it handles it like a window title. Compare
start "c:\windows\notepad.exe"
and this
start "test" "c:\windows\notepad.exe"
Then you should just add a title param to start program. Also consider to use cmd /C instead.
I need to make the primary .exe unrunnable from it (When you try to start it directly ,you get a message : Cannot start directly,if it runs from the secondary exe (only it,must have a crc verification i think) then start .
Hope i make myself clear
First .exe can't start directly
Second .exe can start the first exe (only)
Make the first exe a DLL. Then the second program can use it but a user won't be able to run it directly.
Set up the EXE that can't be started directly to accept a parameter, such as a SHA-256 hash of some unique data from the one that's supposed to start it. If that parameter doesn't exist or is not what's expected, display an error and exit.
EDIT:
static class Program
{
static void Main(params string[] args) //<- first needed change
{
if(args.Length == 0 || args[0] != "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")
{
Console.WriteLine("Cannot execute this program directly.")
return;
}
... //rest of main function
}
}
The simplest way to do that is to have command line parameters, or beter still, set an environment variable and run it, so theres little way to trace the requirements from a "can I get round the fact you want me to use your app to run it". However, I would say a DLL would be the way to go really.
I am not sure if the process name (Process.GetCurrentProcess().ProcessName) would give you the 1st or the second EXE name but you can make the 1st EXE as a DLL and the second as an EXE.