Static variables initialization problem with MFC DLL

Hi, 

I have the following code, working well in my MFC app, where a 
static member variable is initialized with the result of a static 
method, as this :

   int MyClass::anInt = MyClass::InitTheInt();

The problem is when this code is used in a DLL -using MFC in a shared DLL-,
the initialization is simply ignored !!!!

What's going on?

Thx.
0
kesseu (2)
4/26/2004 4:00:36 PM
vc.mfc 33608 articles. 0 followers. Follow

3 Replies
708 Views

Similar Articles

[PageSpeed] 17

Herbert wrote:

>Hi, 
>
>I have the following code, working well in my MFC app, where a 
>static member variable is initialized with the result of a static 
>method, as this :
>
>   int MyClass::anInt = MyClass::InitTheInt();
>
>The problem is when this code is used in a DLL -using MFC in a shared DLL-,
>the initialization is simply ignored !!!!
>
>What's going on?

How do you determine this? Please give a more complete example, including a
minimal class definition, and indicate what goes into which files.

-- 
Doug Harrison
Microsoft MVP - Visual C++
0
dsh (2498)
4/29/2004 4:37:56 PM
"Doug Harrison [MVP]" <dsh@mvps.org> wrote in message news:<5fb290phu0k82qlp87ockc5tko6m0c57ia@4ax.com>...
> Herbert wrote:
> 
> >Hi, 
> >
> >I have the following code, working well in my MFC app, where a 
> >static member variable is initialized with the result of a static 
> >method, as this :
> >
> >   int MyClass::anInt = MyClass::InitTheInt();
> >
> >The problem is when this code is used in a DLL -using MFC in a shared DLL-,
> >the initialization is simply ignored !!!!
> >
> >What's going on?
> 
> How do you determine this? Please give a more complete example, including a
> minimal class definition, and indicate what goes into which files.

No need to be complicated. With a simple project with a simple cpp file,
if we have the class MyClass defined as follows :

class MyClass
{
   private :
     static int anInt;

   public :
     static InitTheInt(){ return LOWORD(GetKeyboardLayout(0)); }
};

and the declaration of member 'anInt' like this :

int MyClass::anInt = MyClass::InitTheInt();

This code works and behave correctly in an Application Project (MFC or not).
However, when done in an MFC DLL Project, the initialization is ignored.

I found also that simply declaring a global variable, and initializing with
the result of a global function, like this :

 int anInt = GetKeyboardLayout(0);

....leads to the same consequences : works except when code is in an MFC DLL Project.

I suspect a misconfigured project setting, because I also have access violation
errors if I use thread local variables, declared with '__declspec(thread)'.

I'm currently using VC6, with "Use MFC in a shared DLL" using 
"Debug Multithreaded DLL" RTL.

Thanks.
0
kesseu (2)
4/30/2004 11:08:48 AM
Herbert wrote:

>No need to be complicated. With a simple project with a simple cpp file,
>if we have the class MyClass defined as follows :
>
>class MyClass
>{
>   private :
>     static int anInt;
>
>   public :
>     static InitTheInt(){ return LOWORD(GetKeyboardLayout(0)); }
>};
>
>and the declaration of member 'anInt' like this :
>
>int MyClass::anInt = MyClass::InitTheInt();
>
>This code works and behave correctly in an Application Project (MFC or not).
>However, when done in an MFC DLL Project, the initialization is ignored.
>
>I found also that simply declaring a global variable, and initializing with
>the result of a global function, like this :
>
> int anInt = GetKeyboardLayout(0);
>
>...leads to the same consequences : works except when code is in an MFC DLL Project.
>
>I suspect a misconfigured project setting, because I also have access violation
>errors if I use thread local variables, declared with '__declspec(thread)'.
>
>I'm currently using VC6, with "Use MFC in a shared DLL" using 
>"Debug Multithreaded DLL" RTL.

That doesn't answer the questions I asked last time, nor does it give me any
new clues. I have several DLLs, MFC extension and non-MFC, that use dynamic
initialization of globals, and they work fine. Again, how do you determine
it's not working? What are you expecting to observe, what do you actually
observe, and what does "observation" consist of? If you can demonstrate this
in a sample project, save the project to a zip file and send it to me, and
I'll have a look. 

-- 
Doug Harrison
Microsoft MVP - Visual C++
0
dsh (2498)
4/30/2004 4:47:09 PM
Reply:

Similar Artilces:

Problem with shutting down and starting up.
Question about Microsoft Outlook 2003 When I check my e-mail the first time, it works find. When I close it the Outlook Icon stays on the taskbar. When I go back to check e-mail again, it does not open. If I click on the taskbar icon this message comes up ´┐ŻOutlook is not responding. If you are using an exchange server e-mail account, you can cancel pending server requests by clicking on the Outlook icon in the notifications area, and then clicking Cancel Server Request on the shortcut menu. ´┐Ż I use a pop3 account, no exchange server. The only way that I can't use Outlook is to reboot. T...

Junk Senders.txt variables?
Hi, my Junk Senders.txt file is starting to get HUGE - because for a long time I have diligently added every spam sender to the list (added a "add to junk mail list" button next to the delete button in the toolbar:) However, a little experimentation appears to indicate that "?" and "*" are allowed wildcard! eg ????@no.domain.name.given seems to filter all senders such as 1234@no.domain.name.given out, but ??**@no.domain.name.given doesn't.... it seems to only filter out 12ab@no.domain.name.given combinations. These wildcards however don't function consiste...

how to create chart with five data variables
I need to create a chart off of a spreadsheet with five variables I have to show: Room Fiscal Year (2003, 2004, 2005, 2006) Business Hour usage After-Hours usage Quantity (number of times the room was used that year) Currently I have the data as Fiscal Year in rows by Business or After Hours (FY03 Business Hours, FY03 After Hours, FY04 BH, FY04AH, etc.) Rooms in columns, and Quantity as cell value. What I want to do is create a stacked column chart that shows has Room for the X-axis value, Quanity for the Y-axis, Fiscal Year on the Z-axis (so it would be a 3-dimensional chart) and stack the ...

Problem with Equation Display
I am a resonably experienced Excel user trying to use to enter some equations into a new spreadsheet. Something's screwy because the cells where I typed my equations are displaying the equation and not the result. I tried doing things differently - using Excel to prod me to enter the equation, and even using simple equations like "=1/2" to try to get a numerical result. No luck, all I get is a text form of the equation. The Tools/Options/View/Formulas checkbox is clear, so that is not the problem. What can I have set wrong? Doug Are the cells in question formatted as...

XLODBC32.DLL
Everytime when I open Excel 2000, it came with an error message "the file is not in a recognizable format". When I clicked "yes" to see what text is contained in the file, it opened up XLODBC32.DLL in all grabage characters. If I clicked "cancel", a blank worksheet appeared. What is causing the problem and how to fix? My OS is Windows 98. Sam Try this KB Sam http://support.microsoft.com/default.aspx?scid=kb;en-us;213284 XL2000: Error Running Macro with Reference to Xlodbc.xla File -- Regards Ron de Bruin (Win XP Pro SP-1 XL2002 SP-2) www.rondebruin...

How to call a non static function from a static function
Hi I need to go to a non static function from a static function?can anybody suggest me how to do it?any kind of help will be greatly appreciated. You need to pass the object whose function you want to call. static void s_foo( A* a ); class A{ public: A(); ~A(); void funcA(); }; main() { ... A a; s_foo(&a); } static void s_foo( A* a ) { a->funcA(); } -Seetharam ...

Problem with CFTPConnection
Hi I've an application that connects to an FTP Server and uploads a big directory (size over 500 MB). Now what happens is that after sometime the application stops uploading files and stays idle. The FTP server connection timeout value is set to 2 minutes. So the server seeing the connection idle for two seconds, disconnect the session and no more file is uploaded. On debugging I found out that after some time CFTPConnection.PutFile() method hangs on some random file and stays like that for 5 minutes (default send timeout value of CFTPSession). During this time the server connection timeo...

VISIO 2000 Problem
Dear All Please advise the following case The VISIO program fail to open the VISIO files and hang up (Get No Responding in the Task manager) in Windows 98, I have already tried to uninstall and reinstall all components of VISIO 2000 from CD-ROM Those files have no problem to be opened in other PC. Michael Lam System Administrator "K" Line Air Services (HK) LTD ------=_NextPart_0001_8629F87B Content-Type: text/plain Content-Transfer-Encoding: 7bit Hello Micheal, Try the following: 1. Put the visio installation CD/Located the visio installation source files location...

Adding variables
How do I set up a query to add groups of variables? Say, for instance, I want to add the variables FO and FE and OC and CT and TB. Then I want to add FN and VG, etc. I'm not quite following ... Where are these "variables" coming from? Queries use data from tables (and from other queries)... Are you saying that you have multiple fields/columns and you wish to sum them? If so, that's how you'd do it in a spreadsheet ... and Access is NOT a spreadsheet! Using multiple columns to store different numbers means both you and Access will have to work over...

Static CMap object
Hi all, I've declared in my .h file the following CMap type : typedef CMap<CString, CString&, MyClass*, MyClass*&> Map; ... ... class AnotherClass { ... public: static TrainBuilderMap BuilderMap; ... ... static void Register(CString name, MyClass* instance); }; In one of my CPP file call the Register method as following: AnotherClass::Register(_T("TEST"), this); The code of Register method is : void AnotherClass::Register(CString name, MyClass* instance) { AnotherClass::BuilderMap.SetAt(name, instance); } When linking my project there the following linker error :...

Parameter variables
Part of the DB I am working on contains a table for Emergency Response data. The primary key ([ERptNo]) is formatted as "yymmddhhmm_ _ _", the last 3 digits being the first 3 letters of the responders last name. If they were the Incident Commander, additional information is collected & added to the IC table, with a primary key ([ERNo]) of "yymmddhhmm". I've built a query with fields from these tables, and need to find a way to add parameters for a search which would open a report. I would like the IC report to show up as the first page, then all ...

Problems with Exchange in a clustered configuration
We have Exchange configured in an active-passive configuration. We have problems failing over the resources to the passive node. I'll move the resources in clusadmin and it appears that the resources moved fine. About 10 minutes later, it'll try to move the resources back and half the resource group will be on one node, while the group with be on another. The only thing That seems to prevent the failover is that the "Exchange HTTP Virtual Server Instance" and some point fails. At least this is the only thing reported in the System log. Event ID 1069. Anyone see...

ANN: XSE initial release (a.k.a. Xml Streaming Events)
Hi everyone! I've made the initial release of the Xml Streaming Events API to XML handing in .NET. Overview ------------------ The XSE allows an XmlReader-based approach to XML manipulation, with the concept of "Match Strategies" that detect when the underlying XML matches a certain expression (such as "kzu:customer/kzu:order", effectively an encapsulated finite state machine), and "Handlers" which are .NET delegates that are called when a match is detected. Example: XseReader xr = new XseReader(new XmlTextReader(uri)); XmlNamespaceManager mgr = new XmlName...

Problem with services calendar
Hi, Since a few days, I am not able to get access to the services calendar with the CRM web interface. I get the message : "recording non available" as you can see in the attached picture http://i38.servimg.com/u/f38/13/08/02/00/erreur10.png When I tried to reach the calendar directly on the server, an ASP warning is generated > ID : 1309 / Source : ASP.NET 2.0.50727.0 I found a hotfix which seems (http://support.microsoft.com/kb/941386/en-us) to correct this bug, but it is impossible to install. So, I do not know any more what to do, and where to search in order to corre...

Date Format Problem
I use money to manage all my UK accounts. I now have a US account. I have just downloaded the latest transactions from that US account and Money tell me it can't import them because the date format is wrong. Money helpfully tells me how to change the date format for my PC and then says i have to restart it for them to take effect but this is a global system change and not that helpfull when everything else is in UK format. Is there away I can get this account data into Money. Is there a translater for the QIF file perhaps? All Help appreciated Paul Martin Not nice but one solu...

A strange problem with Service ...
Hi: I have a very strange problem and could not make any sense out of it: I wrote a Windows service program (Win-XP). The program impersonates the loggedon user (a user with only "Guest" privilege) and access user's registry settings from HKEY_CURRENT_USER. I found out that the program works (which means successfully read values from the HKCU) ONLY if I pop up a message box (by calling MessageBox(...), does not matter what is shown by the message box) in the service program before the impersonating codes. If I do not show the message box in the service, I get error "...

Split variable and exec stored procedure
Hi all, having a variable with values separated by ; how can I split it to execute a stored procedure for each of them? Like declare @MyVariable varchar(50) set @MyVariable = 'value1;value2;value3' and exec MyStored for each of value1, value2, value3, etc. Thanks a lot. Luigi On Fri, 5 Feb 2010 01:18:01 -0800, Luigi wrote: >Hi all, >having a variable with values separated by ; >how can I split it to execute a stored procedure for each of them? > >Like > >declare @MyVariable varchar(50) >set @MyVariable = 'value1;value2;value...

Canadian edition bank list problem
For some reason my bank no longer shows up in the bank account list in the canadian edition. When i initially set up Money my bank was there and setup correctly. I messed a few things up so i wanted to start over now my bank isn't there ( Royal Bank of Canada). Now a lot of canadian banks aren't in the list. Has anyone else had this problem? Why would the bank list change? I checked XP's regional settings and everything is set to Canada. THanks, Christian ...

outlook express problem #10
Help! My Outlook Express 6.0 will send, recieve and open email but the message section is blank. The cursor has the hour glass by it like something is loading but the text never appears. I can click properties of the email and go to message source and read the text. I've run several different virus scans with no evidence of a virus. All esel works fine. Bubbajo bubbajo <anonymous@discussions.microsoft.com> wrote: > Help! My Outlook Express 6.0 will send, recieve and open > email but the message section is blank. The cursor has > the hour glass by it like somethi...

local variable inside static method
Hi all, I thought local variable inside static method is implicitly static but my test with threading say otherwise. Why is so? I had always been taught to declare the variable near its usage, but it is simply not so with static method. I think... static void StaticMethod() { string[] onetwothree = new[] { "one", "two", "three" }; // use onetwothree variable... } would be more elegant than static string[] onetwothree = new[] { "one", "two", "three" }; static void StaticMethod() { // use onetwoth...

IMAP Problems #3
I have always used Outlook Express 6 with IMAP without any problems. I installed Outlook 2003 last night, migrated my email accounts into it from Outlook Express, and I'm getting these errors: Task 'Checking for new mail in subscribed folders on <mailbox name>' reported error (0x800CCC0E) : 'Outlook is unable to download folder (null) from the IMAP e-mail server for account <mailbox name>. Error: Unable to connect to the server. If you continue to receive this message, contact your server administrator or Internet service provider (ISP).' I have 3 IMAP acc...

Problem with grid lines.
My Excell worksheets show horizontal grid lines but not vertical gridlines on screen. Making the sheet look like lined paper rather than fully grided! I have tried re-loading the software but that did not correct it. I have tried going into "Tools", "Options" and "View" unchecked gridlines box, closed Excel reopened it, then checked gridlines box but still does not bring back vertical gridlines. Help! Julian One suggestion...... Try playing with the "refresh" rate on your Monitor, especially if it is an LCD type. Right-click on Desktop>Proper...

Sync-problem with Windows and Button
I have a button, which open a window, when I click on it. When the window lost his focus he hides himself. Now when I click on the same button should show the window again or hide when the button the window is visible. The problem is when I click out of the window the window lost his focus (ok!). When I click on the button the windows has lost his focus, but the button means that I will show the window. Is nearly the same problem as a popup-button. Who knows a solution? Please help me, it make me creazy.... Michael "michael Rogowski" <michaelrogowski@freenet.de> wrote in ...

Rules problem with internal emails
I am having a problem with rules and Outlook that I have been unable to solve. I have a rule that when a message arrives and the sender's address contains "outside.com", it is moved to a specific folder. This rule works flawlessly and I have no problems. I have another rules that uses the same criteria, but the sender's address contains "inside.com". These emails are internal to my company (meaning the addresses are on the exchange server processing the rules). This rule does not work - the messages stay in my inbox and are not moved. I have no idea why. We rece...

exchange2003 front-end problem
hi,everyone I have 3 domain: mydomain.com,a.mydomain.com and b.mydomain.com. I setup exchange server in each domain and they worked well both in outlook and in OWA. I setup another exchange server 2003 in mydomain.com as front-end server.I want anyone in my 3 domain use OWA via this front-end server.IT worked well for user in mydomain.com and a.mydomain.com. But when user from b.mydomain.com connects to front-end server for OWA,they can not pass authentication and get 404 error at last.If they connect to exchange server in b.mydomain.com via OWA,everything is OK.I do not know what happene...