Function pointer

Windows specific questions.
deltarho[1859]
Posts: 1014
Joined: Jan 02, 2017 0:34
Location: UK

Function pointer

Postby deltarho[1859] » Feb 24, 2018 2:57

I have noticed that when DIM is used within an 'If/End If' construct, scope is lost on leaving the construct. My work around is to reassign, within the construct, another which has scope without the construct. Bit of a dog's dinner but it works.

I have 'Static As Any Ptr AES' without an 'If/End If' construct. It is Static because it is in a Windows callback function.

I have, pseudo code, within an 'If/End If' construct

Code: Select all

Dim LibTest As Function( parameters ) as long
LibTest = DyLibSymbol( library, <"Entry point>" )
AES = LibTest

However, AES( parameters ) without the construct generates

error 71: Array not dimensioned, before '(' in 'AES.....

I have tried a few 'variations on a theme' but to no avail.
sancho3
Posts: 209
Joined: Sep 30, 2017 3:22

Re: Function pointer

Postby sancho3 » Feb 24, 2018 4:01

AES is an 'any ptr'? Don't you therefore need to cast it to function:
Cast(function(parameters) as returntype, AES)(parameters)

Code: Select all

function moo(byval x as integer) as integer
   x += 12
   return x
end function

dim as function(byval x as integer) as integer test
dim as any ptr AES

test = @moo()
AES = test
'AES(9)
? cast(typeof(test), AES)(9)
? cast(function(byval x as integer) as integer, AES)(9)
sleep
fxm
Posts: 7733
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: Function pointer

Postby fxm » Feb 24, 2018 6:24

deltarho[1859] wrote:I have noticed that when DIM is used within an 'If/End If' construct, scope is lost on leaving the construct.

In -lang fb, a variable declared so ("Dim variable") inside an [If...End If] block is already a local variable at the block level (visible only inside the block, and cannot be accessed outside it). Therefore exiting the procedure (constructor) that contains this block only adds a second level of scoping.
deltarho[1859]
Posts: 1014
Joined: Jan 02, 2017 0:34
Location: UK

Re: Function pointer

Postby deltarho[1859] » Feb 24, 2018 6:58

@fxm Thanks. Understood.

@sancho3

You made life a little difficult for me by mixing two methods so I had to use the 'comment out' trick to see where it broke.

I found that test = @moo() was needed by both methods otherwise I got a GPF.

However, I do not have a function name, like moo, to get an address. All I have is LibTest which is a pointer to the <"Entry point">.

This is the actual code:

At the head of WndProc ( Callback function ) I have

Code: Select all

Static As Long dllIsLoaded
Static As Any Ptr AES

and here is the failing code

Code: Select all

Case EncAES
  If dllIsLoaded = 0 Then
    Dim As Any Ptr library = Dylibload( "EncDecStretchCNG.dll" )
    If library = 0  Then
      TDWrapper Hwnd, "Loading failure", "Failed To load EncDecStretchCNG.dll", TD_ERROR_ICON
      Exit Function
    End If
    Dim LibTest As Function( As Long, As Zstring, As Zstring, As Long ) As Long
    LibTest = Dylibsymbol( library, "EncDecStretchCNG" )
    If LibTest = 0  Then
      TDWrapper Hwnd, "Entry Point falure", "Could Not retrieve the entry Point of EncDecStretchCNG", TD_ERROR_ICON
      Exit Function
    End If
    dllIsLoaded = -1
    AES = LibTest
  End If
  pwszName = AfxIFileOpenDialog(hwnd, All_Files2) ' Get file to encrypt
  If pwszName Then
    zAESName = *pwszName
    CoTaskMemFree(pwszName)
    zPassKey = AfxInputBox( Hwnd, 0, 0, "AES", "Please provide password/passkey", "", , True ) ' Get password/passkey
    AES( AES256, zAESName, zPassKey, 0 )
  End If


The statement AES( AES256, zAESName, zPassKey, 0 ) is the one failing.
fxm
Posts: 7733
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: Function pointer

Postby fxm » Feb 24, 2018 8:40

Similarly, as wrote sancho3:

Code: Select all

    Cast(Function( As Long, As Zstring, As Zstring, As Long ) As Long, AES)( AES256, zAESName, zPassKey, 0 )
deltarho[1859]
Posts: 1014
Joined: Jan 02, 2017 0:34
Location: UK

Re: Function pointer

Postby deltarho[1859] » Feb 24, 2018 10:02

I did try that. I put it before AES( AES256, zAESName, zPassKey, 0 ) but I am still getting an error 71 as I mentioned earlier. I commented AES( AES256, zAESName, zPassKey, 0 ) and got a successful compilation but nothing was actually done. ie no encryption took place.

I inserted 'LibTest(AES256, "F:\FreeBASIC\RSA\AES\10MB.txt", "123456", 0)' just after 'AES = LibTest' to force the issue and encryption took place.

I then removed that and got zAESName and zPassKey printed out in the next section. They were correct.

So, Cast(Function( As Long, As Zstring, As Zstring, As Long ) As Long, AES)( AES256, zAESName, zPassKey, 0 ) is not working.

I reckon that AES = LibTest is the weak link.
fxm
Posts: 7733
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: Function pointer

Postby fxm » Feb 24, 2018 10:34

How are declared AfxIFileOpenDialog(...) and pwszName ?
How is declared zAESName ?
How are declared AfxInputBox(...) and zPassKey ?


Before line:

Code: Select all

Cast(Function( As Long, As Zstring, As Zstring, As Long ) As Long, AES)( AES256, zAESName, zPassKey, 0 )
insert:

Code: Select all

Print "'" & zAESName & "'", "'" & zPassKey & "'"
to better check the arguments values to be passed.


Instead of:

Code: Select all

Cast(Function( As Long, As Zstring, As Zstring, As Long ) As Long, AES)( AES256, zAESName, zPassKey, 0 )
you can also test:

Code: Select all

Cast(Function( As Long, As Zstring, As Zstring, As Long ) As Long, AES)( AES256, "F:\FreeBASIC\RSA\AES\10MB.txt", "123456", 0 )
deltarho[1859]
Posts: 1014
Joined: Jan 02, 2017 0:34
Location: UK

Re: Function pointer

Postby deltarho[1859] » Feb 24, 2018 10:56

How are declared AfxIFileOpenDialog(...) and pwszName ?
How is declared zAESName ?
How are declared AfxInputBox(...) and zPassKey ?

I have been using the Afx* functions for sometime without issue- they are from José Roca's WinFBX.

zAESName and zPasskey are being got correctly. I mentioned that in my last post - they were printed just before the Cast as you suggested.

I wrote some separate code specifically to test library loading before adding to my project and that was not confined to an If/End If block. I reassigned the symbol pointer just like AES = LibTest and got problems.

I am now certain that AES = LibTest is the issue. Yes, we are equating two 'As Any Ptr' but it seems to me that more is required.
dodicat
Posts: 4764
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Function pointer

Postby dodicat » Feb 24, 2018 11:28

Sometimes just using var ZZ = something, and let the compiler sort it out helps.
For instance we can see what AEZ is here.

Code: Select all

 
#include "windows.bi"
'Case EncAES
  'If dllIsLoaded = 0 Then
  function dummy() as long
  dim as any ptr hwnd
  #define  TD_ERROR_ICON 20
  #define AES256 0
  #define zpasskey "password"
  dim as long dllisloaded
    Dim As Any Ptr library = Dylibload( "EncDecStretchCNG.dll" )
    If library = 0  Then
      messagebox( Hwnd, "Loading failure", "Failed To load EncDecStretchCNG.dll", TD_ERROR_ICON)
      Exit Function
    End If
    Dim LibTest As Function( As Long, As Zstring, As Zstring, As Long ) As Long
    LibTest = Dylibsymbol( library, "EncDecStretchCNG" )
    If LibTest = 0  Then
      messagebox( Hwnd, "Entry Point falure", "Could Not retrieve the entry Point of EncDecStretchCNG", TD_ERROR_ICON)
      Exit Function
    End If
    dllIsLoaded = -1
var    AES = LibTest
#print typeof(AES)

' var pwszName = AfxIFileOpenDialog(hwnd, All_Files2) ' Get file to encrypt
  'If pwszName Then
   var zAESName = "HELLO"'*pwszName
    'CoTaskMemFree(pwszName)
   'var zPassKey = AfxInputBox( Hwnd, 0, 0, "AES", "Please provide password/passkey", "", , True ) ' Get password/passkey
    AES( AES256, zAESName, zPassKey, 0 )
   ' end if
   return 0
end function
dummy
  'End If

I obviously don't have the "EncDecStretchCNG.dll" file, but it looks promising.
deltarho[1859]
Posts: 1014
Joined: Jan 02, 2017 0:34
Location: UK

Re: Function pointer

Postby deltarho[1859] » Feb 24, 2018 11:45

@dodicat
I will have a look in a few minutes. Thanks

@fxm
Cast(Function( As Long, As Zstring, As Zstring, As Long ) As Long, AES)( AES256, "F:\FreeBASIC\RSA\AES\10MB.txt", "123456", 0 )

That was a very good idea. It worked.

I am using 'Dim As Zstring*260 zPassKey, zAESName' and the dll is' ByRef szInFile As Stringz * 260, ByRef szPassword As Stringz * 260'

At the moment everything looks fine, but clearly it is not.
deltarho[1859]
Posts: 1014
Joined: Jan 02, 2017 0:34
Location: UK

Re: Function pointer

Postby deltarho[1859] » Feb 24, 2018 12:44

I replaced zAESName in Cast with "F:\FreeBASIC\RSA\AES\10MB.txt" and left zPasskey and that worked.

Len(zAESName) = 37 but it should be 29. Len(zPasskey) = 6 which is correct.

I use *pwszName in 6 other places using sInFile = *pwszName,where sInFile is a string, in Open sInFile......... without problem.

So, there is something wrong with zAESName = *pwszName.
fxm
Posts: 7733
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: Function pointer

Postby fxm » Feb 24, 2018 12:55

How is declared pwszName ?
deltarho[1859]
Posts: 1014
Joined: Jan 02, 2017 0:34
Location: UK

Re: Function pointer

Postby deltarho[1859] » Feb 24, 2018 13:07

Dim pwszName As Wstring Ptr
deltarho[1859]
Posts: 1014
Joined: Jan 02, 2017 0:34
Location: UK

Re: Function pointer

Postby deltarho[1859] » Feb 24, 2018 13:15

@dodicat
Couldn't get your code to work. In fact, I have never been able to get #print to work ever. None of the examples in the Docs work. It had not occurred to me that WinFBE maybe at fault. Opened poseidonFB and your code worked. So do examples in the Docs.

I will let Paul Squires know.
fxm
Posts: 7733
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: Function pointer

Postby fxm » Feb 24, 2018 15:24

deltarho[1859] wrote:So, there is something wrong with zAESName = *pwszName.

Normally, no problem to assing a dereferenced WString pointer to a ZString variable.
AfxIFileOpenDialog(...) ?

Return to “Windows”

Who is online

Users browsing this forum: No registered users and 1 guest