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:
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
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
This post has been edited 3 times, last edit by "StefanSchnell" (May 24th 2009, 9:07am)
Cool!
Hi Stefan,
that looks cool indeed!
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
that looks cool indeed!
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)
Hello A1880,
thanks.
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
thanks.
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
This post has been edited 1 times, last edit by "StefanSchnell" (May 11th 2009, 11:54pm)
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
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
This post has been edited 1 times, last edit by "StefanSchnell" (Aug 2nd 2009, 7:49am)
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
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
Hello Stefan,
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
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
New advanced version of VarPtr available
Hello community,
you can download a new advanced version of VarPtr here or from the attachment.
Cheers
Stefan
you can download a new advanced version of VarPtr here or from the attachment.
Cheers
Stefan
G'day Stefan
Thanks in advance,
Bruce.
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.Hello community,
you can download a new advanced version of VarPtr here or from the attachment.
Cheers
Stefan
Thanks in advance,
Bruce.
VarPtr with UniCode
Hi Bruce,
try this:
Hope it helps.
Cheers
Stefan
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
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
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
This post has been edited 5 times, last edit by "StefanSchnell" (Mar 25th 2010, 12:38pm)
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)
We could do this in Jabaco very similar:
the module MVarPtr:
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
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)
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
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
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
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
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.
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.
Similar threads
-
Votings »-
Programming language
(Aug 21st 2008, 9:06pm)
-
History & News »-
RELEASE: Jabaco 1.4.0 BETA - 2008-11-18
(Nov 21st 2008, 8:52pm)
-
General topics, questions and discussions »-
difference between ByVal, ByRef, ByJava
(Jan 24th 2009, 2:11pm)
-
Vorschläge »-
Erster Test
(Dec 17th 2008, 11:12pm)
-
Tips, Tricks, Samples & Tutorials »-
Threads and Synchronisation
(Aug 21st 2008, 10:41pm)
