Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Which header file is CString in?

  1. Dec 5, 2008 #1
    Hello

    Just a quickie. could any one tell me which header file i have to include to be able to use CString in VC++? I have looking on the net and I can't find one that works. One suggests to use AFX.h but I get more errors with that included than without it.
     
  2. jcsd
  3. Dec 5, 2008 #2
    CString is defined in afx.h
    your errors maybe because you include also windows.h
    if you include any MFC headers (afx.h), don't include windows.h
    if it doesn't solve your problem, tell what errors you got...
     
  4. Dec 5, 2008 #3
    I've included the file you mentioned and I still get the same error.

    The attachment is a screen shot of the errors.

    Can anyone make sense of them?
     

    Attached Files:

  5. Dec 5, 2008 #4
    It would be more helpful if you'd post the actual source you're trying to compile.
     
  6. Dec 5, 2008 #5
    Here is the code:

    Code (Text):
    void IPaddress(){
        CString strTemp;
        struct hostent *host;
        struct in_addr *ptr; // To retrieve the IP Address

        DWORD dwScope = RESOURCE_CONTEXT;
        NETRESOURCE *NetResource = NULL;
        HANDLE hEnum;
        WNetOpenEnum( dwScope, NULL, NULL, NULL, &hEnum );
        WSADATA wsaData;
        WSAStartup(MAKEWORD(1,1),&wsaData);
        if ( hEnum )
        {
            DWORD Count = 0xFFFFFFFF;
            DWORD BufferSize = 2048;
            LPVOID Buffer = new char[2048];
            WNetEnumResource( hEnum, &Count, Buffer, &BufferSize );
            NetResource = (NETRESOURCE*)Buffer;

            char szHostName[200];
            unsigned int i;

            for ( i = 0; i < BufferSize/sizeof(NETRESOURCE); i++, NetResource++ )
            {
                if ( NetResource->dwUsage == RESOURCEUSAGE_CONTAINER && NetResource->dwType == RESOURCETYPE_ANY )
                {
                    if ( NetResource->lpRemoteName )
                    {
                        CString strFullName = NetResource->lpRemoteName;
                        if ( 0 == strFullName.Left(2).Compare("\\\\") )  
                            strFullName = strFullName.Right(strFullName.GetLength()-2);
                            gethostname( szHostName, strlen( szHostName ) );
                            host = gethostbyname(strFullName);
                            if(host == NULL)
                                continue;
                            ptr = (struct in_addr *)
                            host->h_addr_list[0];                    

                            // Eg. 211.40.35.76 split up like this.            
                            int a = ptr->S_un.S_un_b.s_b1;  // 211          
                            int b = ptr->S_un.S_un_b.s_b2;  // 40
                            int c = ptr->S_un.S_un_b.s_b3;  // 35
                            int d = ptr->S_un.S_un_b.s_b4;  // 76

                            strTemp.Format("%s -->  %d.%d.%d.%d",strFullName,a,b,c,d);
                            AfxMessageBox(strTemp);
                    }
                }
            }

        delete Buffer;
        WNetCloseEnum( hEnum );
    }

    WSACleanup();
    }
     
  7. Dec 5, 2008 #6
    31si could you post the FULL set of #include lines from the top? Sometimes windows headers can do funny things if you don't #include them in the correct order.
     
  8. Dec 6, 2008 #7
    The headers are as below:

    #include "stdafx.h"
    #include "CP2121 Assessment.h"
    #include <stdio.h>
    #include <String.h>
    #include <mmsystem.h>
    #include <d3d9.h>
    #include <D3DX9.h>
    #include <d3d9types.h>
    #include <winsock2.h>
    #include <iostream>
    #include <windows.h>
     
  9. Dec 6, 2008 #8
    So, maybe I am getting in over my head here, but...

    Does this help any? The poster has error messages very similar to yours, and the responses say:

    From poking around it sounds like stdafx is not an actual standard header file, but is a local file that VC will have generated for just your project. Is it possible you need to edit stdafx to add the include for whatever header really includes CString, or reconfigure your project so that it generates stdafx to do this for you?
     
  10. Dec 6, 2008 #9
    I can see what you are saying but If I were to include the String.h file, which is where I think CString is, in the .cpp file would it not import be the same as it importing in one of the other header files?
     
  11. Dec 6, 2008 #10
    I'm not exactly sure, I'm sorry, I don't have experience specifically with MFC. But:

    - You are correct that importing the CString header file directly in the .cpp is exactly the same as importing the CString header file in stdafx.h.

    - I can definitely tell you CString would NOT be in String.h. String.h is a standard C header and not a Microsoft thing.

    The advantage of doing it through stdafx.h, as I understand the link there, is that VC++ is supposed to have facilities for automatically generating stdafx.h's, therefore you don't have to know what the header file for CString is... (Unless you're using VC++ express in which case you apparently don't have MFC access at all...)
     
  12. Dec 6, 2008 #11
    OK, next step anyone got any idea's which header file it is in?
     
  13. Dec 6, 2008 #12
    Right, CString is defined in AFX.h. I have imported it and..... I get less errors.

    The next error is show below:
     

    Attached Files:

  14. Dec 6, 2008 #13
    better Idea. I have now imported #include "atlstr.h"

    And I get only one error but I think I can get around it. I will keep you up to date with advances.
     
  15. Dec 6, 2008 #14
    Problem solved. Thank you all for all of your help you have been a tremendous help.

    No doubt, I will be back for more assistance very soon.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Which header file is CString in?
Loading...