tags:

views:

583

answers:

2

I have these two lines of code.

CString strHost = AfxGetApp()->GetProfileString(_T("WebServices"), _T("Server"));
AfxMessageBox(strHost);

Nowhere in the app do I set the value. (the installer does that). So the strHost, should be the same no matter where or when this line is run.

Here's what I've got. Press A -> run function that contains code -> see expected data; Press B -> do some windows callbacks -> run function that contains code -> see "";

I'd think something in B was clearing the value, but if I press B first and then A, A yeilds what I want not the "".

Sadly I don't have access to the code that holds the windows callbacks, or rather it has got to be the biggest pile of badly managed obfiscation I've ever seen.

What I'm currious about is, if the GetProfileString is pulling the data from somewhere it shouldn't be? It's the only thing I can think of.

Questions.
1.) Is there a way to confirm I'm still working with the same "key"?
2.) Has anyone ever encountered this before?

Please if you think this is a bad question, and wish to mark it as such, leave a comment so I can fix it.


Though I didn't find the answer fully I did track down something interesting.
I'm using AfxGetApp()->GetProfileString..
It seems the function AfxGetApp() is returning different things at different times.. I'm not sure how that could happen, but at least I can now account for it.

+1  A: 

GetProfileString() can also read from a .ini file, besides the registry. If the call to GetProfileString() is done on a different CWinApp-derived object than the one in the main application (in a dll for example), it might try to read from the application's .ini file. Try stepping into GetProfileString() to find out what's going on.

However my advcie is: don't use GetProfileString(). Use CRegKey to directly query the registry. This is probably not what you want to hear because it's a major to have to pass the registry key around. What I usually do is #define a global APPLICATION_REG_KEY macro in stdafx.h. It upsets the purists but it's very convenient.

Roel
That's what I was thinking I might have to do. The problem is the application is a mess. (I inherited it from a guy who got fired) and the get and set profile string is used all over the bloody place. I'd want to replace it everywhere and that would mean full regression test.. Yuck.. But thanks.
baash05
Sadly I can't step into the code. This is a PPC app and the problem only happens on one unit type, when the user swipes a credit card. I think you're right though. The problem is, how do I fix it.
baash05
Yeah well in that case I feel for you as I don't see any other way. Maybe try with a tool like RegMon to see if any registry access is done in your Press B scenario. Not sure how feasible that is in your environment.
Roel
it's not.. But I think your idea about the DLL affects, might hold water.. I'm putting afxmessage boxes in the dll to see what might be going on.
baash05
A: 

I found the answer.. Sort of. The AfxGetApp function returns different objects depending on where it's called.. If it's called in a dll, for instance, it returns a pointer to that. I think it even returns the wrong thing when the app is using the OS calls. This means the GetProfileString is for a different "profile".

I didn't find anyone who'd encountered this, but I consider the issue resolved. Feel free to add more "answers" if you don't..

baash05