Pb With API and GetComputerName

BlitzMax Forums/BlitzMax Programming/Pb With API and GetComputerName

Kingo(Posted 2006) [#1]
Hello :)

I'm trying to use the API Windows Commands, but it works an half lol

I would like to test GetComputerName (kernel32.dll).

I wrote some code but I have only the 4 first letters of my computer name :( but I can't have the computer name complete.

Here's the code I've written :

'Code Test

Global GetComputerName(lpbuffer:Byte Ptr,nSize:Long Ptr)

Local taille:Long 
Local name:Byte

kernel32 = LoadLibraryA("kernel32.dll")

If kernel32
	GetComputerName = GetProcAddress(kernel32,"GetComputerNameA")
Else
	Notify("Failed")
EndIf

taille = 100


GetComputerName(Varptr name,Varptr taille)

Print String.FromCString(Varptr name)


Any idea ?

Thx in advance


REDi(Posted 2006) [#2]
Extern "win32"
	Function GetComputerNameA(lpbuffer:Byte Ptr,nSize:Int Ptr)
EndExtern

Local Size = 32								' maximum length of cstring
Local Buffer:Byte Ptr = MemAlloc(Size)		' allocate memory to hold cstring
GetComputerNameA(buffer,Varptr(size))		' ask for name
Local name$ = String.FromCString(buffer)	' convert cstring to string
MemFree buffer 								' release cstring

Print name$
Print "computer name contains "+size+" characters"

Hope that helps :)


Kingo(Posted 2006) [#3]
thx :) just a question :

"Externe "Win32" => The GetComputerName command is a kernel32 lib command. so, Extern "Win32" has witch command ?

kernel32 lib and so are included ?


DStastny(Posted 2006) [#4]
I think you are confused to what "Win32" means. It tells BMAX what the calling convention of the function is. Windows C/C++ uses what is called a STDCALL or PASCAL calling convention. For BMAX this means "Win32"

In BMAX I believe the default is what is termed "C" Calling convention. This is if you put nothing after your function prototype. It might be REGISTER but I dont know since its not documented. REGISTER is very fast but requires more code for compiler to implment so most Languages dont implment it as it is only usefuly if you have enough registers to pass the paramaters without using the stack.

Basically a calling convention is how the paramaters are managed on the stack and who, the caller or the callee, is responsible for cleaning it up when the function returns.

If you use the incorrect calling conventions you will crash. Maybe not right away but you are estentially mucking up the applications stack memory.

Good rule of thumb if it is windows API or DLL function call add "Win32" very few calls are "C" convention. Best place to look is the windows header files. but it does require you understand C.

Hope this helps understand what the "Win32" means.

Doug Stastny


Kingo(Posted 2006) [#5]
Ok ;) thx for explaination.

But, an another thing I don't understand. When we want to use API, if we look at some api commands with for ex "api guide" we can see (this is an example) : commandname(thing as string, .....)

and when we try to do this in bmax we change the variable type (for ex a string in the function becomes a byte ptr in bmax...) I don't understand all this thing :(


klepto2(Posted 2006) [#6]
For strings you use $z to tell the function that it uses a cstring.
You use a byte ptr if the function want a type.

Take a look at this source:

http://www.blitzbasic.com/Community/posts.php?topic=54935