API Commands

BlitzPlus Forums/BlitzPlus Beginners Area/API Commands

SebHoll(Posted 2005) [#1]
How Can I Use API Commands which Return a String Given Inside the Functions Parameters. E.g.

.lib "shell32.dll"
FindExecutable%(file$,directory$,return):"FindExecutableA"


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/findexecutable.asp

The value is apparently returned in the placeholder "return" in the function.


How can I read the data being output to the placeholder? - I think I have to use memory banks. Could someone please explain and possible write a sample script to read the output in blitz?

Thanks in advance

Seb


Snarty(Posted 2005) [#2]
Hi,

You should be fine supplying memory bank pointers. Return* for the above case. Play around with it.
.lib "shell32.dll"
FindExecutable%(file$,directory$,return*):"FindExecutableA"
Use something like this.
TextBuffer=CreateBank(256)

FindExecutable File$, Directory$, TextBuffer
For n=0 To BankSize(TextBuffer)-1
	ascii=PeekByte(TextBuffer, n)
	If ascii=0
		Exit
	Else
		Ret$=Ret$+Chr(ascii)
	EndIf
Next



SebHoll(Posted 2005) [#3]
Hi Snarty,
Thanks for getting back to me on that, I finally got it to work!!! One more thing though, what does "*" mean when you are declaring the function variables in the userlibs. Does it mean the variable contains a memory bank address?

Thanks

Seb

P.S. I've been trying to get the following function to work: ExtractAssociatedIcon().

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/resources/icons/iconreference/iconfunctions/extractassociatedicon.asp

.lib "shell32.dll"
ExtractAssociatedIcon%(hInst%,filename$,IconIndex%):"ExtractAssociatedIconA"

.lib "user32.dll"
DestroyIcon%(hIcon$):"DestroyIcon"
DrawIcon%(hdc%,x%,y%,hIcon%):"DrawIcon"
GetDC%(hwnd%):"GetDC"

.lib "kernel32.dll"
GetModuleHandle%(lpModuleName%):"GetModuleHandleA"


Global wndMain 		= CreateWindow("Remote File System - Client",100,100,400,300,0,5)
Global gadIconPanel = CreatePanel(220,185,32,32,wndMain)

Global temp$ = "C:\windows\Soap Bubbles.bmp"

Global icnPanelDC	= GetDC(QueryObject(gadIconPanel,1))
Global icnModHandle	= GetModuleHandle(0)
;GETS STUCK HERE
Global icnImage		= ExtractAssociatedIcon(icnModHandle,temp$,0)

DrawIcon(icnPanelDC,0,0,icnImage)


But no matter what variables I pass to it - it doesn't work and Blitzcc.exe crashes. Any ideas?

Thanks once again


Snarty(Posted 2005) [#4]
"*" Does indeed mean it is a pointer to a bank.

As for the the other API command. A BMP does not have an icon of it's own, instead it has an Associated icon. So, it will look for the exe associated with the .bmp extension return that (if there is one) and try to store the associated exe path in the "filename$" parameter.

Now, this is where it falls over. Blitz will crash because you are trying to use a string. I would suggest switching "filename$" to "filename*" and use a MAX_PATH size bank (512).

Also, a check that icnImage<>0 would be advisable here.

Here are a couple of functions I created to aid with null terminated string insertion and extraction.
Bank=CreateBank(5)

hResult=SetString(Bank, "Paul")
Print hResult
Print ExtractString(Bank)+"|"

hResult=SetString(Bank, "Cut")
Print hResult
Print ExtractString(Bank)+"|"

hResult=SetString(Bank, "Extra Long")
Print hResult
Print ExtractString(Bank)+"|"

WaitKey

Function ExtractString$(Bank)

	Local RetString$=""
	Local ascii=0

	For n=0 To BankSize(Bank)-1
		ascii=PeekByte(Bank, n)
		If ascii=0
			Exit
		Else
			RetString=RetString+Chr(ascii)
		EndIf
	Next
		
	Return RetString

End Function

Function SetString(Bank, Txt$)

	; Function returns 1 if fully successful, 0 if the string did not fit.

	If Bank=0 RuntimeError "Bank Handle invalid."
	Local BSize=BankSize(Bank)
	
	For n=1 To Len(Txt$)
		If n=>BSize
			PokeByte Bank, n-1, 0
			Return 0
		EndIf
		PokeByte Bank, n-1, Asc(Mid(Txt, n, 1))
	Next
	
	If n=>BSize
		PokeByte Bank, BSize-1, 0
		Return 0
	EndIf
	
	PokeByte Bank, n-1, 0
	Return 1

End Function



SebHoll(Posted 2005) [#5]
Hi Snarty,

Thanks alot for all the help with memory bank pointers and everything. I managed to get the code to work just as you said (by having the filestring as a memorybank and also the index). Those functions that you wrote are really useful when you start using the Windows API so thanks for posting them. Final question though...

Say if you create a memorybank with "256" bytes:
Print "Memory Bank Example"
memBank1 = CreateBank(256)
WaitKey()
End
If you run the program in debug-mode and stop the program at WaitKey(), it shows you what currently is in each of the variables. However there is a number assigned to variable "memBank1" (in my case 2640001). Is that the address (or memory pointer) of that bank in the System RAM?

Thanks for all your help

Seb


Snarty(Posted 2005) [#6]
I'm not 100% sure on this. But I'm guessing that number is a Handle to Blitz's internal Struct list for memory bank objects. You could probably read the section of memory with a hex editor and find the size of the bank, and the pointer to the memory it uses.


SebHoll(Posted 2005) [#7]
Thanks for all your help

Seb


Snarty(Posted 2005) [#8]
Any time :)