View Full Version : Writing a C Function
Viper_Viper
August 7th, 2006, 19:32
Ok I can write some simple C functions, but I am having a lot of difficulty converting this program into a C function. Its located here (http://members.cox.net/viperviper/getusername.c)but I will include it for reference. All I want it to do is return your current username. This program currently compiles and runs fine, I just want it to be a function.
#include <stdio.h>
#include <windows.h>
int main()
{
char buffer[20];
DWORD ret = GetEnvironmentVariable("USERNAME", buffer, sizeof(buffer));
if (!ret || ret == ERROR_ENVVAR_NOT_FOUND)
{
printf("Failed to retrieve username!\n");
return 1;
}
if (ret > sizeof(buffer)) //function always returns length of environment variable. if it's longer than our buffer, we need a bigger buffer
{
printf("The buffer thingy needs to be longer!\n");
char buffer[ret];
GetEnvironmentVariable("USERNAME", buffer, ret); //note: there is no error checking on this second call; perhaps there should be
}
printf("Current username is: %s\n", buffer);
return 0;
}
Proto
August 7th, 2006, 19:53
Well since I am on a Linux box I can't really test it, but I don't see where the problem is
#include <stdio.h>
#include <windows.h>
int myFunction()
{
char buffer[20];
DWORD ret = GetEnvironmentVariable("USERNAME", buffer, sizeof(buffer));
if (!ret || ret == ERROR_ENVVAR_NOT_FOUND)
{
printf("Failed to retrieve username!\n");
return 1;
}
if (ret > sizeof(buffer)) //function always returns length of environment variable. if it's longer than our buffer, we need a bigger buffer
{
printf("The buffer thingy needs to be longer!\n");
char buffer[ret];
GetEnvironmentVariable("USERNAME", buffer, ret); //note: there is no error checking on this second call; perhaps there should be //just catch the value of ret and check it... i guess :p
}
printf("Current username is: %s\n", buffer);
return 0;
}
int main()
{
return myFunction();
}
Viper_Viper
August 7th, 2006, 20:50
Well, I wanted it to return a char[] so I can use it in a program. Working with strings is very confusing to me.
Proto
August 7th, 2006, 22:08
#include <stdio.h>
#include <windows.h>
char* buffer;
char* myFunction()
{
buffer = malloc(sizeof(char)*20);
DWORD ret = GetEnvironmentVariable("USERNAME", buffer, sizeof(buffer));
if (!ret || ret == ERROR_ENVVAR_NOT_FOUND)
{
printf("Failed to retrieve username!\n");
return 1;
}
if (ret > sizeof(buffer)) //function always returns length of environment variable. if it's longer than our buffer, we need a bigger buffer
{
printf("The buffer thingy needs to be longer!\n");
buffer = malloc(sizeof(char)*ret);
GetEnvironmentVariable("USERNAME", buffer, ret); //note: there is no error checking on this second call; perhaps there should be //just catch the value of ret and check it... i guess :p
}
printf("Current username is: %s\n", buffer);
return buffer;
}
int main()
{
char *myString = myFunction();
return 0;
}
Viper_Viper
August 8th, 2006, 07:01
Sweet! Thanks man, it worked great! Man working with C seems way more difficult than working with JAVA, from someone who has little experence with both.
EDIT: Whow! buffer = malloc(sizeof(char)*(INT));That is SOOOO helpful! Now I dont have to make buffers for everything! LIFESAVER!
Ramsus K
August 12th, 2006, 21:03
Sweet! Thanks man, it worked great! Man working with C seems way more difficult than working with JAVA, from someone who has little experence with both.
EDIT: Whow! buffer = malloc(sizeof(char)*(INT));That is SOOOO helpful! Now I dont have to make buffers for everything! LIFESAVER!
Don't use it carelessly though. C isn't garbage collected like Java, so when you malloc, it sticks around until your program ends, even if you no longer have a pointer to the allocated memory. Write a couple of careless loops, and it's a good way to crash your program, and maybe even the machine depending on the OS. You should always keep a pointer to the memory you malloc as long as you're using it until you call free on it.
You can find lots of tutorials on dynamic memory allocation online. I suggest checking one out. Also, you might want to read up on pointers if you're going to use malloc.
http://www.cs.cf.ac.uk/Dave/C/node10.html
http://www.cs.cf.ac.uk/Dave/C/node11.html (yes, the spacing is screwed up)
http://vergil.chemistry.gatech.edu/resources/programming/c-tutorial/pointers.html
http://vergil.chemistry.gatech.edu/resources/programming/c-tutorial/dynamic.html
A good resource to help clear up problems with understanding these concepts is the comp.lang.c usenet FAQ:
http://c-faq.com/
Proto
August 12th, 2006, 23:01
oh yeah, I always forget about freeing memory in C (I always properly write my destructor in C++ though :D), in this case you should do a free(buffer) before the second malloc allocation, and when you finish using the buffer.
ChankastRules
December 6th, 2006, 00:55
Would someone kill these spammers? they are everywhere these days.
Viper_Viper
December 6th, 2006, 05:44
how dare these spammers deface my really old thread.
vBulletin® v3.8.7, Copyright ©2000-2013, vBulletin Solutions, Inc.