I am getting More data is available error with the GetComputerNameEx function, but no idea how to fix it.
This is my code:
int wmain()
{
COMPUTER_NAME_FORMAT nameType = ComputerNameDnsFullyQualified;
WCHAR computerName[MAX_COMPUTERNAME_LENGTH + 1];
DWORD size = ARRAYSIZE(computerName);
BOOL pcName = GetComputerNameEx(nameType, computerName, &size);
DWORD error = GetLastError();
if (pcName != 0)
{
wprintf("Computer name: %s\n", computerName);
}
else
{
wprintf(L"Error getting the name. Code: %li\n", error);
}
return 0;
}
No idea how to set size variable as output so I can declare the computerName array correctly.
You have to call the function twice; once with a null pointer to get the required size, and again with a buffer of (at least) the specified size. As the docs say:
To ensure that this buffer is large enough, set this parameter to NULL
and use the required buffer size returned in the lpnSize parameter.
This is a common pattern for Win32 functions. And yes, it does lead to a possible race condition, but that's just how it works.
Example
DWORD dwSize = 0;
if (GetComputerNameEx(nameType, nullptr, &dwSize))
{
WCHAR* computerName;
computerName = (WCHAR*)malloc(dwSize * sizeof(WCHAR));
if (GetComputerNameEx(nameType, computerName, &dwSize))
{
// use the name
}
free(computerName); // don't forget to free
}
Per the GetComputerNameEx() documentation:
lpBuffer [out]
A pointer to a buffer that receives the computer name or the cluster virtual server name.
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.
lpnSize [in, out]
On input, specifies the size of the buffer, in TCHARs. On output, receives the number of TCHARs copied to the destination buffer, not including the terminating null character.
If the buffer is too small, the function fails and GetLastError returns ERROR_MORE_DATA. This parameter receives the size of the buffer required, including the terminating null character.
If lpBuffer is NULL, this parameter must be zero.
For example:
int wmain()
{
COMPUTER_NAME_FORMAT nameType = ComputerNameDnsFullyQualified;
WCHAR *computerName = NULL, *computerNameNew;
DWORD size = 0;
BOOL pcName;
DWORD error;
do
{
pcName = GetComputerNameExW(nameType, computerName, &size);
if (pcName) break;
error = GetLastError();
if (error != ERROR_MORE_DATA) break;
computerNameNew = (WCHAR*) realloc(computerName, sizeof(WCHAR) * size);
if (!computerNameNew) {
error = ERROR_OUTOFMEMORY;
break;
}
computerName = computerNameNew;
}
while (1);
if (pcName)
{
wprintf("Computer name: %s\n", computerName);
}
else
{
wprintf(L"Error getting the name. Code: %ul\n", error);
}
free(computerName);
return 0;
}
That error means that the GetComputerNameEx function needs a larger buffer to store the returned string.
To avoid the race condition cited in Jonathan Potter's answer, you could do something like this:
LONG error = ERROR_MORE_DATA;
WCHAR* buffer = NULL;
DWORD bufferLength = /* Some initial reasonable length for the string buffer */;
while (error == ERROR_MORE_DATA) {
// Create a buffer with bufferLength size (measured in WCHARs)
buffer = realloc(buffer, bufferLength * sizeof(WCHAR));
if (GetComputerNameEx(nameType, buffer, &bufferLength)) {
error = ERROR_SUCCESS;
} else {
error = GetLastError();
}
}
if (error != ERROR_SUCCESS) {
// Some error occurred
...
}
// Use buffer containing computer name
// Don't forget to free(buffer)
Related
Hi I am trying to use a kobject to write to a int array from sysfs. So the input is a char* and a size variable. I cant seem to get this to work however. My expected input is "num1 num2 num3 "
static ssize_t pids_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) {
int num_count = 0;
int i = 0;
int result = 0;
int cur_pid = 0;
char *dst;
char *ddst;
printk(KERN_INFO "GPIO_DEGUG: enter");
dst = buf;
ddst = buf;
printk(KERN_INFO "GPIO_DEGUG: size of buffer %d ",count);
while(ddst < (buf + sizeof(size_t)*count)) {
ddst ++;
if (ddst[0] == ' ') {
result = kstrtoint(dst,10,&cur_pid);
dst=ddst+1;
printk(KERN_INFO "GPIO_DEGUG: kstrtoint suceeded %d ",cur_pid);
printk(KERN_INFO "GPIO_DEGUG: kstrtoint suceeded res: %d ",result);
pids[num_count] = cur_pid;
num_count += 1;
}
}
for(i=0;i<num_count;i++) {
printk(KERN_INFO "GPIO_TEST: pid: %d \n", pids[i]);
}
printk(KERN_INFO "GPIO_DEBUG: leaving\n");
return count;
}
When I echo "100 " > /sys/vt/vt7/pids I get
[ 2765.712770] GPIO_DEGUG: enter
[ 2765.724468] GPIO_DEGUG: size of buffer 5
[ 2765.735101] GPIO_DEGUG: kstrtoint suceeded 0
[ 2765.746526] GPIO_DEGUG: kstrtoint suceeded res: -22
[ 2765.757746] GPIO_DEBUG: leaving
I suppose this is an argument error any help would be nice, thanks.
Function kstrtoint expects full string to contain single integer value. The only exception is a newline character at the end of the string:
The string must be null-terminated, and may also include a single newline before its terminating null.
As you can see, string "100 " doesn't fit for that requirement: it contains exceeded space.
For parse only part of the string as an integer, you may use simple_strtol:
long val = simple_strtol(dst, &ddst, 10);
if(ddst == ddst) {/* Parsing has been failed. */};
While this function is marked as an obsolete, there is still some code in the kernel which uses it.
Another possibility is to use sscanf. It expects fixed number of integers in the string, but it is an usual situation with attributes: having complex representation of the attributes is not recommended:
The conventions for sysfs state that each attribute should contain a single, human-readable value; if you have a lot of information to return, you may want to consider splitting it into multiple attributes.
(Linux Device Drivers 3, chapter 14).
The kstrtoint function is defined here:
http://lxr.free-electrons.com/source/lib/kstrtox.c#L245
If you notice the last value of *res defined in the function is the value you wish to use. In your case cur_pid should be the value in which you want to print, the result should always be zero if it was successful. I would suggest checking result to make sure that the conversion has succeeded.
This should work:
int cur_pid, result;
char *dst = NULL;
cur_pid = result = 0;
dst = buf;
result = kstrtoint(dst, 10, &cur_pid);
if (result)
printk(KERN_INFO "GPIO_DEGUG: kstrtoint suceeded res: %d ", cur_pid);
else
printk(KERN_INFO "ERROR");
I am trying to show a single value's name using RegEnumValue (I can use RegGetValue, but i'm trying to learn how to use the RegEnumValue function), but I'm getting the error code 87, "The parameter is incorrect".
Here's my code:
int wmain()
{
//RegOpenKeyEx
HKEY hKey = HKEY_CURRENT_USER;
LPCWSTR subKey = L"WinSide";
DWORD options = 0;
REGSAM samDesired = KEY_QUERY_VALUE;
HKEY pkOpenResult;
//RegEnumValue
DWORD index = 0;
WCHAR valueName[16383];
LPWSTR pValueName = valueName;
DWORD size=sizeof(valueName);
DWORD reserved = NULL;
DWORD type;
WCHAR data[255];
LPWSTR pData=data;
DWORD sizeData = sizeof(data);
LONG openKey = RegOpenKeyEx(hKey, subKey, options, samDesired, &pkOpenResult);
if (openKey != ERROR_SUCCESS)
{
wprintf(L"Error opening the key. Code: %li\n");
}
else
{
wprintf(L"Key opened!\n");
LONG enumValue = RegEnumValue(pkOpenResult, index, pValueName, &size,
&reserved, &type, pData, &sizeData);
if (enumValue != ERROR_SUCCESS)
wprintf(L"Error code: %li\n", enumValue);
else
{
wprintf(L"Going to show the value's name here!");
}
RegCloseKey(pkOpenResult);
}
return 0;
}
What am I doing wrong?
So sorry for any mistake.
lpReserved must be NULL as stated in the documentation. You pass a non-null value. That is the reason for the failure. Remove the reserved variable and simply pass NULL for lpReserved.
The lpcchValueName argument specifies characters rather than bytes. You pass the number of bytes in error. That mistake won't necessarily hurt you now but there is a buffer overrun error in the making there so you do need to fix it.
I'm fairly new with C. I have a function called kstrfrom that needs to create and return a new kstring object that contains a copy of the contents of a null-terminated C string, including the null terminator.
The .length member of the returned kstring should be the length of cstr, plus one for the null terminator. The .data member should be a pointer to newly-allocated memory, into which you have copied the contents of cstr, including the null byte at the end.
If there is an error allocating memory, this function should call abort() or throw an uncaught exception.
kstring kstrfrom(const char *cstr)
{
int length=1;
while(*cstr!='\0')
{
cstr+=1;
length+=1;
}
int i = 0;
kstring cdest={NULL,0};
cdest.data = malloc(length+1);
if(cdest.data == '\0')
{
abort();
}
else{
while(cstr[i] != '\0')
{
cdest.data[i] = cstr[i];
i++;
}
}
cdest.data[i] = '\0';
cdest.data[++i] = '\0';
return cdest;
}
I've ran a few test cases:
Test 9/ 26: kstrfrom gives correct length skipped (0)
Test 10/ 26: kstrfrom contains null byte succeeded (1)
Test 11/ 26: kstrfrom contains correct data skipped (0)
Test 12/ 26: kstrfrom copies, not shares, data skipped (0)
As you can see I need help with giving correct link, containing correct data and copying data.
At the end of
while(*cstr!='\0')
{
cstr+=1;
length+=1;
}
You have lost the initial value of cstr
try
int length=1;
char * tmp = cstr;
while(*tmp!='\0')
{
tmp+=1;
length+=1;
}
You are not setting the length member...
cdest.length = length + 1;
Returning the kstring is problematic.
kstring res;
res = kstrfrom( "My String" ); /* works */
kstrfrom( "Another string" ); /* leaks memory */
Other comments are describing how you are ignoring language features.
Your code can be achieved more easily with...
kstring kstrfrom(const char *cstr)
{
kstring cdest={NULL,0};
cdest.data = strdup( cstr );
if( cdest.data == NULL ){
abort();
}
cdest.length = strlen( cstr ) + 1; /* not done in original example */
return cdest;
}
Just finished putting this function together from some man documentation, it takes a char* and appends a const char* to it, if the size of the char* is too small it reallocates it to something a little bigger and finally appends it. Its been a long time since I used c, so just checking in.
// append with realloc
int append(char *orig_str, const char *append_str) {
int result = 0; // fail by default
// is there enough space to append our data?
int req_space = strlen(orig_str) + strlen(append_str);
if (req_space > strlen(orig_str)) {
// just reallocate enough + 4096
int new_size = req_space;
char *new_str = realloc(orig_str, req_space * sizeof(char));
// resize success..
if(new_str != NULL) {
orig_str = new_str;
result = 1; // success
} else {
// the resize failed..
fprintf(stderr, "Couldn't reallocate memory\n");
}
} else {
result = 1;
}
// finally, append the data
if (result) {
strncat(orig_str, append_str, strlen(append_str));
}
// return 0 if Ok
return result;
}
This is not usable because you never tell the caller where the memory is that you got back from realloc.
You will need to either return a pointer, or pass orig_str by reference.
Also (as pointed out in comments) you need to do realloc(orig_str, req_space + 1); to allow space for the null terminator.
Your code has a some inefficient logic , compare with this fixed version:
bool append(char **p_orig_str, const char *append_str)
{
// no action required if appending an empty string
if ( append_str[0] == 0 )
return true;
size_t orig_len = strlen(*p_orig_str);
size_t req_space = orig_len + strlen(append_str) + 1;
char *new_str = realloc(*p_orig_str, req_space);
// resize success..
if(new_str == NULL)
{
fprintf(stderr, "Couldn't reallocate memory\n");
return false;
}
*p_orig_str = new_str;
strcpy(new_str + orig_len, append_str);
return true;
}
This logic doesn't make any sense:
// is there enough space to append our data?
int req_space = strlen(orig_str) + strlen(append_str);
if (req_space > strlen(orig_str)) {
As long as append_str has non-zero length, you're always going to have to re-allocate.
The main problem is that you're trying to track the size of your buffers with strlen. If your string is NUL-terminated (as it should be), your perceived buffer size is always going to be the exact length of the data in it, ignoring any extra.
If you want to work with buffers like this, you need to track the size in a separate size_t, or keep some sort of descriptor like this:
struct buffer {
void *buf;
size_t alloc_size;
size_t used_amt; /* Omit if strings are NUL-terminated */
}
The Labwindows documentation says the following about the ServerTCPRead Method:
int ServerTCPRead (unsigned int conversationHandle, void *dataBuffer, size_t dataSize, unsigned int timeOut);
dataBuffer void * The pointer to the buffer in which to store the data.
However, when I actually declare a pointer and pass it to the method, it returns with the following error:
FATAL RUN-TIME ERROR: "test1.c", line 146, col 61, thread id 0x00001474: Array argument too small (1 bytes). Argument must contain at least 60000 bytes (60000 elements).
Here is the code I am using:
char * receiveBuf="";
ssize_t dataSize = 60000;
switch (event)
{
case TCP_CONNECT:
break;
case TCP_DATAREADY:
if ((dataSize = ServerTCPRead (g_hconversation, receiveBuf,
dataSize, 1000))
< 0)
{
//SetCtrlVal (g_hmainPanel, MAINPNL_RECEIVE, "Receive Error\n");
}
else
{
display_value_from_client(receiveBuf);
}
break;
You have allocated receiveBuf as an empty string, so there is no space to store the data you would receive in the function call. I would replace
char * receiveBuf="";
ssize_t dataSize = 60000;
with
ssize_t dataSize = 60000;
char * receiveBuf = malloc(dataSize);
and don't forget to call free later as needed.
Also based on the error this call may block until it receives dataSize bytes. You should check the documentation for that.