Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


opening/executing a file in win32

This topic is 6269 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

how do i open a file in win32? i want to open a .txt file. i want notepad/wordpad to popup with the txt file loaded. how do i do this in my win32 program?

Share this post

Link to post
Share on other sites
Well, as they say, "there are many ways to skin a cat".. and programming is no exception. I''d suggest using CreateProcess().

Here''s a code sample that demonstrates what you are trying to do.... :

#include "stdafx.h"
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
//--Note that most of the params are blank...
// and are related to security, and such..
// Also note that the application parameter is blank....
// If you specify the application param, CreateProcess...
// will not use the path environment variable to locate
// the application, which means you are responsible
// for supplying the path to the program...
// however, when passing the application name in the cmd param..
// CreateProcess WILL use the path environment variable to..
// locate the file..
// This means any command line that will execute from..
// the "Run" command in the start menu will work with..
// Create Process...

ZeroMemory( &stStart, sizeof( STARTUPINFO ) );
ZeroMemory( &stProc, sizeof( PROCESS_INFORMATION ) );
CreateProcess( NULL, "notepad c:\\bug.txt", NULL, NULL, FALSE,
0, NULL, NULL, &stStart, &stProc );
return 0;


This is just a bare bones win32 app, which will spawn notepad and open "c:\bug.txt", and then die( notepad will continue to run ).

Hope that helps...


"It''s only after you''ve lost everything that you''re free to do anything." Tyler Durden

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
thanks man, exactly what i needed.

Share this post

Link to post
Share on other sites
I guess you can also use WinExec() function:

UINT WinExec(
LPCSTR lpCmdLine, // address of command line
UINT uCmdShow // window style for new application

That is well-described in the MSDN.

Share this post

Link to post
Share on other sites
You can also use ShellExecute(). Funny how many ways there are to do the same thing.

ShellExecute has the advantage of being able to do much more than just open a file; it can be used to print, mail, edit or any other action for which a default application-file association is defined.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!