How to fill ProgID after creation ATL COM object. ProgID importance - c++

I'm trying to create com object using C++ ATL. In add ATL Simple Object wizard I forgot to fill ProgID field. Is it possible to add ProgID later? I found that it is located in *.rgs file. Is it safe to edit this file? If yes, how to deal with UI that are also required in definition syntax in this case?
As I understod ProgID is important when you want to create COM object for example using command CreateObject in VB. What are other methods to create com object with late binding when ProgId is missing?

Yes, it's safe to edit the .rgs file. It's just a registry script - defining where the ProgID is going to be inserted into the registry. (When using C++ in Visual Studio, it seems to me that they don't seem to have as many automatically-generated files that you shouldn't edit. The ones that are generated are usually labelled. (Like the stubs and headers that are generated from .idl files.))
All the wizards are doing is automatically filling in some of the files that you would normally need to do by hand. Knowing what gets put where is really useful, so, if you've already tracked down this bit, it sounds like you're on the right track.

Related

Creating mfc c++ controls during runtime

My current project has me attempting to create dialogs based on a .json file.
I want to parse through a .json file and use the information given to construct a dialog in mfc c++ on the fly.
Can this be done? I have never created buttons/edit boxes/drop downs/ect including configuration on the fly.
Thanks in advance for the insight/advice.
Best Regards,
shalld

Static library vs Dynamic library for storing classes

I was storing my classes in static libraries.For example modify the original header file like adding line pragma comment(lib,"MyClass") then copy files to the visual studio's include and lib folder.Whatever everything was fine until i wanted to add Menu to my class.Lib files does not have resources so i am going to use dynamic link library for this class.Is this logical that storing classes in dlls? also i dont know how to use dlls like that...
is there some examples for this? Also there is classes like CFileDialog that have resources(dialog) this kind of classes use static libraries or dynamic ?
To me, this sounds like "the wrong place to split things". In other words, if your library needs a resource that is part of the application, then it's probably not meant to be a library in the first place - it is either a proper standalone DLL that contains its own resource, because it has a complete and standalone functionality, or it's actually part of the main executable, and uses the main executable's resource.
The point of making something into a library is that it allows the separation of the library contents from the main application.
Another option is of course that you pass in the relevant resource information from the main application to your class in the library [this works for a a DLL too, of course].
My point here is that a menu is something that belongs to the main application - it knows what it has under File, Edit, View, etc. If you are writing a word processor, you may have things like "Spell checking" in there, but you don't want "Spell checking" in the Photo Editor software, but you probably want some way to get colour profile information to match the monitor's colour balance with the official colours. So your "class" to handle menus probably shouldn't "know" what the menus are, but just how to deal with menus in general - what menus you have comes from the actual main application.
I know this isn't really a direct answer to your questions...

How to add a new row to Excel file using unmanaged C++?

How can I add a new row (with contents) to an existing Excel .xls file using unmanaged C++ running on Windows?
I don't mind using OLE, COM, or any external free library, whatever is the easiest way.
There is a COM interface which is well documented.
I'd suggest you start with the Workbooks.Open method to open an existing excel file.
If you only need basic features (no formatting, formula's, ...), you can also use BasicExcel: A c++ library which doesn't have any dependencies (it reads and writes the excel file as a compound file) and is much easier to use than the COM interface (at least from c++).
I've used SQL to do this. I don't have sample code handy, but a quick google search brought this up: Link
Hope its helpful.
If you have no restrictions to use managed libraries you can check NPOI, a managed library to handle Excel file format.
Since it is managed it should be possible to register it as a COM server. If, for any reason, it proves hard/impossible to register it as a COM server you can write a thin COM server (either in C++ or C# or whatever you prefer) to expose just the functionality you need to your unmanaged C++ code.
I've used this one: ExcelFormatLib, it's great and simple to use, C++, well maintained, compiles and works without any trouble.

Implementing a custom wizard for Visual Studio for custom C++ classes

in order to make new C++ classes conform to some rather picky coding conventions (upfront: I am not in the position to discuss these...), I was thinking about a way of generating stubs for new C++ classes. Currently, everyone is doing copy-paste, regularly missing some detail. The IDE in use is MS Visual Studio 2005, but I think there has not been much of a change in 2008 and 2010 regarding these topics.
My first idea was to implement a command line script to do this, which would be fairly straight forward to do. Alternatively, I thought about using a default VS extension mechanism for better IDE integration. So, this would be hooking in some custom stuff when selecting Add->New Item... on a filter (Solution Explorer).
After some investigation, I found out there is an easy-to-use templating mechanism, which unfortunately does not work for C++ (http://msdn.microsoft.com/en-us/library/6db0hwky%28v=vs.80%29.aspx). For C++, it seems like you have to implement a custom wizard instead, incorporating html for the layout and javascript for the logic.
Regarding the custom wizard approach, I've come to the conclusion that this would require some effort (at least for me) to get this done. MSDN is not very detailed on this topic. I've found some walkthroughs in the web, which are dealing with custom wizards for projects only (Add->New Project... instead of Add->New Item...).
So, here's the qn: Anyone having done this or something similar? Is it (better IDE integration) worth the effort (coping with the details of custom wizard implementation), or would you suggest the go for the command line tool instead? More than two days of work would not pay off in the current project, I guess.
If you think custom wizards are great, maybe you can give some hints to get started. Also, maybe there are alternatives I did not come up with. VS Add-Ins seemed to be over the top for this, and adapting e.g. VC\vcprojectitems\newc++file.cpp will not do the job.
Thanks in advance and best regards...
Jerb
I have done something similar using a custom wizard.
For my purposes I just wanted to inject some simple macros into each class to insert it into a static factory object.
I didn't need to modify the actual wizard, just copied the default generic class wizard and modified the code generation javascript.
The documentation is rubbish on the topic, but here is a good place to start for the javascript (that is where you are going to get the most power):
http://msdn.microsoft.com/en-us/library/t41260xs(v=VS.71).aspx
The default class wizard javascript is located here:
C:\Program Files\Microsoft Visual Studio
10.0\VC\VCWizards\CodeWiz\Generic\Class\Scripts\1033\default.js
Duping the Project
Duplicating this is trickier than I thought, you need to copy and paste the
Microsoft Visual Studio 10.0\VC\VCWizards\CodeWiz\Generic\
folder, then head to:
C:\Program Files\Microsoft Visual Studio 10.0\VC\VCAddClass\Generic
Dupe this folder and modify the Generic.vsdir inside it to point to a dupe of ..\Simple.vsz (a file in the VCAddClass folder).
The Simple.vsz file points back to the location of the "Generic" folder in VCWizards you duped at the start, so point your new simple.vsz at that.
Code Generation
As for the actual code generation, its not all that difficult to pick up. To get things started faster, here is the General way the default.js works:
Once the wizard is finished, the code gen method kicks off from:
function OnFinish(selProj, selObj)
selProj is (as far as I can tell) an instance of EnvDTE.Project
http://msdn.microsoft.com/en-us/library/envdte.project.aspx
Getting information from the wizard seems to be based around:
wizard.FindSymbol("CLASS_NAME")
The real magic starts to happen on the selProj.CodeModel object
oCM.AddClass(strClassName, strHeader, vsCMAddPositionEnd, "", "", vsCMAccessDefault);
It seems the convention for these methods to add anything to a file simply modifies the file as a single action, as it requires the file path as its paramter.
This returns a CodeClass instance and can be added to by its methods like:
AddAttribute
AddFunction
...
This is quite restricting if you are looking for very strict code formatting (or in my case inserting macros that don't fit normal code syntax.
The simple way around this is to just build a string on your own for the parts that you need full control over using the EditPoint interface.
An EditPoint is a location inside a code file to which you can call methods like:
EditPoint.Insert(string)
Editpoint.InsertFromFile(path)
To get an EditPoint in a location where you would want to insert code, simply use the location of one of the existing items in the code gen file (like class or constructor) and get a TextPoint using .StartPointOf or .EndPointOf and manipulating the parameters.
Once you have a TextPoint you can create an EditPoint like so:
newclass.EndPointOf(vsCMPartBody).CreateEditPoint().Insert("\nprivate:\n REGISTER_TYPE_MEMBER("+strClassName+");\n");
To get a TextPoint inside the .cpp file instead:
oConstructor.StartPointOf(vsCMPartWhole,vsCMWhereDefinition).CreateEditPoint().Insert("REGISTER_TYPE_BODY_ID("+strClassName+",REPLACE_ID);\n\n\n");
This gives you the power to do anything you want via JScript string manipulation as long as you can find the input data you need via the wizard (which I have not yet delved into)

C++ DLLs newer version

All,
I have a C++ COM DLL written using Visual Studio. All the interfaces have GUIDs in idl and rgs files. We want to create a new DLL with brand new GUIDs as we want it to co-exist on the same machine with the old one but with different logic. The number of GUIDs is more than 200. Is there a tool that finds the GUIDs and replaces them ? I noticed for every GUID in idl file there are 3 same ones in the rgs files.
I am not really into C++ COM but I have to get this done :
Rgds,
MK
We use the following dumb but working approach: we store all the GUIDs relevant to COM classes and interfaces as #defines in one header that is included into the .idl file. When we need to break compatibility we just open that file and manually replace all the GUIDs. Not very elegant, but reliable and works.
So my suggestion is that you just search for all the GUIDs in your project and replace them. I guess you'll be better off moving them all in one place at the same time.