Solved

Microsoft C++ code failing in executable that worked

Posted on 2016-10-23
9
52 Views
Last Modified: 2016-10-26
I suddenly have a failure on an application has been working for 15 years, and the executable has not been recompiled for the last 6 months.

I am using:
1. Operating System: Windows 10
2. Compiler: Microsoft Visual Studio 2010
3. Code: Microsoft C++ MFC SDK

Suddenly, all saves fail in a Microsoft procedure CRecentFileList [see Microsoft code below]. It doesn't fail until after a print to screen. This is true of executables from 6 months ago that have been working un until now. and happens on different Windows 10 machines.

Questions:
1. Is there any way to fix this?
2. If I can't fix it, can I eliminate: ENSURE(SUCCEEDED(hr)); Add(psi, strAppID); so that it doesn't fail?
3. I tried this, and it doesn't recognize that I changed it. How do I get the compiler to recompile and recognize that I chaned filelist.cpp?
4. Would it help to go to Microsoft Visual Studio 2015?

---------------------------------------------------

MICROSOFT CODE WITH CALL....

The failure is in the code below.

Fails in call to afxGlobalData.ShellCreateItemFromParsingName below.
Returns: hr = 0x800401f0 CoInitialize has not been called. (normally hr==S_OK)
Called with: lpWPath = 0x00a5eee0 "C:\winfree\_small.fre"


filelist.cpp:
void CRecentFileList::Add(LPCTSTR lpszPathName, LPCTSTR lpszAppID)
{
	if (!afxGlobalData.bIsWindows7)
	{
		Add(lpszPathName);
		return;
	}

	CString strAppID = lpszAppID == NULL ? _T("") : lpszAppID;

#if (WINVER >= 0x0601)
	ASSERT(AfxIsValidString(lpszPathName));

	Add(lpszPathName);

	HRESULT hr = S_OK;
	CComPtr<IShellItem> psi = NULL;

#ifdef UNICODE
	hr = afxGlobalData.ShellCreateItemFromParsingName(lpszPathName, NULL, IID_IShellItem, reinterpret_cast<void**>(&psi));
#else
	{
		USES_CONVERSION;
		LPOLESTR lpWPath = A2W(lpszPathName);
		hr = afxGlobalData.ShellCreateItemFromParsingName(lpWPath, NULL, IID_IShellItem, (LPVOID*)&psi);  <---------------------- Point of failure
	}
#endif

	ENSURE(SUCCEEDED(hr));

	Add(psi, strAppID);
#endif
}

Open in new window

0
Comment
Question by:DickStone
  • 5
  • 2
  • 2
9 Comments
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 250 total points
ID: 41856267
If you are saying that all or most of the same Windows 10 program running on different machines all started failing w/o any rebuilding within a small time window, then possibly a Windows Update has affected you.

If you restore your system to a restore point when your program worked, does the program work again?
Control Panel\All Control Panel Items\Recovery
0
 

Author Comment

by:DickStone
ID: 41856305
Yes. The same executable started failing on all Windows 10 machines at exactly the same place. It's always caused by the MS Recent File routine on the line indicated, when a print is followed by a save.

I'm sure that it is a Windows update being incompatible with something in the program that's causing it. However, I'm not sure for how long it's been tht it hasn't worked.

Telling all users to restore their machines and never update again really isn't a choice. I need to fix the problem or work around the failing MS code.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 41856449
>> Telling all users to restore their machines and never update again ...
Nope, never suggested that.

>> I'm sure that it is a Windows update being incompatible
Ok, that's what I thought, but suggested the experiment to be sure. Had this remark been in the OP, I would not have suggested this test.
0
 
LVL 32

Accepted Solution

by:
sarabande earned 250 total points
ID: 41857324
did you check the file path C:\winfree\_small.fre to be still accessible from user account?

is the contents of the file ok? can it be changed - say by deleting one or more from list. what happens if you do so? can you add to recent file list up to the maximum or does it crash before?

did you install visual c++ 2015 redistribution additionally to vs2010 redistribution? check if there are redistributions added by Windows update (or any other program). also check contents of PATH (open a command window and type PATH) environment variable and compare between computers which still work and others.

if all fails, it is not difficult to provide a 'recent file' functionality not using CRecentFileList. you might try before to add a CoInitialize(); call before using CRecentFileList in your code. if the error message is true (where i have doubts), it also could solve the issue.

Sara
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:DickStone
ID: 41858653
>> did you check the file path C:\winfree\_small.fre to be still accessible from user account?

1) There is no problem writing to it. If the program prints to the screen, then this starts happening after each print..

As another clue, saving to a different file causes the problem to permanently go away.

> is the contents of the file ok? can it be changed - say by deleting one or more from list. what happens if you do so? can you add to recent file list up to the maximum or does it crash before?

Don't understand this.

> did you install visual c++ 2015 redistribution additionally to vs2010 redistribution? check if there are redistributions added by Windows update (or any other program). also check contents of PATH (open a command window and type PATH) environment variable and compare between computers which still work and others.

My guess is that the redistribution was changed with a Windows update. It's failing on any machine I checked.

>if all fails, it is not difficult to provide a 'recent file' functionality not using CRecentFileList. you might try before to add a CoInitialize(); call before using CRecentFileList in your code. if the error message is true (where i have doubts), it also could solve the issue.

This is all buried deep in the MS code. I never call CRecentFileList . It's called during a file save.

IMPORTANT: I'd love to change CRecentFileList to remove the error check , but don't  know how. How do I either override that function in my code. Or can I change the MS code and then recompile (how?)?
0
 

Author Comment

by:DickStone
ID: 41858873
I looked at the reference to the other problem. It appears to be exactly what happens to me, event upgrading from C++ 6. I don't understand why it took 5 years to manifest, though.

I tried adding the initialization. But I got (for either CoInitialize or CoInitializeEx:

2>c:\wfree\winfree.cpp(712): error C2660: 'CoInitialize' : function does not take 0 arguments
When I look at the definition, it's:

__checkReturn WINOLEAPI  CoInitialize(__in_opt LPVOID pvReserved);
What am I missing for an argument.
0
 

Author Comment

by:DickStone
ID: 41859119
WORKING!     Thank you!

At the beginning of InitInstance(), I added :
      int hResult = CoInitialize(NULL);      // 2016-10-25 Added
      if ( hResult )
    {
        AfxMessageBox("Initialization failed");                        // IDP_OLE_INIT_FAILED
        return FALSE;
    }
0
 

Author Closing Comment

by:DickStone
ID: 41859127
Apparently, this was caused by the prgram being set up 15 years ago, and missing an initialization.
0
 
LVL 32

Expert Comment

by:sarabande
ID: 41859832
great.

answers to some open questions:

> is the contents of the file ok? can it be changed - say by deleting one or more from list. what happens if you do so? can you add to recent file list up to the maximum or does it crash before?
 Don't understand this.
i meant whether there is a file C:\winfree\_small.fre and that it can be opened as a text file by Notepad or Visual Studio, and that it contains a list of recent file entries.

This is all buried deep in the MS code. I never call CRecentFileList . It's called during a file save.
the mfc framework allowed you to opt for an automatically handled recent file list when the project was created. to locate where this happened, you would set a breakpoint to filelist.cpp and after the error you could have looked into the stack and should be able to locate the code where it decides to store the current filepath to recent file list. then you would reset the Option for mfc to handle recent file list and add your own implementation.

we are all happy that it is no more necessary.

this was caused by the prgram being set up 15 years ago, and missing an initialization.
the CoInitialize call is needed to initialize the COM library of Windows by your current thread. this may have worked for 15 years if - for example - mfc has made the CoInitialize indirectly with any other call to COM. for example by a call into an Active-X control like a date-picker. if Windows 10 replaced the Active-X by a .NET component, the CoInitialize would no longer called (by your ui thread) and therefore the next call to COM failed. of course, this is a speculation.

Sara
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now