hi all,

after a day of attempts, i decide to try to post for help.

My trouble is mixed VB with ASM.
I'm just trying to write at a memory address that i receive ByRef from VB.

The strange thing is:

if i do:
call lstrcpyn, Ptr02, offset Caption, 20
where Caption is a string defined in my asm source @data and Ptr02 is pointer i receive ByRef from VB,
everithing work correct, i can write in VB memory space.

if i do:
mov byte ptr Ptr02[0],0FFh
mov byte ptr Ptr02[1],030h
mov byte ptr Ptr02[2],02Fh
whit this method i don't found nothing (0) in VB memory space.
It don't work.

I can't understand, yesterday it seems to work, to day i've try for 10 ours... nothing



If some help.... many thanks B7
Posted on 2002-03-26 12:55:01 by Bit7
if i do:
mov byte ptr Ptr02[0],0FFh
mov byte ptr Ptr02[1],030h
mov byte ptr Ptr02[2],02Fh
whit this method i don't found nothing (0) in VB memory space.
It don't work.

that's because you're overwriting the pointer itself and not the bytes at the memory location it's actually pointing to. to make the above code work you'd have to do something like:


mov edi,Ptr02
mov byte ptr [edi],0FFh
mov byte ptr [edi+1],030h
mov byte ptr [edi+2],02Fh
Posted on 2002-03-26 13:59:56 by Tola
Tola,

thanks !

mmmmh but are you shure?

I remeber that i've used that syntax many times...

Ok tomorrow @job (is there i've the trouble with that stupid VB) i will try if problrm was there.

Thanks again B7
Posted on 2002-03-26 14:18:55 by Bit7
You cannot ByRef a string in VB, because what your asm function will receive is a pointer to a BSTR. You need to pass the string ByRef, you will then get a pointer to an ANSI string. Refer to this post of mine for further details: go here. (in case the link doesn't take you direct to the post, it is the 9th one down the page).


I will get around to writing a tutorial on this one day...
Posted on 2002-03-26 16:09:10 by sluggy
hi all,

Tola, you were right ! But i can't go out of my problem :(

I Try to explain better:

Ptr01 and Ptr02 are two pointer to 2 array of byte in VB.

If in my asm i do:
call lstrcpyn, Ptr02, offset TestString, 20

i get in VB perfectly my array back.

if i do
call lstrcpyn, Ptr02, ptToMemory, 10
where ptToMemory is a pointer to a space created with GlobalAlloc, filled by ReadFile, i got in VB 0.

I can't understand... If i fill that space "by myself" then i get in VB the content....


I can't understand....

sorry, but every idea or suggestion will be wonderful.

Bye B7
Posted on 2002-03-27 02:15:38 by Bit7
Hi,

maybe I'm wrong but there are a few points I can share.

VB parameters often (maybe always?) uses the VARIANT structure. This is defined as the follows (my translation):



VARIANT struc
var_vt dw ?
var_wReserved1 dw ?
var_wReserved2 dw ?
var_wReserved3 dw ?

var_lVal:
var_bVal:
var_iVal:
var_fltVal:
var_dblVal:
var_boolVal:
var_scode:
var_cyVal:
var_date:
var_bstrVal:
var_puintVal: dd ?, ?
VARIANT ends


this means if you call a VB function with an integer you'll need to create a variant structure and set the entries var_vt: to VT_INT4 and var_iVal: to the integer.

the first entry gets the data type like:

VT_EMPTY, VT_I4, VT_BSTR etc.

In case of VT_BSTR at var_bstrVal: will follow the string pointer, but this is usually a Unicodestring. In addition there is the length of the string appended as dword just before the string.


for the string "Hallo" this results in:



db 05h, 00h, 00h, 00h
db 48h, 00h, 61h, 00h, 6Ch, 00, 6Ch, 00h, 6Fh, 00h, 00h, 00h



Hope this helps ..

Bye Miracle
Posted on 2002-03-28 11:56:33 by miracle
Hey there.
About a month ago I was coping with the same problem and eventually came up with this. I'm not sure if this is of any help...

First declare your function as:
Private Declare Sub MyProc Lib "MyDLL.dll" (Buffer As String)

And then you can call it as:
Dim Test As String
Test = "Hello world"
MyProc Test

In the DLL itself this is how the procedure is declared:

MyProc PROC Buffer :DWORD
pushad ; I don't know why but I had to include
;these for VB not to halt

mov eax,
mov edi,

;...do reads / writes to / from the buffer
;using edi like this: mov al, BYTE PTR

popad
ret 4 ;number is size of all parameters in bytes
MyProc ENDP
Posted on 2002-03-28 14:46:15 by Milos