My question is a bit related to this one but it's not what I was aiming for:
programmatically merge .reg file into win32 registry
What I want to do is to create a program that can import a .reg file using win32 or some other library. I tried looking around but failed at that part. Something like a regedit.exe /s function. Is it possible to do this without using regedit.exe or reg.exe in anyway?
So.. no system("myfile.reg") or like ShellExecute() using reg.exe or regedit.exe
Thanks!
Since regedit.exe created the reg file in the first place, I'm not really sure why you wouldn't also want it to do the import.
One other option would be to parse the reg file and then convert the parsed information into direct calls to the RegCreateKey, RegSetValue Win32 API functions, but I suspect that would be a fair amount of coding effort.
Related
I know how to create and code my own setup program but i need to be redirected at some point. This point i'm sure inlight other people too.
I created a setup project. All is done. Except, installation files inside of EXE.
I know 2 different ways of doing this:
Create resource in EXE and embed RAR/ZIP file.
Put compressed archive with files along with EXE. EXE will read contents and data from this protected and compressed ZIP.
But what i want is number 1. I want to embed it. But;
What is the proper way of embedding this? Are other setup creators do the same thing? Embed resource as compressed single zip in EXE? Or do they another trick?
How do you extract files? On the fly by memory? Like read each file one-by-one. Synced. Or first, copy ZIP to temp and extract from it.
Or even embed all files separately to the resources.
I, even think that if i should create simple MSI without dialogs and embed it and run from background but i want to take all control. I want everything belongs to the original setup that i created.
Note:
I want to make my own dialogs, effects, procedures, functions and
steps. Yes, MSI is acceptable but i will stick with its features. Oh,
If im able to extend it, why should i spent more time doing this
instead of making my own? I am so confused... I am talking about very big setup project here. Not just a standard ugly UI with less features. At least, im gonna try :)
Do not give me any sample/code just show me a correct path, please.
Best options here:
Create ZIP compatible EXE that reads itself as ZIP and read the file list and extract.
Create non-zip compatible EXE that has a hidden body somewhere and read that area (seek) and get the list & extract.
The proper way to do it is to use the Windows installer technology, aka MSI. There is a nice, Microsoft blessed toolset called WiX that you can use to greatly simplify the process.
http://wix.codeplex.com/
If you are truly intent on reinventing the wheel, you can look through the source code to WiX on how things are done.
Best options here:
Create ZIP compatible EXE that reads itself as ZIP and read the file list and extract.
Create non-zip compatible EXE that has a hidden body somewhere and read that area (seek) and get the list & extract.
Why write your own? Much easier to use WiX (http://wixtoolset.org/) with optionally a graphical interface like WiXEdit (http://wixedit.sourceforge.net/). Have you thought about additional requirements like uninstall etc...
Good luck!
Would InnoSetup help with your problem? You can personalise the dialogs and extend its functionality quite a lot.
This seemed like a common question but after doing some searching, I wasn't really able to find my answers. There is an article on this here:
http://www.codeproject.com/KB/shell/shellextguide1.aspx
But it's for a very old version of Visual Studio. I'm using VS 2008, so the instructions and interfaces don't seem to match what I'm seeing.
I want to create a simple shell extension using C++ that creates a context menu for files with extension .GZ. When right clicking on these files, I should be able to click my context menu item and have a callback in code to do some sort of operation on that file.
Other context menu items would do things like spawn modless dialogs to accept user input before executing some action.
From what I've seen, ATL is used for this but I have never used ATL, so all of the object types and interfaces are very confusing to me. It wouldn't be so bad if I had a proper tutorial or documentation to read.
Can anyone help me out? Isn't there some sort of tutorial out there that isn't 10 years old?
I can't tell you exactly how to write a shell extension, but I will provide a number of tips. Writing a Shell Extension offers some significant advantages over the much simpler “registry-only” method:
With a Shell Extension, you can dynamically create a context menu item (or submenu) that is more relevant to the selected file(s). For example, if you are writing a Shell Extension for zip files, it is possible to create a submenu within the context menu that shows the entire contents of the zip.
You can handle multiple files simultaneously, which may be more beneficial not just for performance purposes but also so that you can work out what to do based on the selection as a whole rather than just for each file.
Some of the downfalls to Shell Extensions are:
Substantially increased complexity. Be prepared to spend a lot of effort on this to get it working. Have a home-espresso machine installed next to your computer and/or hire someone to make you coffee.
Substantially increased difficulty in debugging. Ditto about coffee.
It's difficult to write a Shell Extension because they can be very hard to debug.
Shell Extensions are loaded by the explorer.exe process, and without specific configuration of Explorer, you need to force-quit the explorer.exe process so that you can install a newer version of your Shell Extension. There is a way to get Explorer to unload DLLs that it is no longer using, but you should only do this on a development machine and not on a deployment target:
In RegEdit, browse to the following key:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer
Add a new DWORD key called “AlwaysUnloadDLL” and set its value to 1.
Restart explorer.
This works most of the time, but there may still be times where you need to close Explorer because the Shell Extension was not unloaded.
Keep in mind that your Shell Extension may be loaded by other applications, for example, if you right-click on a file with an applications “open file” dialog, then your Shell Extension will be loaded into that application, and not Explorer.
If your Shell Extension causes a runtime error, quite often the result will simply be that your context menu item does not show, very rarely will you be told that your Shell Extension failed to load or that it caused a runtime error.
Configuration can be hard, even with an installation, registry data needs to be created in several places, and depending where you want your context menu to show, the places in the registry may differ between different versions of Windows.
What you'll need to do:
Visual Studio offers some shortcuts to creating Shell Extensions, but basically you'll need to create a COM DLL. A Shell Extension for context menu items must implement both the IContextMenu interface and the IShellExtInit interface.
In the IShellExtInit::Initialize() method, you can obtain the selected files from the IDataObject parameter. From memory, the data is in “Drag-n-Drop” format, so you need to get an HDROP handle from the IDataObject and query the files from there (this is from memory, it may actually be different than as I described here, so proceed with caution).
Once your DLL is ready to be “installed”, you must copy it somewhere, and then run regsvr32 to make sure it is registered.
Follow this guide to know where to put registry keys.
There may be issues with 64-bit Windows, if you build a 32-bit DLL it may not load in 64-bit Explorer… so keep this in mind if you are having trouble with 64-bit Windows.
Your DLL will actually have two GUIDs associated with it. I can't remember exactly how it works, but one GUID refers to the DLL itself and the other refers to the actual Shell Extension. Make sure you use the GUID of the actual Shell Extension when creating keys in the registry where a GUID is required.
All things considered… (tl;dr)
Weigh up the costs of whether a Shell Extension is worth it. If you want to create menu items dynamically based on the selected files, then a Shell Extension may be the only way. If you want to handle all files simultaneously then you'll probably need a Shell Extension as well.
An alternative to the context menu method, could be to have a drag-n-drop target on the user's desktop or something. Explore other ways that you could have the user submit your files to your application, because a Shell Extension is often far more effort than it is worth. I found this out the hard way and I think everyone else has too.
Apparently this supposed to be possible. For example opening and operating on a file with NOTEPAD, or HxD. But aren't they all text files...how would one specify which text editor to open the file and operate on the file with using the WINDOWS API. It is certainly not in "CreateFile".
Hopefully I'm understanding your question... The easiest way to do this is to launch the desired editor and pass the filename as an argument, rather than "invoking" the file (which will launch the default program associated with the file type).
For example, notepad.exe mytextfile.txt or gvim.exe mytextfile.txt.
If the editor is not on your %PATH%, you'll need to use a full path file name.
What are you trying to do, exactly? You could:
Maintain a list of editors that you expect to be installed and have entries for in the system's PATH (bad idea)
Have an editor/editors that you want to use, query the Windows registry to find the installation path of the editors (using RegGetValue), and launch the editor with CreateProcess) (a little better idea)
Query the registry to get the default editor for a given file type and then launch that editor using CreateProcess. (best idea)
But it all depends on what your goal is really.
Edit based on requirements
So, just so we're on the same page, from C++, you want to:
Take a command line parameter to your C++ application (filename)
Open that file in an arbitrary editor
Detect when the user has made changes to that file
Operate on the file contents
Is that correct?
If so, you could:
Use Boost libs to compute a CRC for the current data in the file
Launch an editor using one of the methods I initially described
Stick in a tight loop and sleep so you don't chew up resources while the initially computed CRC matches one calculated every iteration of the loop
Of course, there are all kinds of issues that you'd have to deal with (that's just a super simple way of describing the algorithm I might use), such as:
What happens if the user doesn't change the file?
What happens if the file isn't found?
I'm sure that there are a number of different methods of doing this, but this is the easiest method that I can think of at the moment (while still being able to be fairly certain of the changes).
Disclaimer: I haven't implemented something like this, so I might be completely off base ;)
Are you looking for the ShellExecute() or ShellExecuteEx() APIs on Windows? They'll launch whatever program is registered for a file (generally based on the filename extention).
How to read/write data into excel 2007 in c++?
Excel provides COM interface which you can use from your C++ application.
I have experience with Excel 2003 only but I think for excel 2007 it will also work.
This can be done e.g. with #import or in the way described in this article:
http://support.microsoft.com/kb/216686
There is a python solution (using COM dispatch) here: http://snippets.dzone.com/posts/show/2036
It's not C++, but the COM interface should be the same no matter which language you use, right?
You wouldn't need to port everything. Just __init__, set_range, get_value, set_value, save (or save_as), close, and quit. You might also need to dispose of garbage (as python has automatic gc).
Or you could just port (and modify) the following code (which I haven't tested, as I don't have excel anymore - you should probably check it by downloading python and pythonwin):
from win32com.client import Dispatch
app = Dispatch("Excel.Application")
app.Visible = True # spooky - watch the app run on your desktop!
app.Workbooks.Open("C:\\book.xls")
range = app.ActiveWorkbook.Sheets(1).Range("a1")
print 'The range was:'
print range.Value
range.Value = 42
print 'The value is now 42'
app.ActiveWorkbook.Save()
app.ActiveWorkbook.SaveAs("C:\\excel2.xls")
app.ActiveWorkbook.Close()
app.Quit()
# any gc to do?
Excel 2007 files are simply zip files. Try to rename .xlsx to .zip: you can extract files and folders. With a text editor you can view that they are all XML files.
So the solution:
use a common class to unzip your xlsx
use an xml parser to grab you data
if you have modified somethig, re-zip all
No COM object required.
Depending on your c++ compiler you can easyly find the required sources.
There are three main things you need to do.
1) Ensure pre-requisite files are installed and locatable.
Blindingly obvious I know, but make sure you have a suitable version of Excel installed, so that you can locate the required Microsoft libraries (and their locations).
namely MSO.DLL, VBE6EXT.OLB and EXCEL.EXE
2) Set up the Microsoft libraries.
In your C++ code, let's say you start with a simple console application, be sure to include the import libraries in any C++ application that interfaces with Excel. In my example I use:
#import "C:\\Program Files (x86)\\Common Files\\microsoft shared\\OFFICE11\\MSO.DLL" \
rename( "RGB", "MSORGB" )
using namespace Office;
#import "C:\\Program Files (x86)\\Common Files\\microsoft shared\\VBA\\VBA6\\VBE6EXT.OLB"
using namespace VBIDE;
#import "C:\\Program Files (x86)\\Microsoft Office\\OFFICE11\\EXCEL.EXE" \
rename( "DialogBox", "ExcelDialogBox" ) \
rename( "RGB", "ExcelRGB" ) \
rename( "CopyFile", "ExcelCopyFile" ) \
rename( "ReplaceText", "ExcelReplaceText" ) \
exclude( "IFont", "IPicture" ) no_dual_interfaces
3) Use the Excel Object Model in your C++ code
For example, to declare an Excel Application Object pointer for reading / writing an Excel Workbook:
Excel::_ApplicationPtr pXL;
pXL->Workbooks->Open( L"C:\\dump\\book.xls" );
And to access and manipulate the Excel Worksheet and the cells within it:
Excel::_WorksheetPtr pWksheet = pXL->ActiveSheet;
Excel::RangePtr pRange = pWksheet->Cells;
double value = pRange->Item[1][1];
pRange->Item[1][1] = 5.4321;
And so on. I have a more in-depth discussion at this following blog posting.
A low tech way I have used on a couple of projects is to make a simple script/macro/whatever that runs an external program. Write that external program in C++. Get that external program to read its input from and write its output to a .csv file (simple comma separated value text file). Excel can easily read and write .csv files, so this setup gives you everything you need to craft a viable solution.
This can all be done via the IDispatch interface. It can get really bloody complicated quickly (Cheers Microsoft) but at least once you've got your head round Excel you'll find integrating with any other MS application easy too :)
Fortunately there is someone over on codeguru who has made the process nice and easy. Once I'd read through that code I started to get my head round what excel was doing and, furthermore, i found it became REALLY easy to extend it to do other things that I wanted. Just remember that you are sending commands to Excel via the IDispatch interface. This means you need to think about how YOU would do something to figure out how to do it programatically.
Edit: the code guru example is for Excel 2003 but it should be fairly easy to extend it to 2007 :)
Start here with the OpenXml Sdk. Download the SDK from here. The SDK uses .NET, so you might need to use C++.NET
It has been a very long time but I have used the Jet OLEDB to get at Excel files. You might start searching in that direction.
I have used a 3rd Party component for this in the past: OLE XlsFile from SM Software (not free, but inexpensive). The advantage of this component over the Microsoft COM components is that you can use it to write XLS files even if Excel is not installed.
It also allows you to create speadsheets or workbooks with embedded formulas and formatting, something not possible if you use CSV files as an interchange format.
If you need the best performance, writing binary Excel files is the way to go and writing them is not as difficult as reading them. The binary file format is relatively well documented by the OpenOffice.org project:
http://sc.openoffice.org/excelfileformat.pdf
and Microsoft has also released the documentation:
http://download.microsoft.com/download/0/B/E/0BE8BDD7-E5E8-422A-ABFD-4342ED7AD886/Excel97-2007BinaryFileFormat(xls)Specification.xps
This solution will work best if you have to write many Excel files, mostly with data and little formatting, and if you don't want to open the files at the same time. If you write a single Excel file to open it afterwards, you can probably use the propsed C++ COM Interface as the other posters have explained.
Using the COM interface will necessitate expensive out-of-process calls unless you write an Excel COM Addin. If you write an Addin that imports your data into the current Excel sheet, filling the sheet will still be much slower than dumping a file, but for a single file at a time this can be acceptable depending on your user scenario. If you do decide to use the COM interface, minimize the number of calls to Excel. Use the methods that allow to insert an array of values if they exist, set style properties by row and column is possible and not per cell.
You can use ODBC to read and write data from an excel file easily without Excel. The link:Here
The link for how to write data using odbc: Here
I've used a set of C++ classes that is available from CodeProject before to write to XLS files.
It's really easy to use, and free! You can find it here.
Took me no time to set up.
What's the best way to programmatically merge a .reg file into the registry? This is for unit testing; the .reg file is a test artifact which will be added then removed at the start and end of testing.
Or, if there's a better way to unit test against the registry...
It is possible to remove registry keys using a .reg file, although I'm not sure how well it's documented. Here's how:
REGEDIT4
[-HKEY_CURRENT_USER\Software\<otherpath>]
The - in front of the key name tells Regedit that you want to remove the key.
To run this silently, type:
regedit /s "myfile.reg"
If you're shelling out, I'd use the reg command (details below). If you can tell us what language you're working with, we could provide language specific code.
C:>reg /?
REG Operation [Parameter List]
Operation [ QUERY | ADD | DELETE | COPY |
SAVE | LOAD | UNLOAD | RESTORE |
COMPARE | EXPORT | IMPORT | FLAGS ]
Return Code: (Except for REG COMPARE)
0 - Successful
1 - Failed
For help on a specific operation type:
REG ADD /?
REG DELETE /?
[snipped]
I looked into it by checking out my file associations.
It seems that a .reg file is just called as the first parameter to the regedit.exe executable on Windows.
So you can just say regedit.exe "mytest.reg". What I'm not sure of is how to get rid of the dialog box that pops up that asks for your confirmation.
Use the Win32 API function ShellExecute() or ShellExecuteEx(). If the comment is 'open' it should merge the .reg file. I haven't tested it, but it should work.
One of the most frustrating things about writing unit tests is dealing with dependencies. One of the greatest things about Test-Driven Development is that it produces code that is decoupled from its dependencies. Cool, huh?
When I find myself asking questions like this one, I look for ways to decouple the code I'm writing from the dependency. Separate out the reading of the registry from the complexity that you'd like to test.