computer name length issue
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: computer name length issue

  1. #1
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    computer name length issue

    Hello everyone,

    Any macro used to represent computer name max length (just host name max length itself, not full-qualified with domain suffix, for example, I just want host name "somehost" in example.com domain, but not "somehost.example.com")?

    http://msdn.microsoft.com/en-us/libr...01(VS.85).aspx

    MAX_COMPUTERNAME_LENGTH is too small and just 15 characters.

    thanks in avdance,
    George

  2. #2
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,610

    Re: computer name length issue

    I think it is 256, in which case you could use MAX_PATH.
    Nobody cares how it works as long as it works

  3. #3

    Re: computer name length issue

    GetComputerNameEx:
    The length of the name may be greater than MAX_COMPUTERNAME_LENGTH characters because DNS allows longer names. To ensure that this buffer is large enough, set this parameter to NULL and use the required buffer size returned in the lpnSize parameter.

  4. #4
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: computer name length issue

    Hi zerver,

    Quote Originally Posted by zerver View Post
    I think it is 256, in which case you could use MAX_PATH.
    Is there a built-in macro?

    regards,
    George

  5. #5
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: computer name length issue

    Hi molotov, as mentioned in the MSDN document, if MAX_COMPUTERNAME_LENGTH macro is not suitable for DNS name, which macro should I use? Thanks.

    Quote Originally Posted by molotov View Post
    regards,
    George

  6. #6

    Re: computer name length issue

    There is no macro.
    lpBuffer
    The length of the name may be greater than MAX_COMPUTERNAME_LENGTH characters because DNS allows longer names. To ensure that this buffer is large enough, set this parameter to NULL and use the required buffer size returned in the lpnSize parameter.

  7. #7
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: computer name length issue

    Quote Originally Posted by George2 View Post
    Hi molotov, as mentioned in the MSDN document, if MAX_COMPUTERNAME_LENGTH macro is not suitable for DNS name, which macro should I use? Thanks.



    regards,
    George
    Many Windows API allow you to send a NULL buffer, and they return you the size required for the buffer. So you should use this technique, as molotov explained.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  8. #8
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: computer name length issue

    Thanks molotov. There is macro.

    Code:
    #include <Windns.h>
    
    
    //
    //  DNS Names limited to 255, 63 in any one label
    //
    #define DNS_MAX_NAME_LENGTH             (255)
    #define DNS_MAX_LABEL_LENGTH            (63)
    #define DNS_MAX_NAME_BUFFER_LENGTH      (256)
    #define DNS_MAX_LABEL_BUFFER_LENGTH     (64)
    Quote Originally Posted by molotov View Post
    There is no macro.
    regards,
    George

  9. #9
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: computer name length issue

    I have a better idea, cilu. Please refer to my post #8. :-)

    Quote Originally Posted by cilu View Post
    Many Windows API allow you to send a NULL buffer, and they return you the size required for the buffer. So you should use this technique, as molotov explained.
    regards,
    George

  10. #10
    Join Date
    Apr 1999
    Posts
    27,427

    Re: computer name length issue

    Quote Originally Posted by George2 View Post
    I have a better idea, cilu. Please refer to my post #8. :-)
    Why is that a better idea? You still have no idea how long the actual name is, and macros aren't going to help you. Why not use the function as it was intended?

    1) Call it once to return the length of the data.
    2) Call it again, using the length returned from 1)

    There are many Windows API functions that return a character buffer. How big that buffer can be is determined by calling the function with NULL or some other value that indicates "return the length to me, and not the data". Then you call the function a second time, now that you know the length of the data.

    I'll even give you how to do this in C++ with any function that works this way:
    Code:
    #include <vector>
    #include <windows.h>
    
    void foo()
    {
        DWORD length;
    
        // get the length first
        GetComputerNameEx( "whatever", NULL, &length );
    
        // create a buffer with the length
        std::vector<TCHAR> buffer(length + 1, 0);
    
        // now get the name
        GetComputerNameEx("whatever", &buffer[0], &length);
    );
    This way, you get the entire name, regardless if it is 1 character, 10, characters, or 10,000 characters.

    Regards,

    Paul McKenzie

  11. #11

    Re: computer name length issue

    Those constants are from the DNS API. They are not mentioned in the API doc for GetComputerNameEx. The documented method for "buffer sizing" with respect to this API has been discussed and illustrated.

  12. #12
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: computer name length issue

    Thanks molotov,

    Quote Originally Posted by molotov View Post
    The documented method for "buffer sizing" with respect to this API has been discussed and illustrated.
    You mean the approach of calling it twice?

    regards,
    George

  13. #13
    George2 is offline Elite Member Power Poster
    Join Date
    Oct 2002
    Posts
    4,468

    Re: computer name length issue

    Thanks Paul,

    Quote Originally Posted by Paul McKenzie View Post
    Why is that a better idea? You still have no idea how long the actual name is, and macros aren't going to help you. Why not use the function as it was intended?

    1) Call it once to return the length of the data.
    2) Call it again, using the length returned from 1)

    There are many Windows API functions that return a character buffer. How big that buffer can be is determined by calling the function with NULL or some other value that indicates "return the length to me, and not the data". Then you call the function a second time, now that you know the length of the data.

    I'll even give you how to do this in C++ with any function that works this way:
    Code:
    #include <vector>
    #include <windows.h>
    
    void foo()
    {
        DWORD length;
    
        // get the length first
        GetComputerNameEx( "whatever", NULL, &length );
    
        // create a buffer with the length
        std::vector<TCHAR> buffer(length + 1, 0);
    
        // now get the name
        GetComputerNameEx("whatever", &buffer[0], &length);
    );
    This way, you get the entire name, regardless if it is 1 character, 10, characters, or 10,000 characters.

    Regards,

    Paul McKenzie
    You are the man!

    regards,
    George

  14. #14
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: computer name length issue

    Quote Originally Posted by George2 View Post
    Thanks molotov,



    You mean the approach of calling it twice?

    regards,
    George
    Exactly.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  15. #15
    Join Date
    Mar 2002
    Location
    NY, USA
    Posts
    12,097

    Re: computer name length issue

    Quote Originally Posted by George2 View Post
    You mean the approach of calling it twice?
    WHY are you still asking, is it was stated that this was the proper methodology all the way back in "Reply #3", back on December 16th!!!!!!
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009
    In theory, there is no difference between theory and paractice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center