Page 1 of 1

Can not read memory block allowed by VB

Posted: Mon Sep 21, 2009 1:25 pm
by Kwai chang caine
Hello at all

I have found this genius code of Stefan Savev who do a thing that it's normally not the works of VB :shock:
Create block memory and read in it
I just return the adress of pointer but not the value 10 :(

VB code

Code: Select all

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function GetProcessHeap Lib "kernel32" () As Long
Private Declare Function HeapAlloc Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function HeapFree Lib "kernel32" (ByVal hHeap As Long, ByVal dwFlags As Long, lpMem As Any) As Long
Private Declare Sub CopyMemoryWrite Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As Long, Source As Any, ByVal Length As Long)
Private Declare Sub CopyMemoryRead Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, ByVal Source As Long, ByVal Length As Long)
Private Declare Function AccessMemory Lib "DllAllocationMemoire.dll" (PointeurAllocation As Long) As Long

Private Sub Form_Load()

 Dim ptr As Long   'int * ptr;
 Dim hHeap As Long
 hHeap = GetProcessHeap()
 ptr = HeapAlloc(hHeap, 0, 2) 'an integer in Visual Basic is 2 bytes
 
 If ptr <> 0 Then
  'memory was allocated
  'do stuff
  Dim i As Integer
  i = 10
  CopyMemoryWrite ptr, i, 2 ' an integer is two bytes
  
  Dim j As Integer
  CopyMemoryRead j, ptr, 2
  MsgBox "The adress of ptr is " & CStr(ptr) & vbCrLf & "and the value is " & CStr(j)
  HeapFree GetProcessHeap(), 0, ptr
 End If
End Sub
Code of complex DLL :mrgreen:

Code: Select all

ProcedureDLL AccessMemory(*BSTR)
     MessageRequester("", Str(PeekL(*BSTR)))
 EndProcedure
But obviously KCC try to read the memory block with PB and can not :(
Someone know why ???

Good day

Re: Can not read memory block allowed by VB

Posted: Mon Sep 21, 2009 1:33 pm
by rsts
Where's your PB code?

cheers

Re: Can not read memory block allowed by VB

Posted: Mon Sep 21, 2009 1:33 pm
by srod
Kwai, as far as I can see the VB code isn't doing much of anything - certainly nothing useful! You allocate some heap memory, stuff the 2 byte value of 10 into it and then read it into variable j? The code simply seems to be demonstrating some heap allocation functions, but it is certainly less than useful!

Here's the same code in PB :

Code: Select all

i = 10
j = i
Okay you can use PB's memory commands if you really wish which allocate heap memory, but what exactly are you trying to achieve?

Re: Can not read memory block allowed by VB

Posted: Mon Sep 21, 2009 3:20 pm
by Kwai chang caine
Hello SROD and RSTS :D

Excuse me for the long time for the answer, but i'm disturbed in my deskroom, and the man don't want to go out :?
The time is so long with him.....but i don't dare say to him, that i talk with my personal MASTER and i like do that all the day, and today it's a problem very important for me :(
Rsts wrote:Where's your PB code?
cheers
It is here :D

Code: Select all

ProcedureDLL AccessMemory(*BSTR)
  MessageRequester("", Str(PeekL(*BSTR)))
EndProcedure
Is it not enough big ??? :lol:
My master at me wrote:Here's the same code in PB :
i = 10
j = i
In fact the VB code create "allocatememory"
Put the number 10
Read the memory so 10
Open MsgBox with the adress of pointer memory and the value...
Apparently that's works like in PB :roll:
My big Master wrote:Okay you can use PB's memory commands if you really wish which allocate heap memory, but what exactly are you trying to achieve?
I'm always in my problem of pass array between VB/PB/DLL :oops:
Because since a month , i have always not found a solution who do all of that :

1/ Send an array string from VB to DLL PB , modify it, (DIM, REDIM, ADD line) and return to VB
2/ Send an array string from PB to DLL PB , modify it, (DIM, REDIM, ADD line) and return to PB
3/ And this two things with the same procedure in the DLL :roll:

I have try several of the thousand method that you have say to me, but they are always a problem somewhere :?

1/ I have try byref, but the pointers of the array DLL disappears because, like you have good explain to me the HEAP (Excuse me the last time i have say HEAD :oops:) is different.
So it's difficult to manage pointers

2/ I have try by ARRAY, and the wonderfull code that you have give to me with PROTOTYPE
But that's not works with VB :?

3/ I have try the fantastic method of XOMBIE, i love ASM, but that's affraid me, because i don't understand anything :oops:

4/ I have try by string joined "procedurereturn Array(1) + Array(2)" but i found this method not nice, and it's limited by 32k by variable, and i surely manage a day big array of DATA

5/ Today i have try also by PIPE, with the great code of NICO and a code of VB.
Apparently that's works, but i don't all understand, and i have a new idea for bloc memory

6/ I have try also by bloc memory, with the help of CPLBATOR, but the sizeofarray and REDIM, DIM not match :?

7/ Then this is me for the Xe method :oops:
I try another time the block memory, but i try in first time VB, because it's him the real problem
VB can't do anything :evil:
Don't want this, or that...don't write like this, like that... :?

1/ So, i have found the above code, for create bloc memory
2/ I put inside, my array of string, in the VB code
3/ I read the bloc in PB DLL
4/ I modify the array (REdim, Dim, add line) in the DLL
5/ I recover my new array, all new and modified :D

The other day, you have say to me they are hundred method to do what i want... :shock:
But KCC don't want so much...just ONE who works and do all what i have say above..it's all :(

It's the first time since i have begin PB, where i can't do what i want...and more i code...more i don't understand, and mix in my head, all this methods :(
I have at the least, 20 differents codes, it do each a thing, but have problem at the end :?
I don't know what i must do.....KCC is lost this time...i'm tired to try, try try have hope, and the day after cry because that's not do all i want .... i'm so sad :cry:

If you have a super big, great idea like you have always...pleeeeaaaasse !!!
Say to your floor clothe what is the more simple, and professionnal for that an EXE VB/PB and DLL can deal data between them :roll:

Thanks a lot MASTER 8)

Re: Can not read memory block allowed by VB

Posted: Mon Sep 21, 2009 4:23 pm
by srod
Okay, I understand what you are doing, but I am not convinced you need to do this or that it will in fact make your life any easier? The code you've posted above uses several declares of the same function simply to work around the fact that you cannot get the address of a variable in VB (unless you use the undocumented VarPtr function!)

Passing a string array from VB to PB should be fine providing you do not expect to be able to retrieve the array in the form of a single PB array etc. You cannot expect that to work. Instead, you should think about using some kind of pointer in the dll (instead of an array). How you use this pointer to access the individual strings depends on how VB bundles up it's string arrays? (I have no experience of this, but it should not be too difficult to get it to work.)

Once you have access to these strings for God's sake do not attempt to modify them using standard PB string functions else you'll run up against the earlier heap problem... again! If VB passes strings in BSTR format then you can modify them using api. If not, then you must be very careful if modifying them from your PB code (again, DO NOT use the regular PB string functions to do this) Use PokeS() etc.

I don't have VB installed Kwai. Otherwise I could have a go at this for you.

**EDIT : if you are using VB 5 or 6, then VarPtr(stringArray(0)) should give you the address of the string array in VB (or at least the address of the first element). In a PB dll, you should be able to receive this with a *strPtr.STRING pointer.

Re: Can not read memory block allowed by VB

Posted: Mon Sep 21, 2009 4:40 pm
by Kwai chang caine
Thanks a lot SROD for understand me 8)

I have just read now two another methods for passing data between exe, and that VB know...the MUTEX and the FileMapping :shock:
Obviously....you are right again two methods.
But is she adapted to my case ??? I don't believe ..
What is your mind ???

The good method is at your mean the passing of pointer.

1/ I can cheat VB for pass the arraysize of the array in the DLL
I pass it, into the zero record, because i never use it :oops:

2/After i can copy all the array into another in the DLL
But the problem is for return the newarray
I i do this "ProcedurReturn @NewArray()" the it's the pointers of NewArray who is send :?
And the second time i use the DLL, NewArray is full of hieroglyph character because VB have freeing the pointer of the DLL :(
I must found a method to return the array from the DLL to VB
It's here that i have thinking to allocateMemory .....

Re: Can not read memory block allowed by VB

Posted: Mon Sep 21, 2009 5:24 pm
by srod
File mapping between a client and a dll is overkill for this kind of thing. You'd really only want to consider that for sharing data between processes, although admitedly, it is an easy way to go.

Well, as I keep saying you should be able to pass an array of strings to a PB dll easy enough and without anything more complex than using a pointer. I do it in Powerbasic. Without access to VB I am unable to help further and we'll just keep going around in circles Kwai. Rather than look for as many different solutions as possible - why not just try one? Work with it until you get it working! Heck you could just poke all the strings into a buffer provided by the dll - simple (if not very elegant!)

Re: Can not read memory block allowed by VB

Posted: Mon Sep 21, 2009 7:35 pm
by Kwai chang caine
why not just try one?
yes srod, it's what i have doing :D
But they are always in the end a problem who stop me because it's impossible.
Remember the example of the passing ARRAY in parameter.....you have say to me it's not possible like this.
I have tried....but not winned :lol:
you should be able to pass an array of strings to a PB dll easy enough and without anything more complex than using a pointer
You are right, the more simple solution, it's the pointers, and it's perhaps the only solution that i have understand....or nearly :lol:

My problem is not for send the pointers of the array to the DLL, but it's for return the array in VB :cry:
Because this method copy the original pointers of the array of DLL
Or then i can modify the array directly in the DLL, with POKE.
GEBONET give me a code like this.

But the problem in this case, it's that the pointer equal an adress where is the real variable.
Ok, but if in my DLL i poke a more big variable that the old.
Then i think..... that crash.

It must create a new pointer for a new line of the array, no ??
Then at my mind, but my mind is not really important :oops: the most difficult it's to return the modified array with no crash :roll:
So perhaps in bloc memory ???

Take the pointer in the DLL, like you say to me, write in memory with poke, if the new value is more big, change the pointer in the old array, on a new string variable (i don't know if is possible ??? :roll:).
After create bloc memory, put the new array in it
Return the pointer of bloc
In the VB return the pointer and try to read bloc of memory with the code above this thread.

I go to try this method, if you are a better way to return the array of the DLL.
Don't be shy ...say it to me :lol:

Tommorow a new fight begin........10e round of the fight between KCC against the bad array of DLL :?
The first who have a new idea.....call the other :lol:

You will miss me already that I shutdown my pc :cry:
Thanks SROD and i wish you a very good night 8)

Re: Can not read memory block allowed by VB

Posted: Mon Sep 21, 2009 8:27 pm
by srod
At the end of the day, what you are attempting to do, for a general dll, is not good practice - you are asking for trouble. Getting a dll to directly alter string data in a client application is very bad - at least in general. When I write a dll to do this it is usually for very specific situations where I know that there is no danger in causing memory leaks or general faults etc.

Let me outline an example; my nxReport reporting engine which is a dll.

There are functions in this dll which need to return strings. At no point do I attempt to return a global string etc. as this is not threadsafe. Instead the dll simply places the string into an optional buffer provided by the client. I say optional, because the client can first query the function for the length of the string etc. before allocating a sufficiently large buffer. Now this is a very common way of getting a dll to return a string - I would say that it is probably THE most common method!

Now, additionally, nxReport will call a callback function in the client application. So my PB written nxReport dll could call, for example, a function in a VB client application. These functions must sometimes return a variable length string. So the callback returns a string to my PB dll and when the dll is done with the string it has a massive problem of freeing this string! Only VB can free it! Because of this and because the callback needs to run as fast as possible, I have insisted that the callback returns an API created BSTR. Such strings are managed by the OS and can be freed by the dll even when created by the VB client! Using a buffer in this case would be too slow for my needs as would calling a second VB function just to free the string etc.

The point I am making Kwai is that you can simplify your problem as far as you want by getting the VB program to write all the strings into a large buffer etc. and then to copy them back after calling the dll! Attempting to get the dll to modify the VB strings 'in place' is like sticking your head into a lions mouth just to see how many teeth he has! If he doesn't bite your head off then there will nevertheless be a damn good chance that he will nibble upon an ear or two!

I strongly advise against attempting to directly alter string data within the client app. Yes it can be done, but it is usually best avoided.

If I had VB lying around then I would have a crack at it for you. As it is any code I could post would be taking pot-shots in the dark and with a bag over my head!

Simplify!

:)

Re: Can not read memory block allowed by VB

Posted: Mon Sep 21, 2009 9:07 pm
by Kwai chang caine
My funny and giant master wrote:Attempting to get the dll to modify the VB strings 'in place' is like sticking your head into a lions mouth just to see how many teeth he has!
:lol: :lol: :lol: :lol:
In france it's usually to say, that the british humor, is special :roll:
But you show to me that your humor is very very good, even with a french :lol:
Like that ...the humor is like the music, friendship, love , etc...it's a little bit universal 8)
I'm happy to see that i'm not alone to love laughing :D
Master SROD one kenobi wrote: As it is any code I could post would be taking pot-shots in the dark and with a bag over my head!
Where is the problem ???? :roll:
It's exactely how i code all the day :roll:
You understand a little bit more why i code PB, like if i coded with my feets :D

Tomorrow ...i tried to create a code like you explain to me
And after....i allow me to present the result of my works to my master.
The head down, and the fingers up for that you can strike a time by error with a rule...like at school in the old time :oops:

When i say to you that it's difficult to quit you.....
It was so difficult to resist to see a last time for today, if you have answered to me :D
This is the good example....you are not so strong....but also funny...even if like you i don't understand all...but fortunately....because if we understand all...perhaps we do peepee in our pants :lol:

Who say that the programmers is old, with big glasses, oily hair, and teeth apart and especially sad and antipathetic ?????
We proof the contrary :D

Image

Re: Can not read memory block allowed by VB

Posted: Mon Sep 21, 2009 9:31 pm
by srod
Kwai, go and put some bloody clothes on! :)

Re: Can not read memory block allowed by VB

Posted: Mon Sep 21, 2009 9:43 pm
by Kwai chang caine
Apparently this picture is not sexy ????
Bouarf !!!! so it's pity....

I go on bed and remove the last close :lol: :lol:

Don't do nightmare with this nice picture :D
At tomorrow...i'm already impatient to talk code with you....and learn to you something :lol: :lol: :lol: