Start process as 64 bit - c#

I will like to show the onscreen keyboard. As a result I went to the following link on stackoverflow:
Calling the on-screen keyboard using a button in C#
That technique works great when I am running my wpf application targeting the x64 platform. In other words it works when I set:
but if I set it to 86x it does not work. I need to run my application as x86 though. How can I solve this problem? I just have an exception when running my code on a x64 bit computer...
Edit
I have temporary solution. What I did to solve this problem was to create a new project in Visual Studio (Console app) and named FooApplication targeting the x64 bit version. That program looks like:
Class Program
{
static void Main()
{
System.Diagnostics.Process.Start("osk.exe");
}
}
then I am able to start the calculator my executing FooApplication.exe from my original wpf application.

Related

Debugging .NET Core in macOS

We are migrating a gigantic solution to .NET Core. It builds and works in Windows and we can debug using Visual Studio without problems.
In macOS, however, we can run it, but still not build it due to some issues.
In the meantime I was trying to think of a way to debug code on Mac. Why doesn't this work?
Add this to one of my DLL files
// Start of my application
System.Diagnostics.Debugger.Launch()
// The rest of my application
Compile that DLL file and use it, alongside the PDB file, to generate my new application.
Navigate to that part of the code in Mac
Nothing happens. Unlike in Windows where that code allows me to attach a debugger.
How can I properly debug a part of my .NET Core code?
Yes, you can!
Install Visual Studio Code and then, instead of running "dotnet run" in the Terminal as always, you can start your API by pressing F5 (make sure you are not running in the terminal at the same time!)
Make sure you put a breakpoint in a place where you are going to use, and then hit an endpoint (for example, Postman).
I have installed the plugin "C#" from Microsoft, but I don't know if that helps...
Alternatively, you can try Visual Studio for Mac, but I found that this works in Visual Studio Code and I prefer it!
On Windows, you have an opportunity to hook up a debugger when the process hits a 'first chance exception'. The default debugger is specified in the registry, and you can find more information in - Configuring Automatic Debugging.
I don’t think that is possible in Mac OS X. Have a look at How does one automatically attach a debugger to a process at process start on OS X?
Also, I tried your code on macOS:
using System;
namespace DebugConsole
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Calling Debugger.Launch");
Console.ReadKey();
System.Diagnostics.Debugger.Launch();
Console.WriteLine("Continuing...");
Console.ReadKey();
Console.WriteLine("Hello, World!");
}
}
}
This builds successfully, but when System.Diagnostics.Debugger.Launch() is hit, on macOS the process does not hit the debugger. On a Windows machine it does. In short, this code should not be the reason why your code does not build on macOS.
Update:
Use Visual Studio Code to open your code. Visual Studio Code asks you to add file launch.json for the debugging configuration.
It will also download the .NET debugging extension which is required for obvious reasons.
You can launch your console application from here, but if you want to launch the process for some reason in the background and then hook up to it then you can do that as well. On the top you will see the menu next to the 'RUN' menu - this has two options. 'Dotnet core launch' will launch the process through Visual Studio Code and attach to it. 'Dotnet core attach' will attach to already running process.
If you select the 'attach' option, it asks you for the process to be attached. Refer to the screenshot below -
Please note that you will have to put a breakpoint in the IDE. As mentioned above, the debugger does not enter debug mode based on the Debugger.Launch() function call.

Android app debugs for a few seconds then just stops

I'm trying to set up a new Android app development environment at home and I've run into a problem that is a showstopper.
First, here is my environment:
Windows 10
Visual Studio Community 2015 with Xamarin platform installed
Test device is: G Pad 7.0 LTE
Android Version 5.0.2
I'm following this tutorial: MSDN tutorial
When I go to debug my test app, here is what happens.
Select debug from Visual Studio with my tablet as the target
VS deploys to the tablet
App starts on the tablet
App and debug session runs for about 5-10 seconds.
Debug session ends and app closes
No errors are displayed in Visual Studio and when i turn on Android logging it's a such a massive overflow of information that I don't really know where to begin searching for a problem.
Please also note that when I first set up the app, according to the tutorial, debugging on the tablet worked fine. The initial build is simply a template with a few basic controls.
In between then and when it stopped working I also added and successfully tested a Windows Phone project on a Windows Phone device.
Once I got to the section where I was modifying the project is when the problem started. I thought that perhaps a change in the code I did caused this issue so I tried another blank app and that blank app now crashes as well.
When I deploy the tutorial app to an emulator it crashes immediately but when I deploy a blank app it doesn't crash.
I also tried restoring the tablet to factory defaults and testing again. Same scenario: Blank app and tutorial app run for a few seconds, debugging stops, app closes.
Any clue as to what type of problem I'm looking at?
Solution
Clean your project (Right-Click on Project and select clean)
Go to your build folder. Probably in your Android project's root folder, named "obj". Delete either build you had, when you ran into this behavior (Debug or Release).
Directory Path
./AndroidProject/obj/Debug
./AndroidProject/obj/Release
Restart Visual Studio
Build and run again and it should work just fine, as the md5-hashes are now matching again.
I had also uninstalled the app before from the device and restarted it, I don't know whether this is required anymore, because the app is now being "reinstalled" by newer Xamarin versions anyway.
adb uninstall com.your.domain
Story and Background information
Have had the same issue with a device running Android 5.0.
Basically, the app was just stopping without any note.
When I now restarted the phone, The log was extended (once) by the following line:
Device could not find component named: *com.your.domain*/{*md5hash*}.*{Activity}
It turns out that with Andrpod 5.0 a hashing mechanism was implemented:
With the 5.0 release, the default package names for Android Callable
Wrappers will be based on the MD5SUM of the assembly-qualified name of
the type being exported. This allows the same fully-qualified name to
be provided from two different assemblies and not get a packaging
error.
See this for further information Xamarin.Android 5.1
So, if the md5-hash now changes from time to time, a simple Clean of your project is not enough.
I ran into this same issue and applied the same solution described above many times, which seemed to remedy this particular problem.
A possibly related fix, but definitely worthwhile note
After releasing this Android app and later releasing updates to the app, we got feedback from users that their Android Home screen shortcut to the app was disappearing after each app update. This is not good, for obvious reasons, but the fix for it was very simple as noted here:
dream-team-mobile-blog - Android app removes shortcut on update Xamarin solution
On the main activity, set a Name attribute of
<your package>.<your main activity>
like:
com.mycompany.myapp.MainActivity
For my solution, I chose to do it in code like this:
[Activity(Name = Constants.PackageName + "." + nameof(MainActivity),
Label = Constants.AppIconTitle, Icon = "#drawable/icon",
Theme = "#style/splashscreen", MainLauncher = true,
LaunchMode = LaunchMode.SingleInstance,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
//Side Note: I change my PackageName between Prod and SQA builds, so using
//a constant as seen above makes that change less risky to miss maintaining.
//Also, using nameof() keeps things tight and a sure match.
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
I mention it here in this post because, having this fix in place may have:
definitely prevented app shortcut loss and
possibly prevented the issue discussed in this post
as the MD5 hash prefix from the Main activity seems to no longer be generated.
This tweak may fix two bugs with one fix. If not, implementing a fixed main Activity Name will surely help your users keep their shortcut to your app when you release updates!
What fixed it for me in Visual Studio 2017 is going to :
Tools → Options → Xamarin → Android Settings
and uncheck:
Preserve Application data cache on device between deploys
go to your AndroidManifest.xml and change your package name.

Cannot Run .NET C# Application Outside Debug Folder

I'm having an odd issue with Visual Studio Community 2015. My program runs correctly inside the Visual Studio Editor, and it also runs correctly when the Application .exe is double clicked in the bin/debug/ directory. However, when the Application is run anywhere else on the computer (including the Release directory) the program does not run correctly. I am definitely copying all of the Debug folder to the location of attempted execution so it's not because of that.
When I run the executable, a loading icon appears on the mouse, in Task Manager it appears as a process using about < 500K RAM and stays there until reboot. No window appears at all, and it is impossible to end the process once it has started, meaning it cannot be deleted.
I'm using .NET 4.5.2 primarily however I've tried 4.5 and 4.6 and neither fixed the issue (I've checked and I have all 3 of them installed). I also tried copying the System DLLs locally and that didn't fix the issue either. I also tried creating a minimalist solution (below) containing a project that only printed "Hello World" and quit.
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
Console.ReadLine();
}
}
}
This program experienced the same issues except it couldn't even be run within the Debug folder, only within Visual Studio. I then changed the project so it opened, then closed again straight away without printing anything so that it didn't use the System libraries at all, and it did the same thing. I then tried running the program in both 64 and 32 bit forced, (on a 64 bit laptop) and that didn't make a difference either.
I'm using Windows 7 Home Premium - Visual Studio Community 2015
Thanks for any help :)
Another possibility for such a rare issue could be your Anti-Virus program. Some of them like to inject an DLL into each created process to better protect you. But this can also lead to such awkward problems.
The only other way that comes to my mind would be to check the Event Viewer or run Process Monitor to get an idea where the error comes from.
Try using the release configuration and enable Break on all exceptions (Check Managing Exceptions with the Debugger for help on enabling the VS exception handling).
Hopefully this will break visual studio and show you enough information on the exception.
You can also try using Process Monitor to find the issue.
I would highly recommend using a simple hello world program to find out the root cause. My suspicion is on some corrupt .NET framework installation
That's because it's not the proper exe...
In order to get a proper working .exe for you App Right Click on your project in Solution Explorer > Go to "publish" tab > select Publish Now
(Now go to debug folder and find exe)
It will automatically open the "publish" folder which has files for install, but NOW go to your "debug" folder and you will find the working exe there and some other Visual Studio generated files releated to publish that you don't really need
~ ChenChi

Building vs 2010 project in vs 2012 creates not always working executable

I've got this weird problem. On my machine, with both VS2010 and VS2012 installed, I've worked further on a VS2010 project from someone else, but I did it in VS2012 because I want all projects to use the same development environment.
I added some RFCs and fixed some bugs so I wanted to test this new software on another Windows 7 machine and it simply does nothing when I try to run the exe, not even log anything, the process just stays in the Processes overview about 10 seconds and then disappears again and nothing is logged.
I've tried my version again but first I added more, and earlier, logging with System.Diagnostics.Trace.WriteLine making the code look like this:
static class Program
{
internal static Denocard.DCLib.Global global = new Denocard.DCLib.Global(System.Reflection.Assembly.GetExecutingAssembly());
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
System.Diagnostics.Trace.WriteLine(string.Format("{0:yyyy-MM-dd HH:mm:ss.fff} - Program.main 1", DateTime.Now));
As that didn't log anything either I took the original Debug folder from the original project and tried that. It works on the test machine and logs into the log file, so I was wondering what would happen when I would build that original project.
So I copied the original project again, opened it in VS2012 and did a clean and build to see if the exe in that Debug folder would work implicating I messed something up. But that also didn't work ...
So, there must be something between VS 2010 and VS2012 that's messing things up, or it has to do with my computer. I've got Windows 7 Ultimate 64 bit and my best guess is that the previous developer used a Windows 7 32 bit system.
I've also tried another computer with Windows 7 32 bit to see if the problems weren't client-specific, but that didn't work either so it seems a build-machine / Visual Studio version problem to me.
Additional info:
- I've looked at the settings for the 3 projects that together turn into 1 application and at the configuration properties I see that 2 of them have 'Any CPU' as platform, but the main project has x86 as platform.
- the .Net version in the config file has been 4 all the time and both my computer as the test computer currently at my desk have version 4.0.30319 so that should be the same.
Anyone with an idea on what's going on here and / or how to solve this?
edit
Installed windbg on the system to get to the core exception or error. In Windbg it seems to be an CLR error, although it doesn't state which. In the Windows event log (found that tip here: Crash dump with unknown origin) it says System.TypeInitializationException at Program.Main(System.String[]). Now we're getting somewhere, although it doesn't say what type initialization is causing the exception ...

Deploying C# application on any computer

I am new to C#.
I have a program that I want to work on any computer.
How do I compile the application to work on any computer without dependencies?
A simple hello world let's say:
class MainApp {
public static void Main() {
// Write text to the console.
Console.WriteLine("Hello");
}
}
In order to run a .Net application, you need the .Net Framework installed on that computer.
If this is a problem for you, pick another language.
To compile the application, you can either use Visual Studio and choose the Build Solution from the Build menu.
You can also use the command line and csc.exe
To compile your application via csc you will need to execute the following statement in the command prompt.
csc YourFile.cs
Any computer attempting to execute the assembly will need the .NET framework installed.
The other computer will need the ability to run .NET programs.
There are two ways that I know of that you can run your software on someone elses computer assuming they meet the above requirement.
The first way is to go into the bin folder and get the executable
(or the .exe file) this can just be distributed and ran. This is ussually in a path simular to this C:\Users\username\Documents\Visual Studio 2012\Projects\YourProjName\YourProjName\bin
The second way is to create a click once application. How to create a click once
On a side note, if you want your program to run on almost any machine I would choose a different language. Java is pretty popular and it is VERY close to C#. Many colleges focus on Java as well so it gives you a leg up if you ever go to school for it. That said, I did my whole degree in Java and ended up in a .NET shop so I had to relearn a ton of stuff.... On second thought, just learn everything...

Categories