Cannot get ShellExecute to work

Hi guys,

I've been trying to open a file from a windows form and I cannot get the ShellExecute to work for me.
The code that i am using is...

1
2
3
4
5
6
 private: System::Void openToolStripMenuItem_Click_1(System::Object^  sender, System::EventArgs^  e) {
	
		  ShellExecute(this->m_hWnd,"open","notepad.exe",L"c:\\Users/introc/Desktop/Photofile.txt","",SW_SHOW );
		  	
}


The "includes" looks like this...

1
2
3
4
5
6
7
8
#pragma once


#include <string>
#include <iostream>
#include <fstream>
#include <Windows.h>



The errors that I'm getting are...

1
2
3
4
5
6
7
8
9

1
1>  photo111.cpp
1>c:\users\introc\documents\visual studio 2010\projects\photo111\photo111\Form1.h(403): warning C4244: 'argument' : conversion from 'wchar_t' to 'char', possible loss of data
1>c:\users\introc\documents\visual studio 2010\projects\photo111\photo111\Form1.h(450): warning C4244: 'argument' : conversion from 'wchar_t' to 'char', possible loss of data
1>c:\users\introc\documents\visual studio 2010\projects\photo111\photo111\Form1.h(458): warning C4244: 'argument' : conversion from 'wchar_t' to 'char', possible loss of data
1>c:\users\introc\documents\visual studio 2010\projects\photo111\photo111\Form1.h(466): warning C4244: 'argument' : conversion from 'wchar_t' to 'char', possible loss of data
1>c:\users\introc\documents\visual studio 2010\projects\photo111\photo111\Form1.h(379): warning C4101: 'transit' : unreferenced local variable
1>c:\users\introc\documents\visual studio 2010\projects\photo111\photo111\Form1.h(562): error C2039: 'm_hWnd' : is not a member of 'photo111::Form1'
1>          c:\users\introc\documents\visual studio 2010\projects\photo111\photo111\Form1.h(30) : see declaration of 'photo111::Form1'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

just leave the first parameter NULL and remove L from the fourth

read this:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153%28v=vs.85%29.aspx
Hi coder777.

Thanks for the reply. I tried what you suggested and I got a different set of errors...

1
2
3
4
5
6
7
8
9
10
1
1>  photo111.cpp
1>c:\users\introc\documents\visual studio 2010\projects\photo111\photo111\Form1.h(405): warning C4244: 'argument' : conversion from 'wchar_t' to 'char', possible loss of data
1>c:\users\introc\documents\visual studio 2010\projects\photo111\photo111\Form1.h(452): warning C4244: 'argument' : conversion from 'wchar_t' to 'char', possible loss of data
1>c:\users\introc\documents\visual studio 2010\projects\photo111\photo111\Form1.h(460): warning C4244: 'argument' : conversion from 'wchar_t' to 'char', possible loss of data
1>c:\users\introc\documents\visual studio 2010\projects\photo111\photo111\Form1.h(468): warning C4244: 'argument' : conversion from 'wchar_t' to 'char', possible loss of data
1>c:\users\introc\documents\visual studio 2010\projects\photo111\photo111\Form1.h(381): warning C4101: 'transit' : unreferenced local variable
1>c:\users\introc\documents\visual studio 2010\projects\photo111\photo111\Form1.h(576): error C2664: 'ShellExecuteW' : cannot convert parameter 2 from 'const char [5]' to 'LPCWSTR'
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Sorry for a dumb question, but how do I convert parameter 2 from 'const char [5]' to 'LPCWSTR'...

Thanks in advance,
O.C.
ok, sorry you compiled it as UNICODE. So place L in front of all the strings.

I just don't know where those warnings come from (you didn't show that code)
Thanks for the info. That helped with the errors I was getting. However it still will not run.

Should I make changes to reference the shell32.lib ? I've already included "#include <ShellAPI.h> ".


Thanks again.
Last edited on
So place L in front of all the strings.

And maybe use ShellExecuteW, to make things nice and clear?

However it still will not run.

So it build cleanly, and then does not do anything when you run it?

But I can see you're using C++/CLI, so you should prob. use the .NET way of launching a file rather than ShellExecute.

Process::Start Method
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.start.aspx

1
2
    Process^ myProcess = gcnew Process();
    myProcess->Start("notepad.exe", "c:/Users/introc/Desktop/Photofile.txt");


or

1
2
3
4
    Process^ myProcess = gcnew Process();
    myProcess->StartInfo->FileName  = "notepad.exe";
    myProcess->StartInfo->Arguments = "c:/Users/introc/Desktop/Photofile.txt";
    myProcess->Start();


ProcessStartInfo Class
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx

Andy

PS When opening something like a text file, the executable name is usually omitted so that the user's preferred (text editor) is used (via the file association mechanism.) But you prob knew this already??

To use Process::Start to open a text file with the associated editor, it's

1
2
    Process^ myProcess = gcnew Process();
    myProcess->Start("c:/Users/introc/Desktop/Photofile.txt");


or

1
2
3
    Process^ myProcess = gcnew Process();
    myProcess->StartInfo->FileName  = "c:/test/dictionary.txt";
    myProcess->Start();


Last edited on
HI andywestken,

Thanks for the advice. I have tried that and it works when i only use

1
2

		Process::Start("c:\\Users/introc/Desktop/Photofile.txt");


as well as

1
2
3
Process^ myProcess = gcnew Process();
		myProcess->StartInfo->FileName  = "c:\\Users/introc/Desktop/Photofile.txt";
		myProcess->Start();



Is it better to use the "myProcess..." choice as this gives me more options for further options (eg; KILL or EXIT) ?
Last edited on
Is it better to use the "myProcess..." choice ...

If the shorter form does what you require you should use it. You can switch to the longer form later, if the need arises.

Andy
Topic archived. No new replies allowed.