Hottest Forum Q&A on CodeGuru - from the week of March 22nd, 2004


Lots of hot topics are covered in the Discussion Forums on CodeGuru. If you missed the forums this week, you missed some interesting ways to solve a problem. Some of the hot topics this week include:

Is it safe to call LoadLibrary inside a constructor? (top)  


j0nas needs to know whether is it safe to call the LoadLibrary function inside a constructor. What do you think? Is it safe?

Is it safe to call LoadLibrary inside a constructor of a global
object in a DLL?

My guess is no because it can deadlock the same way as calling
LoadLibrary inside DllMain. But, I'm not 100% sure. Any ideas?

Well, the global constructors are called before DllMain, so you would assume the answer would then be, yes it would be unsafe, because of circular DLL references.

If you want, step into the code via the disassembler, placing breakpoints in the ctor's and in the dllmain and using the OS symbols; it should be very easy to see what happens.

So, given that case, the answer would be to have an init function in the DLL.

Can I override the MFC inheritance? (top)  


mddd asked an interesting question. Take a look at what he wants to do.

Here is the MFC class hierachy:
Will I be able to do something like this?
My SDialog derives from CDialog.

My SDialog has functionality that I want to be able to use in
my class (CMyPropertyPage) that I will derive from CPropertyPage.

One obvious approach is to use multiple inheritance:

CMyPropertyPage : public CPropertyPage, public SDialog

Multiple inheritance is strongly discouraged with MFC—especially when deriving from two classes that are both derived from CWnd as in your case—this won't work anyway.

You should probably implement the functionality you need in a class derived from CPropertyPage. If you absolutely need to share some part of the implementation for both CDialog- and CPropertyPage-derived classes, you could still consider creating a mix-in class with that behaviour (however, that class must not be derived from CWnd or any other MFC class such as CObject). You then could use multiple inheritance, deriving CYourDialog from CDialog and the mix-in class, and CYourPropertyPage from CPropertyPage and the mix-in class.

Which STL data structure should I use? (top)  


avi123 has some difficulties finding the correct data structure. Can you help him?

I'm having diffculties to find the right data structure for me.
I have a structure with five fields. Something like this:
struct myStruct
   int iField1;
   int iField2;
   int iField3;
   int iField4;
   string sField5;
I need some kind of data structure that will hold unknown
numbers of elements of myStruct and I need to have each fields as
a key field.

If I have only 1 field as the key I would have used hash_map.
for example if the key is iField1

then I would have used hash_map with int as key and with myStruct
as the data.

I need to be able to have five keys (and return the correct element
according to 1 of them, meaning the key is not combination of them,
but each one of them should be able to retrive the correct element).

You should use a vector<const myStruct> to store the data and a map<int,const myStruct&> to store an index that points to the correct value of myStruct for each value of iField1. Use another map for each additional index.

Adding and deleting myStructs will be some work. Updating even more, so I recommend implementing it as delete+insert (thus the const). At last, wrap the vector and maps up in another class to make the whole thing easier to use. For example,

class my_container
   void add(const myStruct& s)
      // push_back to the vector, update the maps, etc.

   // other functions for getting at the data

   std::vector<myStruct> structs_;
   std::map<int, myStruct*> map1_;
   // etc for the other maps

Which DLL contains CTime? (top)  


love2mao is working with an application in which he used CTime and CTimeSpan. Now, he wants to distribute this application to the client. He wants to know which DLL he needs to restribute along with the application.

In my app, I used CTime and CTimeSpan and I want to distribute my
app. I chose the MFC DLL as shared to get a small .exe. How can I
know which DLL I need to run my app in a new environment?

In theory, you don't need to know which DLL provides which feature because your clients should automatically have copies of all the DLLs they need on their system. They're installed with the standard distributions of Windows. The only times you need to distribute a DLL are either when you built it yourself or if your app only works in debug mode. However, you're not allowed to distribute debug versions of the standard Microsoft DLLs; it contravenes the license agreement.

About the Author

Sonu Kapoor

Sonu Kapoor is an ASP.NET MVP and MCAD. He is the owner of the popular .net website DotNetSlackers publishs the latest .net news and articles - it contains forums and blogs as well. His blog can be seen at:


  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • IBM Worklight is a mobile application development platform that lets you extend your business to mobile devices. It is designed to provide an open, comprehensive platform to build, run and manage HTML5, hybrid and native mobile apps.

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds