Converting Ptr

General FreeBASIC programming questions.
Dinosaur
Posts: 1300
Joined: Jul 24, 2005 1:13
Location: Searcy AR USA
Contact:

Converting Ptr

Postby Dinosaur » Jun 30, 2011 23:57

Hi all

As a user of the CGUI library, most of the declares for it are CDECL.
And on many of the calls to these routines there is a spare data field.
For example

Code: Select all

AddButton( CGUI_DOWNLEFT, "#Recipes", @Main_Window1, NULL)
The Null entry allows me to put a Cdecl Ptr to data that I can use. So when the operator presses this button, the system goes to a Routine called Main_Window1, which is declared as follows.

Code: Select all

Declare sub Main_window1 Cdecl (Byval userdata As Any Ptr)
But now I want to put a Ptr to a UDT called MStatus in the Null field as per

Code: Select all

AddButton( CGUI_DOWNLEFT OR CGUI_EqualWidth, "#Recipes", @Main_Window1, @MStatus)
But I am having difficulty with converting this Ptr to be used within the sub.This compiles but causes an exception on the print statement.

Code: Select all

Sub Main_window1 Cdecl (Byval userdata As Any Ptr)
    Dim MS as MStat Ptr
    MS = userdata
    With *MS
        ?.FileName
My udt is declared as "Dim Shared MStatus AS MStat Ptr"
Regards

EDIT: "With *CAST(MStat PTR, userdata)" causes the same error.
D.J.Peters
Posts: 7835
Joined: May 28, 2005 3:28

Postby D.J.Peters » Jul 01, 2011 5:29

You construct an pointer on pointer !

Dim Shared MStatus AS MStat Ptr

MStatus is an pointer

and @MStatus creates MStatus ptr ptr

Code: Select all

sub test(userarg as any ptr)
  dim as MStat ptr arg = cptr(MStat ptr, userarg)
  ' now you can use it
end sub
Dim Shared AS MStat Ptr MStatus
Test(MStatus) ' !!! without an extra @MStatus
if you use it as param why you made it as global ?

Joshy
Dinosaur
Posts: 1300
Joined: Jul 24, 2005 1:13
Location: Searcy AR USA
Contact:

Postby Dinosaur » Jul 01, 2011 5:40

Hi all

Joshy, I realised my typing error after you pointed it out.
It was only an error in my example, not with the actual tests.

My problem still stands though, how to convert a "Cdecl (Byval userdata as Any Ptr)" to one that I can use for my udt. "(Byval userdata as MStat Ptr)"

REgards
D.J.Peters
Posts: 7835
Joined: May 28, 2005 3:28

Postby D.J.Peters » Jul 01, 2011 5:42

change this

AddButton( CGUI_DOWNLEFT OR CGUI_EqualWidth, "#Recipes", @Main_Window1, @MStatus)

to this

AddButton( CGUI_DOWNLEFT OR CGUI_EqualWidth, "#Recipes", @Main_Window1, MStatus)

thats all my friend :-)

Joshy
Dinosaur
Posts: 1300
Joined: Jul 24, 2005 1:13
Location: Searcy AR USA
Contact:

Postby Dinosaur » Jul 01, 2011 5:50

HI all

Joshy, that wont work, as the CGUI C lib pops up an error
Passing different pointer types, at parameter 4 of ADDBUTTON
The Addbutton Function is actually declared as

Code: Select all

declare function AddButton cdecl alias "AddButton" (byval x as integer, byval y as integer, byval label as zstring ptr, byval CallBack as sub cdecl(byval as any ptr), byval userdata as Any ptr) as integer
Removing the Cdecl from there creates more errors.
Regards

Sorry, I thought you just removed the Cdecl from the declare statement.So, I guess the question is, how to convert an "Any" pointer to a udt pointer.
if you use it as param why you made it as global ?
At the moment the Ptr has to be visible through the complete module. I am converting the project slowly to remedie that.
MOD
Posts: 555
Joined: Jun 11, 2009 20:15

Postby MOD » Jul 01, 2011 8:38

What about casting it?
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » Jul 01, 2011 8:46

EDIT: nvm, I misread. It shouldn't complain to pass any ptr type to any ptr.
Dinosaur
Posts: 1300
Joined: Jul 24, 2005 1:13
Location: Searcy AR USA
Contact:

Postby Dinosaur » Jul 01, 2011 23:24

Hi all

MOD, I did try casting with the same result.

Hopefully by saying less, this problem becomes clearer.

Code: Select all

'First I draw a button and add my udt Pointer (@MStatus)
Button.Run   = AddButton( 2,268 OR CGUI_EqualWidth, "#Run", @RunMode, @MStatus)

'Now someone presses this button, And it goes here
Sub RunMode  Cdecl (Byval userdata As Any Ptr)
    'here I try To convert the Ptr

    With *CAST(MStat PTR, userdata)
        ?.FileName      'EXCEPTION_ACCESS_VIOLATION
    End With

    'Or
       
    Dim MSPtr as MStat Ptr
    MSPtr = userdata
   
    With *MSPtr
        ?.FileName      'EXCEPTION_ACCESS_VIOLATION
    End With
End Sub
Regards
EDIT: Doesn't matter where and how I remove / place CDECL, it always results in warning 3(1): Passing different pointer types.
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Postby MichaelW » Jul 02, 2011 3:48

There should be no problem converting the pointers, and assuming that you have allocated a UDT variable somewhere and initialized the pointer, there should be no access violation.

Code: Select all

type MStat
    a as integer
    b as integer
end type

sub Main_window1 Cdecl (Byval userdata As Any Ptr)
    Dim MS as MStat Ptr
    MS = userdata
    print MS->a, MS->b
    print cast(MStat Ptr,userdata)->a, cast(MStat Ptr,userdata)->b
end sub

dim MStatus as MStat
dim p as MStat ptr = @MStatus

MStatus.a = 1
MStatus.b = 2
Main_window1( p )
Main_window1( @MStatus )
sleep

Dinosaur
Posts: 1300
Joined: Jul 24, 2005 1:13
Location: Searcy AR USA
Contact:

Postby Dinosaur » Jul 02, 2011 6:37

Hi all

Michael after trying your example, I realised that I passed a Pointer to a Pointer to the CGUI routine.
Keeping track of what is a pointer and what is not, is becoming a headache.
I will have to modify all my names so that a pointer name always ends in "Ptr"

Many thanks for the help.

REgards
D.J.Peters
Posts: 7835
Joined: May 28, 2005 3:28

Postby D.J.Peters » Jul 02, 2011 11:18

Dinosaur wrote:I realised that I passed a Pointer to a Pointer to the CGUI routine.
D.J.Peters wrote:You construct an pointer on pointer !
That was my first sentence I wrote.

how ever you got it now :-)

Joshy
Dinosaur
Posts: 1300
Joined: Jul 24, 2005 1:13
Location: Searcy AR USA
Contact:

Postby Dinosaur » Jul 02, 2011 22:56

Hi all

Joshy, did you just throw an "I told you so" at me.? :)

I have used the word MStatus (Master Status) in my code for nearly 30 years, ever since I started building machines. It's very hard to all of a sudden accept it as meaning something different.

Did I just throw "an excuse " at you :)

Regards

Return to “General”

Who is online

Users browsing this forum: No registered users and 1 guest