Creating .lnk-file using only C++ and cmd - c++

I have an .exe file and I want to add it to Autorun without using explorer. I know way to my .exe file. For adding something to Autorun I should create shortcut.lnk and put it to C:\Users\User\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup. I tried to use cmd command mklink, but this command creates .symlink, not .lnk.
How can I add my file to Autorun, apart from creating .lnk shortcut?
How create .lnk, using only C++ and cmd (not js, Powershell, BAT, etc.)
I know, that .lnk is just a txt file in HEX code? so I can put text with address in it and change add-in. It is not good way, in my opinion.

.lnk is a binary file with specific format. The proper way to enable autorun is to create a registry entry in Run or RunOnce sections.

Related

Not reading file from environment path

I have create a project in which I am trying to generate a .txt file and then calling OS specific commands (I'm using unix so using system() function to trigger application) to open it in default application.
My code works perfect.
But the problem starts when I try to run my application after setting it in environment path. I am doing that for global visibility. File gets generated correctly by while opening the .txt using system() seems like it is trying to look at it at different directory.
I want to some how convey to the system() function to look at the specific location.
How can I solve this problem?
It sounds like you are actually just in a different working directory. Try showing the result of getcwd.

Open file to display content in C++

I have 2 questions to ask regarding opening files (any kind of files) using C++. I am currently working on a GUI program and I want to add a changelog in txt form. Also I want a menu in my program to open that changelog.txt with the default text editor every user has installed or to simply put it to open that text file. Please keep in mind that I want to open the file for display NOT in the program for input/output.I know I can do that using
system("notepad.exe filepath.txt");
or to open them with the preset program:
system("filepath.txt");
The problem is that both of those open a command line behind the notepad. I know there is another command to open files using Win32 API called CreateProccess() but my compiler doesn't recognise that command (OpenWatcom W32).
So here are my questions:
1) Is there any other command to open files or is there a way to stop the command line from opening when using system command?
2) How do you define in Windows that the text file is in the current program folder? I mean instead of giving the entire filepath which will change from user to user is there any way to "tell" the program that the file is always on the current folder that the program is in?
I am sorry for any errors, if you want any clarification please let me know.
CreateProcess would be the wrong function to use here. That would require you to decide which process to run. The user may prefer to use a text editor other than Notepad, I know I do! The right way to do this on Windows is to ask the shell to open the file with whatever program the user has associated with the file. The ShellExecute function does this.
Call it like this:
ShellExecute(
MainWindowHandle,
"open",
FullyQualifiedTextFileName,
NULL,
NULL,
SW_SHOWNORMAL
);
You will need to include the Shellapi.h header file and link to the Shell32.lib library. If your compiler does not include these files, and I would be surprised if that was the case, then you can get them from the Platform SDK. That said, if you are serious about programming on Windows you should get hold of a tool that gives you access to the Windows API.
I do recommend that you use a fully qualified path for a task like this. Since your text file is located in the same directory as the executable you should simply join that directory to your text file's name. Get hold of the full path to the executable by calling GetModuleFileName passing NULL for the hModule parameter.

Relative path problem for a deployed win32 application

I have written a c++ program and deployed it in say c:\my_app, and my executable's path is c:\my_app\my_app.exe. Say, my_app needs many files such as the_file.txt, which is located in c:\my_app\the_file.txt.
In my executable, I open the txt file as, xx.open("the_file.txt");
Moreover, I have associated my program with let's say .myp extension.
When I'm on Desktop, and want to open a file named example.myp, my program can not see the_file.txt. Because, it (somehow) assumes that it's currently working on Desktop.
Is there any easy way to handle this problem by changing shell command for open in HKEY_CLASSES_ROOT? The naive solution would be to change all file open operations with something like %my_app_location/the_file.txt". I don't want to do that.
Always use a full path name to open a file. In other words, don't open "foo.txt", open "c:\bar\foo.txt". To find the install directory of your EXE use GetModuleFileName(), passing NULL for the module handle.
These days you shouldn't add files to c:\my_app....
Instead use the ProgramData Folder and full paths.
Use SHGetSpecialFolderPathA with CSIDL_COMMON_APPDATA to get the ProgramData folder and the create your program directory and add your files.
You should set current directory for your app's folder with SetCurrentDirectory function. After that you can open file by name without full path

Why can't I launch notepad++ from the command line anymore?

I'm teaching myself some Django tonight using the local dev package at instantdjango.com
I made it through half the tutorial, building a basic map with points on it, then closed out of the console for a bit. I just fired it back up and now when I try to use notepad++ to edit files, console tells me it doesn't recognize the command. What happened?
When I as working through the first half, I was able to type: "notepad++ filename.ext" and I'd get a text editor that would pop up.
Now when I type that, it doesn't recognize the command.
How do I get back the ability to use the text editor and how did I lose it?
In a windows terminal, you can launch notepad++ with the following:
start notepad++ <filename>
Note that the filename is optional.
Most likely the directory in which the Notepad++ executable resides is not in your system's PATH. For information about fixing this please see How to set the path in Windows 2000 / Windows XP.
If you are using gitbash or cygwin, you can create an alias
alias np='start notepad++'
And use
np myfile.txt
This is what I have done, in this way you dont have to type notepad++
Create np.bat file with this set of commands
#echo off
start "" "C:\Program Files (x86)\Notepad++\notepad++.exe" %*
place np.bat file in c:\Windows
open the command prompt and type np or np myfile.txt and enter.
One way is to make a change to this registry key:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\Notepad++.exe]
You can download a zipped .reg file from Donn Felker that lets you open a file with just an n shortcut.
I edited the .reg before running it and to make sure the path to Notepad++ is correct (e.g. C:\Program Files (x86)) and I also changed the shortcut to n instead of n.
Then double click to add to your registry.

Open a Specified File in Excel from a GUI - Borland C++

I am using Borland Builder C++ 2009. I want to add a button to a form that allows the user to open a file in Excel that I specify. I can't think of how to do this. I know how to link with other code and executables -- is there a Microsoft Excel executable that I could use? How could I specify the file then? Any hints on this, or at least a place to look online, would be greatly appreciated.
Assuming that the file type is registered with Excel, you could call ShellExecute() on the file, using the "open" verb. This will cause the file to be opened as if double clicked by the user in Explorer and will invoke Excel.
If that isn't the case, and you can assume that Excel is installed, you could instead pass "excel" to ShellExecute() as the application, and the path of the file as the parameter. (Note that I didn't test this, but it worked from the Run dialog, so I think that it should work from ShellExecute() as well).
Thanks, Andy. I am using ShellExecute() as you suggested, giving Excel as the application and the path of the file as the parameter. It works to open Excel, however, it cannot seem to find the file. I have tried moving the file around, typing in the entire path, part of the path with no change. Here is the code I use:
ShellExecute(NULL, "open" ,"Excel.exe", "C:\\Documents and Settings\\Lab1\\My Documents\\Waypoint Tool.xls", NULL, SW_SHOWNORMAL);
So, I need to figure out why it isn't able to find this file.
Thank you for the suggestion to use ShellExecute though. I think I am on the right track!
Try:
print("ShellExecute(NULL, "open" ,"Waypoint Tool.xls", "C:\\Documents and Settings\\Lab1\\My Documents\\", NULL, SW_SHOWNORMAL);");
Looking at this page: http://msdn.microsoft.com/en-us/library/bb762153(VS.85).aspx
It seems like it wants the file you are wanted to execute the open on as the third parameter and the directory for the fourth.
System() command should be enough i think.
For any Windows application, we can open any file using the Location of .exe file and the File Path.
Eg.,
system("PATH C:\\Program\ Files\\Microsoft\ Office\\OFFICE11;%PATH% & excel \"C:\\Documents and Settings\\User\\Desktop\\ExcelFile.xls\"");