Click here to Skip to main content
15,921,660 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Message for window overlapping? Pin
Tomasz Sowinski13-Jun-01 2:20
Tomasz Sowinski13-Jun-01 2:20 
GeneralRe: Message for window overlapping? Pin
BryanBHU13-Jun-01 15:42
BryanBHU13-Jun-01 15:42 
GeneralRe: Message for window overlapping? Pin
Tim Deveaux14-Jun-01 2:52
Tim Deveaux14-Jun-01 2:52 
GeneralCRicheditView n Dialog (MDI) Pin
Majid Khattak12-Jun-01 14:22
Majid Khattak12-Jun-01 14:22 
GeneralCopy icon to clipboard.. Pin
J Patel12-Jun-01 12:37
J Patel12-Jun-01 12:37 
GeneralSafely remove files... Pin
12-Jun-01 11:26
suss12-Jun-01 11:26 
GeneralRe: Safely remove files... Pin
Jeff Naber12-Jun-01 11:33
Jeff Naber12-Jun-01 11:33 
GeneralBuilding a linked list inside a DLL before DllMain Pin
Steve The Plant12-Jun-01 11:02
Steve The Plant12-Jun-01 11:02 
Hi!

Recently, I've figured out a method to build a linked list of items
in an .exe before WinMain gets called. The method relies on the fact
that a global instance of a class will always call it's contructor
before WinMain is called. Some example code of what I'm doing:


cLinkedList g_List; // my linkedlist of data. it's global and currently empty

class cListBuilder // this class will add a new node in the linkedlist
// when a variable of this class is declared
{
cListBuilder()
{
cData* poData = new cData;
g_List.AddNode("NodeName", poData);
}
}

cListBuilder ListBuilder; // because this is a global, it's constructor is
// called first, right before WinMain, filling the
// linked list with one node.

int WinMain()
{
// by the time we get here, m_gList has one element
printf("NumNodes: %d\n", g_List.GetNumNodes());
}

I just made up the code from memory, so it might not compile out of the box.

Anyway, this works inside an .exe, but it doesn't when I try this method inside
a DLL. The implementation differences being that:

1) g_List is now declared in it's own seperate .cpp file (gList.cpp)

2) WinMain is replaced by DllMain (duh). The function is now seperated into
it's own file, DllMain.cpp. It calls "extern cLinkedList g_List" to have
access to the list.

3) the decleration of cListBuilder is in it's own .h as well. (cListBuilder.h) It
gets g_List by calling "extern cLinkedList g_List", the same way DllMain.cpp does

So now, the project looks like this:

-- gList.cpp -------
cLinkedList g_List;

-- End gList.cpp ---

-- cListBuilder.h ------

extern cLinkedList g_List; // the g_List *should* be coming from gList.cpp

class cListBuilder // this class will add a new node in the linkedlist
// when a variable of this class is declared
{
cListBuilder()
{
cData* poData = new cData;
g_List.AddNode("NodeName", poData);
}
}

static cListBuilder ListBuilder; // now declared static to have it persist

-- End cListBuilder.h --

-- DllMain.cpp ---------
extern cLinkedList g_List;

int DllMain()
{
// g_List *should* have one node in it when it reaches here.
}
-- End DllMain.cpp -----


That's pretty much how my project looks like right now. It's very
similar to what I was doing before in the .exe. The problem is
that by the time I reach DllMain, the linkedlist is empty even
though cListBuilder::cListBuilder() was called. I placed a break
point inside the constructor, and it definately stops there. The
constructor does add the new node to the linkedlist without fail.
But, like I said, by the time I reach DllMain, it's like I never
touched g_List. It's like the actions I did inside cListBuilder::
cListBuilder() are completely forgotten as soon as I go out of
the scope of cListBuilder.h. And the g_List's destructor never
gets called, either, so it's not like it actually empties the list.

The problem is compounded when I add more files like cListBuilder
that try to add more nodes inside g_List. For example, if I added
a new file to the project called cListBuilder2.h that looked like:

-- cListBuilder2.h ------

extern cLinkedList g_List; // the g_List *should* be coming from gList.cpp

class cListBuilder2
{
cListBuilder2()
{
cData* poData = new cData;
g_List.AddNode("NodeName2", poData);
}
}

static cListBuilder2 ListBuilder2;
-- End cListBuilder2.h --


So, because I now have ListBuilder and ListBuilder2 declared as global static
variables, they both would try to add a node into g_List. But the weird thing
is that if ListBuilder's constructor gets called first and adds a node, by the
time ListBuilder2's constructor is called, the list is magically empty again.
Then ListBuilder2 would add it's own node and then by the time we reach DllMain,
the list is empty once more.

I can't figure out why this doesn't work in a DLL when it works perfectly in
an .exe. I don't know how Dlls handle global variables differently than
executables.

I'm not quite sure if I actually explained the problem really well. It's a pretty
weird situation, if you ask me. So, if anybody has any insight into this, it would
be really helpful. I can try to clear this up some more, if I can.

Thanks for taking the time to read this monster post.

Steve
wrlddomprod@hotmail.com
GeneralRe: Building a linked list inside a DLL before DllMain Pin
Tomasz Sowinski12-Jun-01 11:20
Tomasz Sowinski12-Jun-01 11:20 
GeneralRe: Building a linked list inside a DLL before DllMain Pin
Tim Deveaux12-Jun-01 14:13
Tim Deveaux12-Jun-01 14:13 
GeneralRe: Building a linked list inside a DLL before DllMain Pin
Steve The Plant12-Jun-01 15:24
Steve The Plant12-Jun-01 15:24 
GeneralRe: Building a linked list inside a DLL before DllMain Pin
12-Jun-01 16:32
suss12-Jun-01 16:32 
GeneralRe: Building a linked list inside a DLL before DllMain Pin
Steve The Plant12-Jun-01 17:36
Steve The Plant12-Jun-01 17:36 
GeneralExporting classes in a dll that uses MFC Pin
Wes Jones12-Jun-01 9:59
Wes Jones12-Jun-01 9:59 
GeneralText viewer application Pin
Jeff Naber12-Jun-01 9:55
Jeff Naber12-Jun-01 9:55 
GeneralRe: Text viewer application Pin
Jeff Naber12-Jun-01 11:29
Jeff Naber12-Jun-01 11:29 
Generalopen files. Pin
12-Jun-01 8:58
suss12-Jun-01 8:58 
GeneralRe: open files. Pin
Carlos Antollini12-Jun-01 9:05
Carlos Antollini12-Jun-01 9:05 
GeneralI did not understand you !!!! Pin
12-Jun-01 19:17
suss12-Jun-01 19:17 
GeneralRe: open files. Pin
12-Jun-01 9:06
suss12-Jun-01 9:06 
Generalwell done !!!!! It was realy silly misprint! Pin
12-Jun-01 19:25
suss12-Jun-01 19:25 
General*.dsw and Borland Compiler Pin
12-Jun-01 8:19
suss12-Jun-01 8:19 
GeneralRe: *.dsw and Borland Compiler Pin
Tim Deveaux12-Jun-01 12:18
Tim Deveaux12-Jun-01 12:18 
GeneralSerial Port Communication. Pin
John Uhlenbrock12-Jun-01 7:44
John Uhlenbrock12-Jun-01 7:44 
GeneralRe: Serial Port Communication. Pin
John Uhlenbrock12-Jun-01 8:55
John Uhlenbrock12-Jun-01 8:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.