-
December 16th, 2008, 07:26 AM
#1
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
-
December 16th, 2008, 08:40 AM
#2
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
-
December 16th, 2008, 09:23 AM
#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.
-
December 18th, 2008, 06:31 AM
#4
Re: computer name length issue
Hi zerver,
Originally Posted by zerver
I think it is 256, in which case you could use MAX_PATH.
Is there a built-in macro?
regards,
George
-
December 18th, 2008, 06:32 AM
#5
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.
Originally Posted by molotov
regards,
George
-
December 18th, 2008, 06:39 AM
#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.
-
December 18th, 2008, 07:19 AM
#7
Re: computer name length issue
Originally Posted by George2
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.
-
December 18th, 2008, 07:49 AM
#8
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)
Originally Posted by molotov
There is no macro.
regards,
George
-
December 18th, 2008, 07:50 AM
#9
Re: computer name length issue
I have a better idea, cilu. Please refer to my post #8. :-)
Originally Posted by cilu
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
-
December 18th, 2008, 08:03 AM
#10
Re: computer name length issue
Originally Posted by George2
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
-
December 18th, 2008, 09:07 PM
#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.
-
December 20th, 2008, 06:09 AM
#12
Re: computer name length issue
Thanks molotov,
Originally Posted by molotov
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
-
December 20th, 2008, 06:10 AM
#13
Re: computer name length issue
Thanks Paul,
Originally Posted by Paul McKenzie
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
-
December 20th, 2008, 06:40 AM
#14
Re: computer name length issue
Originally Posted by George2
Thanks molotov,
You mean the approach of calling it twice?
regards,
George
Exactly.
-
December 21st, 2008, 01:39 PM
#15
Re: computer name length issue
Originally Posted by George2
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,2010
In theory, there is no difference between theory and practice; 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|