Question

I have a legacy MFC application that seems to be using in several wrong ways the conversion to HashKey...

There is the following template method:

template<class ARG_KEY>
AFX_INLINE UINT AFXAPI HashKey(ARG_KEY key)
{
    // (algorithm copied from STL hash in xfunctional)
    ldiv_t HashVal = ldiv((long)(ARG_KEY)key, 127773);
    HashVal.rem = 16807 * HashVal.rem - 2836 * HashVal.quot;
    if (HashVal.rem < 0)
        HashVal.rem += 2147483647;
    return ((UINT)HashVal.rem);
}

It gives me an error on the line:

ldiv_t HashVal = ldiv((long)(ARG_KEY)key, 127773);

One output says:

error C2440: 'type cast' : cannot convert from 'GUID' to 'long'

If static cast the error is cannot convert from 'HMENU' to 'long' which is also a problem.

Is there a way to cast whatever comes into long??

Solution

Thanks to @Joachim Pileborg

After navigating the almost 1million lines of code it seems that the program is only using the first variable from the GUID structure.

Thus I overloaded the template according to what is expected:

template<>
AFX_INLINE UINT AFXAPI HashKey<GUID>(GUID key)
{
    return key.Data1>>4;
}
Was it helpful?

Solution

This was the process necessary to create the specialization:

  1. Investigate ALL the uses of HashKey for GUID
  2. Check what is using or expecting

The conclusion was that the original version in MFC 6.0 used the first element of the GUID structure (with a very very nasty conversion). The first element of the GUID is a long type so the casting was all right.

Thus we create the following specialization IN THE PROJECT and not in the MFC libraries:

template<>
AFX_INLINE UINT AFXAPI HashKey<GUID>(GUID key)
{
    return key.Data1>>4;
}

Which is how the HashKey is being used in the project.

OTHER TIPS

This is risky but if you must, you can try:

long key1 = *(long*)(&key);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top