VarSetCapacity variable reading error Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
taojiaheng
Posts: 3
Joined: 22 Dec 2020, 03:23

VarSetCapacity variable reading error

Post by taojiaheng » 22 Dec 2020, 03:47

i ma code not working :? :? :? :?

now have two functions. a function and b function.

a function effect is reading png file data to a VarSetCapacity variable, and return .

b function effect is reception a function result, but There is a problem here. b function reading a function result don't agreement!!!!

Here is the main code!!!!


function a

Code: Select all

screenshot(pos_x, pos_y, w, h, ByRef  len)
{
	; 截图
	;pos := pos_x "|" pos_y "|" w "|" h
	;pToken := Gdip_Startup()
	;snap := Gdip_BitmapFromScreen(pos)
	;Gdip_SaveBitmapToFile(snap, "temp.png")
	;Gdip_DisposeImage(snap)
	;读取文件 
	file := FileOpen("temp.png", "r")
	VarSetCapacity(content, 102400)
	len := file.RawRead(content, 102400)
	return content
}

function b

Code: Select all

F1::
    len :=  0
	res := screenshot("50", "50", "1000", "1000", len)
	index := 0
	Loop,  5 {
		num := NumGet(res, index, "UChar")
		MsgBox, %num% - %index%
		index := index + 1
	}
	;httpPost("http 10.1.3.63:8080 ", "/", res.data, res.len)
	return

function b: variable res is inconsistent with A


all codes

Code: Select all

#Include ./AHKv2-Gdip/Gdip_All.ahk

memcpy()
{

}

httpGet(host, path)
{
	; 参数1   char * host
	VarSetCapacity(host_ptr, StrLen(host) *  2)
	StrPut(host, &host_ptr)
	; 参数2   char * path
	VarSetCapacity(path_ptr, StrLen(path) *  2)
	StrPut(path, &path_ptr)
	; 参数4   int buff_len
	buff_len := 10240
	; 参数3   char * resBuff
	VarSetCapacity(res_buff, buff_len)
	; 调用dll
	res := DllCall("httpClient.dll\post", "Str", host_ptr, "Str", path_ptr, "Str", res_buff, "Int", buff_len)
	MsgBox, http接口返回的字节数 =  %res% `n 返回内容 `n %res_buff%
	return res_buff
}

httpPost(host, path, ByRef  send_content, send_content_len)
{

	; 参数1   char * host
	VarSetCapacity(host_ptr, StrLen(host) *  2)
	StrPut(host, &host_ptr)
	; 参数2   char * path
	VarSetCapacity(path_ptr, StrLen(path) *  2)
	StrPut(path, &path_ptr)
	; 参数4   int buff_len
	buff_len := 10240
	; 参数3   char * resBuff
	VarSetCapacity(res_buff, buff_len)
	;参数5   char * buff
	VarSetCapacity(buff_ptr, send_content_len)
	; 内存拷贝
	index = 0
	Loop,  30 {
		num := NumGet(&send_content, index, "UChar")
		;MsgBox, %num% - %index%
	   NumPut(num, buff_ptr, index, "UChar")
		index := index + 1
	}
	
	
	;参数6  int send_len
	send_len := send_content_len
	; 调用dll
	res := DllCall("httpClient.dll\post", "Str", host_ptr, "Str", path_ptr, "Str", res_buff, "Int", buff_len, "Str", send_content, "Int", send_len)
	MsgBox, http接口返回的字节数 =  %res% `n 返回内容 `n %res_buff%
	return  res_buff
}

screenshot(pos_x, pos_y, w, h, ByRef  len)
{
	; 截图
	;pos := pos_x "|" pos_y "|" w "|" h
	;pToken := Gdip_Startup()
	;snap := Gdip_BitmapFromScreen(pos)
	;Gdip_SaveBitmapToFile(snap, "temp.png")
	;Gdip_DisposeImage(snap)
	;读取文件 
	file := FileOpen("temp.png", "r")
	VarSetCapacity(content, 102400)
	len := file.RawRead(content, 102400)
	return content
}

F1::
    len :=  0
	res := screenshot("50", "50", "1000", "1000", len)
	index := 0
	Loop,  5 {
		num := NumGet(res, index, "UChar")
		MsgBox, %num% - %index%
		index := index + 1
	}
	;httpPost("http 10.1.3.63:8080 ",  Broken Link for safety "/", res.data, res.len)
	return

t(){
	s := "tttt213213"
	return s
}

F2::
	str := t()
	len := StrLen(str)
	MsgBox, %len%



taojiaheng
Posts: 3
Joined: 22 Dec 2020, 03:23

Re: VarSetCapacity variable reading error

Post by taojiaheng » 22 Dec 2020, 03:52

please . The ball ball you !!!

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: VarSetCapacity variable reading error  Topic is solved

Post by swagfag » 22 Dec 2020, 04:02

because u cannot return VarSetCapacity allocated variables from functions(except by assigning back to a ByRef function parameter)
if u want to return using the return keyword, ull have to allocate memory on the heap using dllcall GlobalAlloc or HeapAlloc or any other memory management function available(and not forget to clean up when u no longer need the memory)

v2 doesnt have that issue, u can simply return BufferAllocated objects.

taojiaheng
Posts: 3
Joined: 22 Dec 2020, 03:23

Re: VarSetCapacity variable reading error

Post by taojiaheng » 22 Dec 2020, 04:16

swagfag wrote:
22 Dec 2020, 04:02
because u cannot return VarSetCapacity allocated variables from functions(except by assigning back to a ByRef function parameter)
if u want to return using the return keyword, ull have to allocate memory on the heap using dllcall GlobalAlloc or HeapAlloc or any other memory management function available(and not forget to clean up when u no longer need the memory)

v2 doesnt have that issue, u can simply return BufferAllocated objects.
thank your!!!!!
i resolved the question

Wish you a happy life!The whole family happiness

Post Reply

Return to “Ask for Help (v1)”