open exe in hidden mode in vc++ - winforms

I want to run an .exe file in hidden mode using vc++ in VS2010.
I have tried this code
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
Process^ p = Process::Start("Ai.exe");
IntPtr calculatorWindowHandle = p->MainWindowHandle;
void *calculatorWindowHandle1 = calculatorWindowHandle.ToPointer();
HWND pHandle = reinterpret_cast<HWND>(calculatorWindowHandle1);
ShowWindow(pHandle, SW_HIDE);
}
But it is opening the EXE but not in hidden mode.
Can anybody please tell me how it can be done.
Thanks in Advance

If Ai.exe is your own application, you can make it hidden just by not having any window in it. Make it Windows application (/SUBSYSTEM:Windows), but don't create any window.
If Ai.exe is not your application, then my question would be - What do you want to achieve? Why you want that application to be hidden?

Yes I found the solution.
I just require to use
Sleep(60);
After
Process^ p = Process::Start("Ai.exe");
because starting a process takes some milliseconds.

Related

AxInterop.ShockwaveFlashObjects missing in COM objects

I am trying to run SWF game in my WPF application. This game accepts some variables which should be constantly updated (about 100 times a second). I did some research and I know that basically there are 2 possibilities to run SWF in WPF:
Embed WebBrowser and pass path to the SWF file.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// fixes warning about ActiveX security
string C_Drive_local = "file://127.0.0.1/c$/";
// path of the flash file, here its in C:\DemoContent\bounce.swf
Uri swfPath = new Uri( C_Drive_local + "DemoContent/bounce.swf");
// load it in the browser
MySWFBrowser.Source = swfPath;
}
Use WindowsFormsHost to host an AxShockwaveFlash control
private void FlashLoaded(object sender, RoutedEventArgs e)
{
WindowsFormsHost formHost = new WindowsFormsHost();
AxShockwaveFlash axShockwaveFlash = new AxShockwaveFlash();
formHost.Child = axShockwaveFlash;
mainGrid.Children.Add(formHost);
string flashPath = Environment.CurrentDirectory;
flashPath += #"\game.swf";
axShockwaveFlash.Movie = flashPath;
}
I would like to try with AxShockwaveFlash since it provides methods for setting variables but in my COM objects I can not see AxInterop.ShockwaveFlashObjects.dll
I tried to install several different versions of Flash Player but without success. I cannt find any information about it. How can I get AxInterop.ShockwaveFlashObjects.dll ? What should I install to have it?

Multiple NotifyIcon images in task status area

I have a WPF application I like to keep quietly running when the user closes the main window. I do this using a NotifyIcon in the task status area, and use it as such in my App.xaml.cs:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
_notifyIcon = new NotifyIcon();
_notifyIcon.DoubleClick += (sender, args) => ShowMainWindow();
_notifyIcon.Icon = Wpf.Properties.Resources.QDrive;
_notifyIcon.Visible = true;
CreateContextMenu();
new Bootstrapper().Run();
Debug.Assert(Current.MainWindow != null, "Application.Current.MainWindow != null");
Current.MainWindow.Closing += MainWindowOnClosing;
}
private void CreateContextMenu()
{
_notifyIcon.ContextMenuStrip = new ContextMenuStrip();
_notifyIcon.ContextMenuStrip.Items.Add("Open Q-Drive...").Click += (sender, args) => ShowMainWindow();
_notifyIcon.ContextMenuStrip.Items.Add("Exit").Click += (sender, args) => ExitApplication();
}
private void ExitApplication()
{
_isExit = true;
Debug.Assert(Current.MainWindow != null, "Application.Current.MainWindow != null");
Current.MainWindow.Close();
_notifyIcon.Visible = false;
_notifyIcon.Dispose();
_notifyIcon = null;
}
Yet after closing and restarting the app a few times while debugging in VS2017, I have multiple icons visible, of which all but the active one vanish on mouse-over. I notice this is a bug with a few other applications I use that I have not developed myself.
How can I prevent this?
NotifyIcon leaves its icon behind if you exit the program without hiding the icon first.
You're hiding it in ExitApplication, of course. I suspect that while debugging, though, you're not always exiting the program by selecting the Exit item on the menu, but simply by stopping Visual Studio. That's why the orphaned icon gets left behind.
This isn't unusual in development, but it won't affect your users unless they use the Task Manager to force an immediate halt to your program.
If it bothers you, though, you could write a global exception handler (something you should probably do anyway) and in that handler you could hide the icon, taking care first to make sure it still exists.
Of course, if you break on exceptions in Visual Studio and you abruptly terminate the program, even that global exception handler won't hide the NotifyIcon.

How to move DLL dependencies to another folder

My WPF application is using MahApps.Metro, it require the MahApps.Metro.dll and the system.windows.interactivity.dll. How do I shift them to a folder, for example folder bin and still make them work? If I just shift them, the application would not start at all. Please help!
It would probably be best if you left all these DLL files alone and let the .NET Framework sort it all out. If you have to move them anyway, you may edit the properties of a reference and set the Copy Local property to false, so it won't output the DLL file when compiling.
In you App.cs file (assuming you're using C#) add the following:
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}
Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
if (args.Name.StartsWith("MahApps.Metro"))
{
System.IO.BinaryReader stream = new BinaryReader(new FileStream("bin/MahApps.Metro.dll", FileMode.Open));
byte[] assembly = stream.ReadBytes((int)stream.BaseStream.Length);
return Assembly.Load(assembly);
}
return null;
}
Edit the above code to suit your needs so that it will work exactly the way you want it to.
You will have to verify that all the DLL files you're using are the same version as the one being targeted, or .NET will throw an exception. Also, the DLL file may not exist and loading the stream might raise some problems if it is used on a network location. All in all it is likely to work, but it's probably not worth the trouble.

C++ CLR windows form RegisterHotKey to restore a minimized window

I am creating a GUI in using VC++ CLR windows form and wanted to make a hotkey to restore my windows from the system tray that I have minimized. I've found that RegisterHotKey is a way to make a global hotkey in the system but I don't understand how to make use of it inside my code.
Any thoughts??
First you need to #include the Windows headers, put it in the stdafx.h precompiled header file for example:
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
#pragma once
#include <Windows.h>
#pragma comment(lib, "user32.lib")
The #pragma ensures that the linker will link the import library for user32.dll, required to link RegisterHotKey().
Next, inside the Form class you need to override the OnHandleCreated() method. It will run whenever the native window for the Form is created, something that can happen more than once. Make that look like this:
protected:
virtual void OnHandleCreated(EventArgs^ e) override {
__super::OnHandleCreated(e);
RegisterHotKey((HWND)this->Handle.ToPointer(), 1,
MOD_ALT | MOD_CONTROL, (UINT)Keys::F1);
}
I hard-coded the hotkey to Ctrl+Alt+F1, change that to the one you want to use. You can add additional hotkeys, change the id argument (2nd argument, I used 1).
Then you need to detect the WM_HOTKEY message that windows will send to you when the user presses the key. That requires overriding the form's WndProc() method, like this:
protected:
virtual void WndProc(Message% m) override {
if (m.Msg == WM_HOTKEY && m.WParam.ToInt32() == 1) {
this->WindowState = FormWindowState::Normal;
this->BringToFront();
}
__super::WndProc(m);
}
Test this by minimizing the form, pressing Ctrl+Alt+F1 and you'll see the window getting restored and moved back into the foreground.
Thanks for your help Hans, I tried with the codes but it didn't really work like I wanted it to. My program will be minimized into the system tray with the codes below
private:
System::Void MyForm::MyForm_Resize(System::Object^ sender, System::EventArgs^ e) {
if (WindowState == FormWindowState::Minimized)
{
Hide();
}
}
If I commented out the part where it hides as it minimize, it worked out fine though.

What is the simplest way to display (and change) an image resource on a WPF dialog (using C++/CLI)?

I have a C++/CLI GUI application and I want to display an image as a visual aid for the user to see what step in a procedure they're at. This image will need to be changed each time the user selects the new step.
Currently I'm using a picture box and have an image loaded from the disk at run time. So there are a few things I need to know here:
Is a picture box the best thing to use for this purpose or is there another control that would better suit?
How do embed the images in the executable and load them from there instead of a file that exists on disk.
How do I load a new image (I'm guessing that this will be fairly obvois if I can crack point 2)?
I've seen a few answers which relate to C# but I've not seen anything which looks like it translates to doing things in a C++/CLI app. Any suggestions would be very welcome.
Well it may not be the best solution, but the following works.
Create a new Windows Forms Application
Add these libraries to your linker settings (Project Proerties -> Link -> Input -> Additional Dependencies):
User32.lib Gdi32.lib
Add these headers:
#include <windows.h>
#include "resource.h"
Add these namespaces:
using namespace System::Reflection;
using namespace System::Runtime::InteropServices;
Add a pair of bitmaps to your resources and call them IDB_BITMAP1 and IDB_BITMAP2.
Add a picture box called m_pictureBox1.
Add a button and double-click the button to add an on-click handler:
System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
// Remove any previously stored images
if(m_pictureBox1->Image != nullptr)
{
delete m_pictureBox1->Image;
}
// Pick a new bitmap
static int resource = IDB_BITMAP1;
if( resource == IDB_BITMAP2)
{
resource = IDB_BITMAP1;
}
else
{
resource = IDB_BITMAP2;
}
// Get the primary module
Module^ mod = Assembly::GetExecutingAssembly()->GetModules()[0];
// Get the instance handle
IntPtr hinst = Marshal::GetHINSTANCE(mod);
// Get the bitmap as unmanaged
HANDLE hbi = LoadImage((HINSTANCE) hinst.ToPointer(),MAKEINTRESOURCE(resource),IMAGE_BITMAP,0,0,LR_DEFAULTCOLOR);
// import the unmanaged bitmap into the managed side
Bitmap^ bi = Bitmap::FromHbitmap(IntPtr(hbi));
// insert the bitmap into the picture box
m_pictureBox1->Image = bi;
// Free up the unmanaged bitmap
DeleteObject(hbi);
// Free up the instance and module
delete hinst;
delete mod;
}
..et voila the bitmaps are stored neatly in you app and each time you click the button the images will swap.

Resources