Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to ensurs that my codes can run on both 32bit and 64bit systems.Please can the following code *run compile on 64bit compiler*? If no, can someon show me a modified version that can * complile on 64bit compiler*?The code is:
C++
INT_PTR GetShortPath( THAR *szFullPath, TCHAR *pszShortPath, INT_PTR iStrlen)
{
     if(!lstrlen(pszFullPath))
      {
          return (INT_PTR)FALSE;
       }
      
     TCHAR *p, *q;
      p = pszFullPath, q = pszShortPath;
      INT_PTR I = 0;
      while(*p)
      {
          *q = *p;
           p++,q++;
    
       }
       while(*p != T('\\'))
       {
          if( I   <  iStrlen)
            {
                *q = *p;
               p++, q++,I++;
           }
           else
           {
              return (INT_PTR)FALSE;
            }
        }
        *q = T('\0');
        return (INT_PTR)TRUE;
}
Posted
Updated 18-Feb-14 9:00am
v9
Comments
Sergey Alexandrovich Kryukov 18-Feb-14 12:51pm    
Why won't you compile, execute it and find out? From the first glance, I cannot see why not...
—SA
Gbenbam 18-Feb-14 13:44pm    
Please see if you can react to my latest comments.
Gbenbam 18-Feb-14 14:16pm    
I apologise for bordering you.The comment I wanted you to see was not submitted to the server.But I have embedded it in the reply to the last comment.
W Balboos, GHB 18-Feb-14 13:01pm    
The only thing that comes to mind as to why the poser would bring up this question is the use of his predefined types, such as INT_PTR. He's incrementing a pointer in his while-loop.

A solution would be to compile separate 32 & 64 bit versions to handle the difference.
Gbenbam 18-Feb-14 13:44pm    
Please see if you can react to my latest comments.

1 solution

This code contains the following errors:

1. The return value type is a boolean value. Use type bool (and the values true and false, rather than TRUE and FALSE). If the return value is passed to a system function, you may need to use BOOL instead

2. the variables iStrlen and I are used as subscript into an array. They should be defined as std::size_t. You can use SIZE_T instead, which is the MS-Version of the same type, but their inability to define and use types properly has already been sufficiently documented by the fact they needed to introduce the *_PTR types - so why bother with unreliable type definitions when you can use standardized types?

3. Literals of type TCHAR need to be defined using the _T() macro, not T(). You are missing the leading underscores.

Fixed those types and values for you:
C++
bool GetShortPath( THAR *szFullPath, TCHAR *pszShortPath, std::size_t iStrlen) //fixed
{
     if(!lstrlen(pszFullPath))
      {
          return false;//fixed
       }
      
     TCHAR *p, *q;
      p = pszFullPath, q = pszShortPath;
      std::size_t I = 0;//fixed
      while(*p)
      {
          *q = *p;
           p++,q++;
    
       }
       while(*p != _T('\\'))//fixed
       {
          if( I   <  iStrlen)
            {
                *q = *p;
               p++, q++,I++;
           }
           else
           {
              return false;//fixed
            }
        }
        *q = _T('\0');//fixed
        return true;//fixed
}

Please note that you should always use the correct type - else you might just be using no type at all and program in Assembler!

Don't use the MS types unless you really, really must (i. e. you're interfacing to system functions that require them, or using a proprietary type definition such as TCHAR).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900