[Solved]Loading SWF and EXE's Files

Just wondering if its possible to make a program in C++ that would be able to open EXE's made in MultiMedia Fusion 2 and SWF's for Flash. Reason being is me and a few mates have been working on a project lately and I'm wondering if c++ would be possible to make a Menu that could open other .exe and .swf files inside it?

Thanks,
Myth.
Last edited on
That's what the system() command is for. However, as you are on Windows, you would be better off using ShellExecute()
1
2
3
4
bool LaunchFile( const string& filename )
  {
  return 32 < ShellExecute( NULL, "open", filename.c_str(), NULL, NULL, SW_SHOWNORMAL );
  }

If you want more information than whether or not the process launched properly, check out CreateProcess() on MSDN.

Hope this helps.
I've used system to open files before but it normally opens them in another window from memory(i forget). Either way I've tried using -
1
2
3
4
bool LaunchFile(const std::string& filename)
{
	return 32 < ShellExecuteW( NULL, "open", filename.c_str(), NULL, NULL, SW_SHOWNORMAL );
}


But it's giving me one of those LPCWSTR errors
error C2664: 'ShellExecuteW' : cannot convert parameter 2 from 'const char [5]' to 'LPCWSTR'
I've dealt with these errors before and for the life of me can't remember what to do it get rid of them. I'm still searching now but if you beat me to it would be a help lol.

Thanks for your help Duoas
Haha, i feel like an idiot - just rememberd I had unicode turn on lol. Well that solves that problem but now I'm facing one I haven't seen.
1
2
3
4
bool LaunchFile(const std::string& filename)
{
	return 32 < ShellExecute( NULL, "open", filename.c_str(), NULL, NULL, SW_SHOWNORMAL );
}


The code is changed back to originally how you had it. But now it's giving me this:
1>.\LoadFile.cpp(5) : error C2446: '<' : no conversion from 'HINSTANCE' to 'int'
1> There is no context in which this conversion is possible
1>.\LoadFile.cpp(5) : error C2040: '<' : 'int' differs in levels of indirection from 'HINSTANCE'
Now this I've never seen before.

I can get something really basic like
1
2
3
4
5
if(0 != system("\"C:\\Windows\\System32\\" "notepad.exe\" >nul 2>&1"))
{
	fprintf(stderr, "Failure to execute NotePad.\n");
	return 1;
}
to work fine but I was wondering if the above can be fixed - What advantages it'll give me over using system (as I don't like using system) anyway. And just wondering if I can make the files open like they are in a sandbox etc. Opening inside one another.
Last edited on
According to the MSDN -- which I suggest you check whenever you get this type of errors before asking -- the HINSTANCE thing is there just for backwards compatibility with 16-bit Windows. All you need to do is cast the returned value to an int, and then make the comparison.
Well for your information I did check MSDN for error 2446 and 2040 but considering I'm not a great programmer I'm unsure so I prefer to come here for answers as I do normally get someone pointing me in the right direction with good information.

I do understand casting but In a situation like this I'm not to sure how to do the right cast? So if someone wouldn't mind showing me which cast to use - it would be a great help.

Thanks,
Myth.
(int)ShellExecute(... will do just fine.
Thanks for that helios it helped out but now I got my most loved error
1>LoadFile.obj : error LNK2028: unresolved token (0A0002EC) "extern "C" struct HINSTANCE__ * __stdcall ShellExecuteA(struct HWND__ *,char const *,char const *,char const *,char const *,int)" (?ShellExecuteA@@$$J224YGPAUHINSTANCE__@@PAUHWND__@@PBD111H@Z) referenced in function "bool __cdecl LaunchFile(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?LaunchFile@@$$FYA_NAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
1>LoadFile.obj : error LNK2019: unresolved external symbol "extern "C" struct HINSTANCE__ * __stdcall ShellExecuteA(struct HWND__ *,char const *,char const *,char const *,char const *,int)" (?ShellExecuteA@@$$J224YGPAUHINSTANCE__@@PAUHWND__@@PBD111H@Z) referenced in function "bool __cdecl LaunchFile(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?LaunchFile@@$$FYA_NAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
I'm looking over it now but not sure if I'll find it. (Not the best debugger) I look forward to the next couple of years when I'm a lot better at this :)
closed account (z05DSL3A)
Can I just ask what your project type is?
As in I'm just making a basic console application at the moment trying to just open up notepad etc?

EDIT:

Not sure if this is any help but I turned UNICODE off.
With uni code off i get this linker error:
1>main.obj : error LNK2028: unresolved token (0A0002C3) "extern "C" struct HINSTANCE__ * __stdcall ShellExecuteA(struct HWND__ *,char const *,char const *,char const *,char const *,int)" (?ShellExecuteA@@$$J224YGPAUHINSTANCE__@@PAUHWND__@@PBD111H@Z) referenced in function "bool __cdecl LaunchFile(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?LaunchFile@@$$FYA_NAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
1>main.obj : error LNK2019: unresolved external symbol "extern "C" struct HINSTANCE__ * __stdcall ShellExecuteA(struct HWND__ *,char const *,char const *,char const *,char const *,int)" (?ShellExecuteA@@$$J224YGPAUHINSTANCE__@@PAUHWND__@@PBD111H@Z) referenced in function "bool __cdecl LaunchFile(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?LaunchFile@@$$FYA_NAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)


With uni code set to [Project -> Project Properties ->Configuration Properties -> Character Set -> Use Unicode Character Set] I get this error:
1>.\main.cpp(7) : 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


The current code for this project is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <windows.h>

bool LaunchFile(std::string& filename)
{
	return 32 < (int)ShellExecute( NULL, "open", filename.c_str(), NULL, NULL, SW_SHOWNORMAL );
}

int main()
{
	std::string myFile;
	std::cout << "Enter a program to open: ";
	std::cin >> myFile;

	LaunchFile(myFile);

	return 0;
}


Hope this helps :)
Last edited on
closed account (z05DSL3A)
Is that a standard Win32 console?

I only ask because the Microsoft docs, say that you get a LNK2028
When attempting to import a native function into a pure image, remember that the implicit calling conventions differ between native and pure compilations.

This is the second time recently[1] that this has cropped up.


[1] http://www.cplusplus.com/forum/windows/7211/
Don't forget to link with -lshell32 (that's shell32.lib or libshell32.a, depending on your compiler).
I edited my above post check that out :)
I think my problem must be I'm not linked with the shell32.lib

EDIT:

Alright just included shell32.lib and it worked a charm - man didn't think I'd need to include anything like that. Thanks a heap for all the help on this guys!
Last edited on
Topic archived. No new replies allowed.