• Launching a program
    30 replies, posted
What is the best way to navigate to a certain folder, then launch a program? Other than using System()? in C++.
Did you try google searching this? [editline]11:45PM[/editline] There are lots of answers on google. Lots of them. Facepunch won't google for you.
[QUOTE=c-unit;23196877]What is the best way to navigate to a certain folder, then launch a program? Other than using System()? in C++.[/QUOTE] Let me guess, people said system() is bad, so you want to use something else which does the exact same thing.
[url]http://msdn.microsoft.com/en-us/library/ms682425%28v=VS.85%29.aspx[/url] There
The only cross-platform solution is system, and even that isn't really cross-platform. :P
[URL=http://msdn.microsoft.com/en-us/library/bb762153%28VS.85%29.aspx]ShellExecute[/URL] if you want to for example open a file in the program it's associated with or open web URLs in the default browser.
Pretty sure boost has a portable interface for creating and managing child processes. (And portable folder navigation)
[QUOTE=esalaka;23198683]The only cross-platform solution is system, and even that isn't really cross-platform. :P[/QUOTE] shit I wish my program that launched another program was cross platform! Better not use system() !
[QUOTE=efeX;23199238]shit I wish my program that launched another program was cross platform! Better not use system() ![/QUOTE] Portable libraries are often much easier (and nicer) to use than native system interfaces anyway. Working with the WinAPI in C++ isn't exactly the most pleasant experience, even if you're very familiar with it. Same can be said for POSIX, if he's on Linux, Unix or Mac etc. edit: And the system function might not do what he wants, as it blocks.
I'm sure using the WinAPI for a newbie is a much more pleasant experience than trying to figure out how to install/use third party libraries, and he's probably launching a Windows specific program anyway. But using system() for this is just bad practice.
[QUOTE=Spoco;23199430]I'm sure using the WinAPI for a newbie is a much more pleasant experience than trying to figure out how to install/use third party libraries, and he's probably launching a Windows specific program anyway. But using system() for this is just bad practice.[/QUOTE] I don't see how configuring a third-party library is any difficult at all. Boost even comes with an installer, and most boost modules are header-only (although the file-system stuff requires you to link against one library). And you still have to figure out how to use the WinAPI, which isn't any easier than learning boost-filesystem. If he just wants working code, there's lots of examples out there for either, but the WinAPI examples won't make any sense to a beginner, compared to the nice object-oriented interfaces boost provides.
So for ShellExecute [code]ShellExecute(handle, "open", "C:\\Program Files\\Steam\\steamapps\\vypr11\\garrysmod\\hl2.exe, NULL, NULL, SW_SHOWNORMAL);[/code] I just want my program to launch gmod, will the code above work?
[QUOTE=c-unit;23208869]So for ShellExecute will the code above work?[/QUOTE] Test it and see
Didn't work..it said handle is an unknown identifier
[QUOTE]ShellExecute(handle, "open", "steam://rungameid/4000", NULL, NULL, SW_SHOWNORMAL);[/QUOTE] Should work... [editline]08:57PM[/editline] [code]ShellExecute(handle, "open", "C:\\Program Files\\Steam\\steamapps\\vypr11\\garrysmod\\hl2.exe, NULL, NULL, SW_SHOWNORMAL);[/code] The closing quote is missing on the filepath. it should be: [code]ShellExecute(handle, "open", "C:\\Program Files\\Steam\\steamapps\\vypr11\\garrysmod\\hl2.exe[B]"[/B], NULL, NULL, SW_SHOWNORMAL);[/code]
How can I find out what the game Id's are?
GMOD's ID is "4000" try this: [QUOTE]ShellExecute(handle, "open", "steam://rungameid/4000", NULL, NULL, SW_SHOWNORMAL);[/QUOTE]
Also when using the fixed ShellExecute you gave me, I get this build error [code]steam game launcher.cpp(22): error C2065: 'handle' : undeclared identifier[/code] [editline]02:00PM[/editline] Ok I will try it thanks [editline]02:01PM[/editline] I still get that same build error using [code]ShellExecute(handle, "open", "steam://rungameid/4000", NULL, NULL, SW_SHOWNORMAL);[/code]
What does the error tell you >_>
That I am forgetting to #include something, right?
No. It's telling you that on line 22 you're using something called 'handle', which it doesn't know about. handle could in theory be defined in some header you can #include, but the msdn-page more or less can tell you that this is certainly not what's missing.
Ah I was missing the Shellapi header
[quote=MSDN]hwnd [in, optional] HWND A handle to the owner window used for displaying a UI or error messages. This value can be NULL if the operation is not associated with a window.[/quote] Since you don't have a window, you use NULL.
Still not working D: [editline]02:21PM[/editline] Oh I Will try that thanks [editline]02:21PM[/editline] Build Error: steam game launcher.cpp(24): 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
[URL]http://social.msdn.microsoft.com/forums/en-US/vclanguage/thread/c1b08c0a-a803-41c3-ac8c-84eba3be1ddb/[/URL] Basically there are two versions of every WinAPI function that takes or returns strings. Wide/Unicode version (ShellExecuteW) and an ANSI version (ShellExecuteA), which one is used when you call ShellExecute depends on your IDE/compiler settings. Nowadays in Visual Studio, the wide versions are used by default. You can either explicitly call ShellExecuteA, or prefix your literals with L ("open" becomes L"open").
[code]cannot convert parameter 2 from 'const char [5]' to 'LPCWSTR'[/code] should be const char[8] no ? [editline]10:30PM[/editline] const char yourChar[8] { 'L', 'P', 'C', 'W', 'S', 'T', 'R' }; If not, excuse me, I'm not really fluent with C++.
[QUOTE=pikzen;23214224][code]cannot convert parameter 2 from 'const char [5]' to 'LPCWSTR'[/code] should be const char[8] no ? [editline]10:30PM[/editline] const char yourChar[8] { 'L', 'P', 'C', 'W', 'S', 'T', 'R' }; If not, excuse me, I'm not really fluent with C++.[/QUOTE] No. LPCWSTR is some weird Microsoft datatype. I think it means Long Pointer to C Wide STRing. Which means that you have enabled Unicode support and should convert your strings into wide characters first.
Oh, thanks. I guess I'll go to sleep smarter tonight :eng101: [editline]11:11PM[/editline] [url]http://www.cplusplus.com/forum/windows/2359/[/url] ?
[QUOTE=esalaka;23215285]No. LPCWSTR is some weird Microsoft datatype. I think it means Long Pointer to C Wide STRing. Which means that you have enabled Unicode support and should convert your strings into wide characters first.[/QUOTE] How do I go about doing so
[QUOTE=c-unit;23215957]How do I go about doing so[/QUOTE] Do what Spoco said, and prefix the string literal with L. [cpp] // ShellExecute defaults to ShellExecuteW on your system, so we need 16-bit character (wide) strings: ShellExecute(NULL, L"open", L"steam://rungameid/4000", NULL, NULL, SW_SHOWNORMAL); // or explicitly use ANSI ASCII version: ShellExecuteA(NULL, "open", "steam://rungameid/4000", NULL, NULL, SW_SHOWNORMAL); [/cpp] I thought you wanted to actually search for a file in a directory structure, then execute it. For just launching a game, the WinAPI is fine.
Sorry, you need to Log In to post a reply to this thread.