Acmlm's Board - I2 Archive - Programming - Opening files in VB (NOT with "Open FileName for Input As #number")
User | Post |
Yarx
Posts: 64/133 |
you can call programs ftom witnin C++ pretty easily.
This uses ShellExecute(), and it will open a file that has an association with it, like a TXT file or a MP3 maybe. HEre I'm using Winver.exe cause I know you have that
#include <windows.h>
void main() { ShellExecute(NULL, "Open", "C:\\Winnt\\System32\\Winver.exe", NULL, NULL, SW_SHOWNORMAL); } Using ShellExecute() in VB
But it's recommended you use something that has a little more control over it such as CreateProcess(), however create process will not execute a TXT file since it is not an executable, and it will not check for associations.
#include <windows.h> #include <stdio.h> #include <tchar.h>
void main() { STARTUPINFO StartUp; PROCESS_INFORMATION ProcessInfo; TCHAR CommandLine[] = _T("C:\\winnt\\system32\\winver.exe"); ZeroMemory( &StartUp, sizeof(StartUp)); StartUp.cb = sizeof(StartUp); if(CreateProcess(NULL, CommandLine, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &StartUp, &ProcessInfo)) { _tprintf(_T("Error creating process. Error: %x\n"), GetLastError()); } else { WaitForSingleObject(ProcessInfo.hProcess, INFINITE); CloseHandle(ProcessInfo.hThread); CloseHandle(ProcessInfo.hProcess); } }
Using these in VB is just a matter of importing the API for them. Here's a good example project that uses both methos of starting a starting a process with VB, using the two methods I've mentioned. They are much better than the VB Shell() command. Check it out.
Hope this helps |
Parasyte
Posts: 29/514 |
Calling programs from within C\C++: http://www.cplusplus.com/ref/cstdlib/system.html
Short example:
#include <stdio.h> #include <stdlib.h>
int main(int argc, char **argv) { char str[0x100];
if (argc != 2) return 1;
sprintf(str,"notepad.exe %s",argv[1]); system(str); printf("Done!\n"); }
Tested with: C:\projects\system>systest.exe testfile.txt
Note that the system function does not create a new thread, so the program will hang until the called program ends. But of course, under Win32, it's not so difficult to create your own thread to place the system call in. |
HyperLamer
Posts: 853/8210 |
Originally posted by Parasyte What are YOOOOUUU doing?
*GASP* Parasyte making a spammy post?
In C++ I think you can use system(). FYI, shell() works nice but what if the default text editor isn't Notepad? Look into the ShellExecute() API function. |
Emptyeye
Posts: 86/2273 |
Here's a delightfully random question....is there a C++ equivilent to that command? Because that would be pretty neat if so. |
Parasyte
Posts: 28/514 |
What are YOOOOUUU doing? |
Icy Guy
Posts: 18/36 |
What the shell? (Ok, bad pun.) That wasn't working for me before, but looking at it now, I realize that it was because I didn't know to include the name of the app I wanted to use to open the document.
Anyway, thanks for your help. |
Chickenlump
Posts: 240/722 |
I have a menu item that when clicked on, opens the readme text in notepad.
Private Sub Readme_Click() shellResult = Shell("notepad.exe readme.txt") End Sub
And if you need other types of files to be opened, just specify the path to the program opening that file. For example : WordPad
Private Sub Readme_Click() shellResult = Shell("C:\Program Files\Accessories\wordpad.exe readme.doc") End Sub
And if you want the opened file to be maximized and brought to the front, do this.
Private Sub Readme_Click() shellResult = Shell("notepad.exe readme.txt", vbMaximizedFocus) End Sub
The readme.text or readme.doc must be in the same folder as your program for that to work (unless you specify a path to your readme, like I did with the program, works just the same. )
|
Icy Guy
Posts: 17/36 |
A while back, I recall reading somewhere that there was a way to open a file without using the method described in the thread title. Basically, what I want it to do is open a text file (let's just say it's "readme.txt") in the default text editor.
To clarify...
1) User clicks "View Readme" on a menu in my program.
2) Readme file opens, as if the user had double-clicked the file in the Windows Explorer.
Can someone help me out? (This time, I'm going to write this down, so I don't forget how to do this. Again. ) |
|