locked
Converting VS 6 MFC project to VS 2010 RRS feed

  • Question

  • Has anyone ever successfully ported a VS 6 MFC program into VS 2010? So far the only method I know of that works is to tediously recreate the GUI and then copy/past all the underlying code.

    My latest attempt to use VS 2010's auto-convert was 90% successful. The VS 6 workspace was converted to a VS 2010 solution, and after a moderate amount of anguished frustration and repeated forum help manged to get the thing to compile after adding '#define _AFX_ALL_WARNINGS' to stdafx.h among other little changes.

    Currently I'm stuck trying to identify and solve the run-time errors that were introduced by the conversion. 

    In debug mode everything crashes almost immediately with a debug assertion failure at:

    _AFXWIN_INLINE HINSTANCE AFXAPI AfxGetResourceHandle()
    { ASSERT(afxCurrentResourceHandle != NULL);
    return afxCurrentResourceHandle; }

    coming from: m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

    In release mode everything lasts longer, but after hitting a popup message saying "Encountered an improper argument" the program window comes up missing all textBox default text and all comboBox options. Code tracing seems to indicate that error occurs at about line 630 of microsoft's dlgcore.cpp file at CreateDlgIndirect(lpDialogTemplate, CWnd::FromHandle(hWndParent), hInst)) in INT_PTR CDialog::DoModal().

    Attempts to trace the error further have failed and instead unexpectedly leave me at AFX_ISOLATIONAWARE_STATICLINK_FUNC(int ,MessageBoxA,(HWND hWnd,LPCSTR lpText,LPCSTR lpCaption,UINT uType),(hWnd,lpText,lpCaption,uType),0). 

    Any ideas on what is causing these errors? The code compiles with zero warnings at warning level2.
    Wednesday, July 31, 2013 4:23 PM

Answers

  • Convert it for this you simply can use _T like SetWindowText(_T("hekki")); or prefix it with L

    This error message means that you are trying to pass a multi-byte string (const char [x]) to a function which expects a unicode string (LPCTSTR). Change your project configuration to use multibyte strings as you already know that

    Thanks


    Rupesh Shukla



    • Edited by Pintu Shukla Wednesday, July 31, 2013 7:41 PM
    • Proposed as answer by Anna Cc Monday, August 5, 2013 7:04 AM
    • Marked as answer by Anna Cc Wednesday, August 7, 2013 9:21 AM
    Wednesday, July 31, 2013 7:38 PM
  • First thing before delete always make sure that pointer is a valid pointer.And after deleting pointer set your pointer to NULL . Second i am not sure ( free((void*)locbytestream); ) what is going on here why this casting and that is also to void . remeber deleting a void pointer produces unexpected result . If it work that is only your luck .

    if(pointer)

    {

     delete [] pinter;

    pointer= NULL; because deleting a NULL pointer doesn't crash your app .

    }

    One more thing whenever use new you should use delete and with malloc use free . mixing is not a good idea

    Thanks


    Rupesh Shukla




    • Edited by Pintu Shukla Wednesday, July 31, 2013 8:52 PM
    • Proposed as answer by Anna Cc Monday, August 5, 2013 7:03 AM
    • Marked as answer by Anna Cc Wednesday, August 7, 2013 9:21 AM
    Wednesday, July 31, 2013 8:37 PM

All replies

  • Your dialog, icon, text, and other resources are not being loaded.  When this happens, it helps to see what exactly are the resources currently in your .exe (or .dll, depending on what you're building).  So in Visual Studio, do a File | Open and open the .exe or .dll, specifying "Resources" in the File Open dialog, and you will see a window with all the resources in it (the ID's should be numbers, but you can click on them and see that the resources are there).  I suspect you will find missing resources or resources without the proper id's.

    -- David

    Wednesday, July 31, 2013 6:10 PM
  • A good idea is create a empty Win32 Console Application and then copy all te required header file source file and resource to your project . make all required changes with respect to lib and dll and  rebuild your solution . by this way you can eliminate most of the issue.

    Thanks


    Rupesh Shukla

    Wednesday, July 31, 2013 6:18 PM
  • @David: Neat trick, I just learned something new. Regrettably, it didn't show any obvious problems. The only two resources missing were two bitmap files showing assortments of window-close x's. Not only was every dialog, icon, menu, and string table in the proper place but the 2010 version appears to have one extra dialog and 14 extra cursor resources.

    @Shukla: I just gave that a try. Copied every .cpp, .h, .lib, .dll, and the .rc file over. After setting MFC to shared dll and changing all the <iostream.h> over to <iostream> I am left with about 200x errors that say: error C2664: 'CWnd::SetDlgItemTextW' : cannot convert parameter 2 from 'const char [2]' to 'LPCTSTR'

    Any ideas on what I may have forgotten?

    Edit: Just remembered the need to switch to multi-byte chars. One moment...


    • Edited by PsiDog Wednesday, July 31, 2013 7:05 PM
    Wednesday, July 31, 2013 6:56 PM
  • Convert it for this you simply can use _T like SetWindowText(_T("hekki")); or prefix it with L

    This error message means that you are trying to pass a multi-byte string (const char [x]) to a function which expects a unicode string (LPCTSTR). Change your project configuration to use multibyte strings as you already know that

    Thanks


    Rupesh Shukla



    • Edited by Pintu Shukla Wednesday, July 31, 2013 7:41 PM
    • Proposed as answer by Anna Cc Monday, August 5, 2013 7:04 AM
    • Marked as answer by Anna Cc Wednesday, August 7, 2013 9:21 AM
    Wednesday, July 31, 2013 7:38 PM
  • Progress!

    Shukla's method was apparently cleaner, because I quickly found my argument fault this time around: m_text.SetWindowText(""); The problem appears to be that that m_text, of type CStatic, is never set to equal anything.

    Or at least, I think that was the problem. If I comment those lines out I end up with a brand new never-before-seen error:

    HEAP[ASCBDataSim.exe]: Heap block at 01AF4040 modified at 01AF404A past requested size of 2
    
    Windows has triggered a breakpoint in ASCBDataSim.exe.
    
    This may be due to a corruption of the heap, which indicates a bug in ASCBDataSim.exe or any of the DLLs it has loaded.
    
    This may also be due to the user pressing F12 while ASCBDataSim.exe has focus.

    The above error occures at free(locbytestream); where locbytestream is defined as: unsigned char *locbytestream = ( unsigned char * ) malloc(((locNumofHexChars/2))*sizeof(unsigned char));  //2 Hex Chars = 1 Byte

    Any idea why m_text.SetWindowText("") and locbytestream apparently worked fine under VS 6 but cause trouble under VS 2010?

    Wednesday, July 31, 2013 7:46 PM
  • Unicode issue you can read more here

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/c1b08c0a-a803-41c3-ac8c-84eba3be1ddb/faq-cannot-convert-from-const-char-to-lpctstr

    Second malloc has nothing to do with VS6.0 or 2010 . As you mentioned that you are getting error while freeing locbytestream  . In that scenario before free you can check if pointer is not NULL only  then go for free . can you post the code where it is getting crash .

    Thanks


    Rupesh Shukla

    Wednesday, July 31, 2013 8:04 PM
  • void __cdecl _free_base (void * pBlock)
    {
    
            int retval = 0;
    
    
            if (pBlock == NULL)
                return;
    
            RTCCALLBACK(_RTC_Free_hook, (pBlock, 0));
    
            retval = HeapFree(_crtheap, 0, pBlock); // Failure happens here
            if (retval == 0)
            {
                errno = _get_errno_from_oserr(GetLastError());
            }
    }

    //Original Code: char * locmsg= new char[HexStr.GetLength()]; unsigned char *locbytestream = ( unsigned char * ) malloc(((locNumofHexChars/2))*sizeof(unsigned char)); //2 Hex Chars = 1 Byte // stuff free(locmsg) ; //Crash one of these two lines free(locbytestream); ////////////////////////////// //Revision #1: Same Crash. char * locmsg= new char[HexStr.GetLength()]; unsigned char *locbytestream = ( unsigned char * ) malloc(((locNumofHexChars/2))*sizeof(unsigned char)); //2 Hex Chars = 1 Byte // stuff if(locbytestream == NULL) bool DebugBreakpointHere = true; free((void*)locbytestream); //Crash one of these two lines delete[] locmsg; ////////////////////////////// //Revision #2: Same Crash char *locmsg = new char[HexStr.GetLength()]; unsigned char *locbytestream = new unsigned char[locNumofHexChars/2 +1];

    //Stuff

    delete[] locmsg; //Crash one of these two lines delete[] locbytestream;




    • Edited by PsiDog Wednesday, July 31, 2013 8:31 PM
    Wednesday, July 31, 2013 8:30 PM
  • First thing before delete always make sure that pointer is a valid pointer.And after deleting pointer set your pointer to NULL . Second i am not sure ( free((void*)locbytestream); ) what is going on here why this casting and that is also to void . remeber deleting a void pointer produces unexpected result . If it work that is only your luck .

    if(pointer)

    {

     delete [] pinter;

    pointer= NULL; because deleting a NULL pointer doesn't crash your app .

    }

    One more thing whenever use new you should use delete and with malloc use free . mixing is not a good idea

    Thanks


    Rupesh Shukla




    • Edited by Pintu Shukla Wednesday, July 31, 2013 8:52 PM
    • Proposed as answer by Anna Cc Monday, August 5, 2013 7:03 AM
    • Marked as answer by Anna Cc Wednesday, August 7, 2013 9:21 AM
    Wednesday, July 31, 2013 8:37 PM