Sign in to follow this  
Followers 0
bittware

Pointer content is destroied after calling?

5 posts in this topic

#1 ·  Posted (edited)

Hello experts,

I came across a weird situation.

$data_byte_struct = DllStructCreate("int data_byte")
    $data_byte_ptr = DllStructGetPtr($data_byte_struct, 1)
    ConsoleWrite("$data_byte_ptr->" &$data_byte_ptr& @CRLF)
    $re_stat = DllCall($hDll, "int:cdecl", "sub_spi_config", "ptr", $hSubHandle, "int", 0, "int*", $data_byte_ptr);just get SPI config
    $re_stat_value = $re_stat[3]
    ConsoleWrite("SPI Config READ Content from Return Array->" & $re_stat_value & @CRLF)
    ConsoleWrite("SPI Config READ Content from Struct->" &"0x"&Hex(DllStructGetData($data_byte_struct,1), 4)& @CRLF)

I use two different methods to watch data_byte_ptr pointer content. They should be with identical result. But why does the first print out the right content which is assigned within DLL function, whereas the second always print out 0x0000?

Could anyone figure that out? Thanks.

BRs,

bittware

Edited by bittware

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

This is really simple.

When using int*, AutoIt already creates an "internal int-structure ", you must not create it on your own.

This is the result of your code:

-> pointer to integer (internal)                   | <- function gets this pointer
   -> integer (holds pointer to your struct)       | ... and sets the integer here
      -> integer (in your struct)                  | ... this int is never reached

PS: assigning a pointer to an int does not work in x64-mode. (Pointer is 64bit wide, int onyl 32bit). In 32bit, both types have the same length of 32bits.

Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites

This is really simple.

When using int*, AutoIt already creates an "internal int-structure ", you must not create it on your own.

This is the result of your code:

-> pointer to integer (internal)                   | <- function gets this pointer
   -> integer (holds pointer to your struct)       | ... and sets the integer here
      -> integer (in your struct)                  | ... this int is never reached

PS: assigning a pointer to an int does not work in x64-mode. (Pointer is 64bit wide, int onyl 32bit). In 32bit, both types have the same length of 32bits.

I am not sure 100% get your points.

Do you mean function just changes the integer(the struct pointer) inside function (internally)?

Once function returns, integer in the struct, which is outside function, is still kept unchanged?

Share this post


Link to post
Share on other sites

You have two threads about the same thing. Cool.


♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites

I understood. Thank you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0