You are not logged in.

  • "StefanSchnell" is male
  • "StefanSchnell" started this thread

Posts: 102

Date of registration: Mar 13th 2009

Location: Oberirsen - Germany

Occupation: Senior Software Engineer

Hobbies: Programming aund Photography

  • Send private message

1

Sunday, May 10th 2009, 3:37pm

How to use pointer with Jabaco

Hello community,
I programmed a small library to use pointers with jabaco, and a class module to use it. Now a small example to use it:

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
Private WinAPI Sub RtlMoveMemory Lib "kernel32.dll" _
  (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)

Public Sub CopyMemory(ByVal Source As Long, ByVal Destination As Long, _
  ByVal Length As Long)
  RtlMoveMemory Destination, Source, Length
End Sub

Private WinAPI Function MessageBox Lib "user32.dll" _
  Alias "MessageBoxA" (ByVal hwnd As Long, ByVal lpText As Long, _
  ByVal lpCaption As Long, ByVal wType As Long) As Long

Public Sub Command1_Click()
   
'-Begin-----------------------------------------------------------------

  Dim VarPtr As New VarPtr()

  VarPtr.Init
   
  '-An example with byte variable and WinAPI CopyMemory-----------------
    Dim a1, a2 As Long

    a1 = VarPtr.CreateVar("Test1", "Byte", 0)
    java#lang#System.out.println "Address a1: " & Str(a1)
   
    VarPtr.SetByteVar "Test1", 42
    java#lang#System.out.println "Variable Test1: " & _
      Str(VarPtr.GetByteVar("Test1"))
   
    a2 = VarPtr.CreateVar("Test2", "Byte", 0)
    java#lang#System.out.println "Address a1: " & Str(a2)
    java#lang#System.out.println "Variable Test2: " & _
      Str(VarPtr.GetByteVar("Test2"))
    CopyMemory a1, a2, 1
    java#lang#System.out.println "Variable Test2 nach CopyMemory: " & _
      Str(VarPtr.GetByteVar("Test2"))
    
  '-An example with string variable and WinAPI MessageBox----------------
    Dim a3, a4 As Long
    
    a3 = VarPtr.CreateVar("strTest", "String", 32)
    java#lang#System.out.println "Address a3: " & Str(a3)
    
    VarPtr.SetStringVar "strTest", "Dies ist ein Test"
    
    MessageBox 0, a3, 0, 0
    
    java#lang#System.out.println VarPtr.GetStringVar("strTest")
    java#lang#System.out.println VarPtr.GetVarType("Test2")
    java#lang#System.out.println VarPtr.GetVarType("strTest")
    VarPtr.DestroyVar "Test1"
    VarPtr.DestroyVar "Test2"
    VarPtr.DestroyVar "strTest"
   
  VarPtr.UnInit
'-End-------------------------------------------------------------------
End Sub


Use the methods Init at the the start and UnInit at the end. To create a variable use CreateVar, the result of this function is the pointer to it. To destroy a variable use DestroyVar. You can create the types Byte, Integer, Long, Single, Double, String and Array, for a memory block with arbitrary size. The get and set functions of the types Single, Double and String use the clipboard for interprocess communication.

The library is programmed in PureBasic language, and has the following code - I think it is easy to unterstand how it works, if you look at the commented source.

I plan to use this library for further experiments with COM and Jabaco.

Cheers
Stefan
Visit my personal or commercial site
If you have questions or suggestions, write me an eMail or
meet me here

This post has been edited 3 times, last edit by "StefanSchnell" (May 24th 2009, 9:07am)


A1880

Intermediate

  • "A1880" is male

Posts: 500

Date of registration: Jan 1st 2009

Location: Hanover, Germany

Occupation: Software Engineer

Hobbies: Hilbert Curves

  • Send private message

2

Sunday, May 10th 2009, 8:43pm

Cool!

Hi Stefan,
that looks cool indeed! :thumbup:

Your "variables" are a clever way to circumvent Java's restrictions w.r.t. memory-fixed variables.
Could they even serve as shared memory between processes? Or is such a variable bound to the address space of a single DLL
which cannot be accessed by other processes, because they create their own instance of the DLL?

In my own Winapi experiments I have encountered some difficulties with the NativeCall mechanism.
It appears to me that temporary files are generated for every NativeCall call and not properly cleaned away after the call.

Thanks and happy experimenting!

A1880

This post has been edited 1 times, last edit by "A1880" (Jun 4th 2009, 11:17am)


  • "StefanSchnell" is male
  • "StefanSchnell" started this thread

Posts: 102

Date of registration: Mar 13th 2009

Location: Oberirsen - Germany

Occupation: Senior Software Engineer

Hobbies: Programming aund Photography

  • Send private message

3

Monday, May 11th 2009, 11:41pm

Hello A1880,
thanks. :D

If the DLL is loaded, it works only in one address space. If you load it again, e.g. with another program, you get another instance of the DLL in another address space and the different instances do not communicate. If you want to do this, try FastMM or take a look at FastShareMem, both are further developments of good old Delphis Sharemem. Maybe now is it possible to use it direct with Jabaco and this library.

I do not notice your observation with the temporary files, but I have look at it for the future - thanks for the hint.

Cheers
Stefan
Visit my personal or commercial site
If you have questions or suggestions, write me an eMail or
meet me here

This post has been edited 1 times, last edit by "StefanSchnell" (May 11th 2009, 11:54pm)


  • "StefanSchnell" is male
  • "StefanSchnell" started this thread

Posts: 102

Date of registration: Mar 13th 2009

Location: Oberirsen - Germany

Occupation: Senior Software Engineer

Hobbies: Programming aund Photography

  • Send private message

4

Sunday, May 24th 2009, 9:15am

Extended version of VarPtr

Hello community,
here is an extended version of VarPtr library. New are functions to set and get variables in an array, to create user defined (UD) types and a little help files with short explanations of the functions.
Cheers
Stefan
Visit my personal or commercial site
If you have questions or suggestions, write me an eMail or
meet me here

This post has been edited 1 times, last edit by "StefanSchnell" (Aug 2nd 2009, 7:49am)


  • "StefanSchnell" is male
  • "StefanSchnell" started this thread

Posts: 102

Date of registration: Mar 13th 2009

Location: Oberirsen - Germany

Occupation: Senior Software Engineer

Hobbies: Programming aund Photography

  • Send private message

5

Monday, June 1st 2009, 10:42am

Pointer for Java

Hello community,

I programmed a Java class for VarPtr. Now it is possible for you to use pointers in Jabaco via F1 and a JAR file. Only the names of a few methods are different, because Jabaco and Java use different type names, e.g. Short vs. Integer, Integer vs. Long and Float vs. Single.
All you need, you find here.

Cheers
Stefan
Visit my personal or commercial site
If you have questions or suggestions, write me an eMail or
meet me here

OlimilO

Intermediate

  • "OlimilO" is male

Posts: 277

Date of registration: Jan 18th 2009

Location: Germany

Occupation: software engineer

  • Send private message

6

Thursday, June 4th 2009, 10:37am

Hello Stefan,



Quoted

Only the names of a few methods are different, because Jabaco and Java use different type names, e.g. Short vs. Integer, Integer vs. Long and Float vs. Single


no not really...



Java Primitive Data Types



Jabaco Data Types and Operators



afair the ranges and names of primitive data types in Jabaco and in Java are quite similar.

but a bit different to VB



cheers

OlimilO

  • "StefanSchnell" is male
  • "StefanSchnell" started this thread

Posts: 102

Date of registration: Mar 13th 2009

Location: Oberirsen - Germany

Occupation: Senior Software Engineer

Hobbies: Programming aund Photography

  • Send private message

7

Sunday, August 2nd 2009, 7:48am

New advanced version of VarPtr available

Hello community,
you can download a new advanced version of VarPtr here or from the attachment.
Cheers
Stefan
Visit my personal or commercial site
If you have questions or suggestions, write me an eMail or
meet me here

Faldegast

Beginner

Posts: 25

Date of registration: Oct 6th 2009

  • Send private message

8

Tuesday, October 6th 2009, 5:19pm

How do you allocate Variable memory?
Java variables may be moved while accessing them (why all access has to go trough the GC). So im guessing you do some API calls to allocate static memory outside of what the GC manages?

axtens

Trainee

  • "axtens" is male

Posts: 37

Date of registration: Mar 16th 2009

Location: Perth, WA, Australia

Occupation: Software/Test Engineer

Hobbies: be a husband and a dad, play ukulele, sing

  • Send private message

9

Tuesday, October 13th 2009, 4:05am

G'day Stefan
Hello community,
you can download a new advanced version of VarPtr here or from the attachment.
Cheers
Stefan
I've been playing with it, but am confused about something: most of your demos are using the *A form of Win32 API calls. Can you show me one which passes a string into a *W function, like GetFileAttributesW. I want to use the *W form because I'm dealing with filenames in Chinese.

Thanks in advance,
Bruce.

  • "StefanSchnell" is male
  • "StefanSchnell" started this thread

Posts: 102

Date of registration: Mar 13th 2009

Location: Oberirsen - Germany

Occupation: Senior Software Engineer

Hobbies: Programming aund Photography

  • Send private message

10

Friday, October 16th 2009, 3:52pm

VarPtr with UniCode

Hi Bruce,
try this:

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
Private WinAPI Function MessageBox Lib "user32.dll" _
  Alias "MessageBoxW" (ByVal hwnd As Long, ByVal lpText As Long, _
  ByVal lpCaption As Long, ByVal wType As Long) As Long
  
Private WinAPI Function FromUniCode Lib "kernel32.dll" _
  Alias "WideCharToMultiByte" (ByVal CodePage As Long, ByVal dwFlags As Long, _
  ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, _
  ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, _
  ByVal lpDefaultChar As String, ByVal lpUsedDefaultChar As Long) As Long

Private WinAPI Function ToUniCode Lib "kernel32.dll" _
  Alias "MultiByteToWideChar" (ByVal CodePage As Long, ByVal dwFlags As Long, _
  ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, _
  ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long

Const CP_ACP As Long = 0

Public Sub Command1_Click()
'-Begin-----------------------------------------------------------------
  Dim VarPtr As New VarPtr()

  VarPtr.Init
   
  '-An example with string variable and WinAPI MessageBoxW--------------
    Dim strTest, lenTest As Long
    Dim Text As String
    
    Text = "I am MessageBoxW"
    
    strTest = VarPtr.CreateVar("strTest", "String", 32)
    VarPtr.SetStringVar "strTest", Text + Chr(0)
    
    strUniTest = VarPtr.CreateVar("strUniTest", "String", Len(Text) + 2)
    ToUniCode CP_ACP, 0, strTest, -1, strUniTest, Len(Text) + 2
    
    MessageBox 0, strUniTest, 0, 0
    
    VarPtr.DestroyVar "strTest"
    VarPtr.DestroyVar "strUniTest"
   
  VarPtr.UnInit
'-End-------------------------------------------------------------------
End Sub


Hope it helps.

Cheers
Stefan
Visit my personal or commercial site
If you have questions or suggestions, write me an eMail or
meet me here

  • "StefanSchnell" is male
  • "StefanSchnell" started this thread

Posts: 102

Date of registration: Mar 13th 2009

Location: Oberirsen - Germany

Occupation: Senior Software Engineer

Hobbies: Programming aund Photography

  • Send private message

11

Friday, October 16th 2009, 4:33pm

Hi Faldegast,

yes, you are right, with this native Windows library you create variables outside of Java and get their addresses in memory back. No garbage collector (GC) or something else.

Cheers
Stefan
Visit my personal or commercial site
If you have questions or suggestions, write me an eMail or
meet me here

  • "StefanSchnell" is male
  • "StefanSchnell" started this thread

Posts: 102

Date of registration: Mar 13th 2009

Location: Oberirsen - Germany

Occupation: Senior Software Engineer

Hobbies: Programming aund Photography

  • Send private message

12

Sunday, January 10th 2010, 8:46am

New advanced version of VarPtr available

Hello community,
you can download a new advanced version of VarPtr here or from the attachment.
It is now a pure Java library and no communication via clipboard is necessary. This is a version for 32-bit Windows.
Cheers
Stefan
Visit my personal or commercial site
If you have questions or suggestions, write me an eMail or
meet me here

This post has been edited 5 times, last edit by "StefanSchnell" (Mar 25th 2010, 12:38pm)


OlimilO

Intermediate

  • "OlimilO" is male

Posts: 277

Date of registration: Jan 18th 2009

Location: Germany

Occupation: software engineer

  • Send private message

13

Tuesday, February 16th 2010, 9:47pm

make P4J VB6 compatible

Hi Stefan,
many thanks for this cool dll I really love the idea. :)
There is a possibility to make it more easy to use and to make it more VB6-compatible.
In VB6 there are 3 hidden functions. In fact the module name is "VBA._HiddenModule".
The 3 functions are: ObjPtr, StrPtr, VarPtr
In VB6 it is common to use the StrPtr-function in this way:
(the following Code is valid VB6-Code)

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Option Explicit 
Private Declare Function MessageBox Lib "user32.dll" Alias "MessageBoxW" ( _ 
    ByVal hhwnd As Long, _ 
    ByVal lpText As Long, _ 
    ByVal lpCaption As Long, _ 
    ByVal wType As Long _ 
) As Long 
Public Sub Command1_Click() 

    Dim txt As String: txt = "this is the text. Quick brown fox jumps over the lazy dog" 
    Dim cap As String: cap = "this is the caption" 
    Dim pTxt As Long: pTxt = StrPtr(txt) 
    Dim pCap As Long: pCap = StrPtr(cap) 
    Dim mr As VbMsgBoxResult 

    mr = MessageBox(0, pTxt, pCap, vbOKCancel) 
    MessButton (mr) 

    mr = MessageBox(0, pTxt, pCap, vbYesNoCancel) 
    MessButton (mr) 

End Sub 
Public Sub MessButton(mr As VbMsgBoxResult) 
    Dim s As String: s = "You clicked: " 
    Select Case mr 
    Case vbOK: s = s & "OK" 
    Case vbYes: s = s & "Yes" 
    Case vbNo: s = s & "No" 
    Case vbCancel: s = s & "Cancel" 
    End Select 
    MsgBox s 
End Sub


We could do this in Jabaco very similar:

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Option Explicit 
Private Winapi Function MessageBox Lib "user32.dll" Alias "MessageBoxA" ( _ 
    ByVal hwnd As Long, _ 
    Byval lpText As Long, _ 
    ByVal lpCaption As Long, _ 
    ByVal wType As Long _ 
) As Long 
Public Sub Command1_Click() 

    Dim txt As String: txt = "this is the text it is a bit long here the quick brown fox jumps over the lazy dog" 
    Dim cap As String: cap = "this is the caption" 
    Dim pTxt As Long: pTxt = StrPtr(txt) 
    Dim pCap As Long: pCap = StrPtr(cap) 
    Dim mr As VBMsgBoxResult 

    mr = MessageBox(0, pTxt, pCap, vbOKCancel) 
    MessButton(mr) 

    mr = MessageBox(0, pTxt, pCap, vbOKCancel) 
    MessButton(mr) 

    MVarPtr.ClearVarPtr 

End Sub 
Public Sub MessButton(mr As VBMsgBoxResult) 
    Dim s As String = "You clicked: " 
    Select Case mr 
    Case vbOK:      s = s & "OK" 
    Case vbYes:     s = s & "Yes" 
    Case vbNo:      s = s & "No" 
    Case vbCancel: s = s & "Cancel" 
    End Select 
    MsgBox s 
End Sub

the module MVarPtr:

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Option Explicit 
Private myVarPtr As New VarPtr 
Private myVars As New Collection 
Public Sub InitVarPtr() 
    myVarPtr.Init 
End Sub 
Public Sub ClearVarPtr() 
    Dim s As String 
    For Each s In myVars 
        myVarPtr.ClearVar s 
    Next 
End Sub 
Public Function StrPtr(v As VBVariant) As Long 
    Dim name As String = v.hashCode 
    myVars.Add name 
    If v.isString Then 
        StrPtr = myVarPtr.CreateVar(name, "String", Len(v)) 
        myVarPtr.SetStringVar(name, v) 
    End If 
End Function

What about modifying the VarPtr-class in order to be able to use a VarPtr and a StrPtr function?
At the moment I am not sure if the name of the class could collide with the VarPtr-functionname.
Would you agree with the idea to integrate the VarPtr.dll and class in the Jabaco-Framework?

many regards
OlimilO

This post has been edited 1 times, last edit by "OlimilO" (Mar 28th 2010, 12:16pm)


  • "StefanSchnell" is male
  • "StefanSchnell" started this thread

Posts: 102

Date of registration: Mar 13th 2009

Location: Oberirsen - Germany

Occupation: Senior Software Engineer

Hobbies: Programming aund Photography

  • Send private message

14

Sunday, March 7th 2010, 5:29pm

Hello OlimilO,
thanks for your suggestions. Sorry for my late answer, but I am a little bit in stress now. It is a good idea. I am looking forward to realize your idea, but before I will talk to Manuel about a technical solution and if it is okay for him, or have you already inform him? If you want we can realize it together. What do you think?
Cheers
Stefan
Visit my personal or commercial site
If you have questions or suggestions, write me an eMail or
meet me here

  • "StefanSchnell" is male
  • "StefanSchnell" started this thread

Posts: 102

Date of registration: Mar 13th 2009

Location: Oberirsen - Germany

Occupation: Senior Software Engineer

Hobbies: Programming aund Photography

  • Send private message

15

Thursday, March 25th 2010, 12:38pm

Hello OlimilO,
I change the class name in P4J. I hope now we have no stress with a potential collision with VarPtr. At the next step I will implement your code to use VarPtr and StrPtr in Jabaco code. You find the library here.
Cheers
Stefan
Visit my personal or commercial site
If you have questions or suggestions, write me an eMail or
meet me here

Faldegast

Beginner

Posts: 25

Date of registration: Oct 6th 2009

  • Send private message

16

Tuesday, May 4th 2010, 10:25pm

What about implementing Common WinAPI functions in Java using Java class libraries. This would allow greater VB6 compatibility while still providing pure Java compatibility for those that wanna write applets and other apps that cant use native calls.

If we really are gonna use native calls, we should make use of libwine for non-Windwos platform. Remember that Jabaco is supposed to be platform independent.

Rate this thread
WoltLab Burning Board