Help! I do not know how to declare strings in a dll...

I've tried the following:

pDrawText proc szText:DWORD, ...
invoke MessageBox,0,ADDR szText, ....

If I run my VB-App which uses the DLL it displays some ANSI Chars, but not the right Text. If I remove ADDR the whole program crashes.

If I use LPSTR or BYTE it does the same.

Christoph von Wittich
Posted on 2002-05-09 15:18:03 by Chris_vw
To help you, we need at least the VB declaration. Or a zip with demo project wich shows your problem :)
Posted on 2002-05-09 15:32:31 by bazik
when you call the procedure pDrawText the parameter you pass to it should be the address of the string.

then in pDrawText you call MessageBox like this

invoke MessageBox 0,szText,....
Posted on 2002-05-09 15:34:32 by MArtial_Code
the string passed to your routine needs to be passed ByVal in VB
Posted on 2002-05-09 16:21:07 by Hiroshimator
There is a very useful function (undocummented) in Visual Basic that pass the address of a variable, the name is VarPtr(). Try it!
Posted on 2002-05-09 17:34:52 by wolfao
wolfao, nice picture :)

Sliver
Posted on 2002-05-09 19:19:08 by Sliver
There is a very useful function (undocummented) in Visual Basic that pass the address of a variable, the name is VarPtr(). Try it!
Do not use this function!!! VarPtr() is to get the address of a variable, NOT a string. To get the address of the string, you need to use StrPtr(), which is also undocumented. But the problem is, VB does not use standard char array type strings, internally it uses BSTR strings, so if you use StrPtr() you will get back the address of a BSTR, which is of no use to you. Also, the string at that point uses UniCode format, which you would have to make allowances for.

Hiro was correct, you need to pass the VB string ByVal, what happens then is VB will convert the string to ANSI format on the heap, and give your function a pointer, you can then access the string as MArtial_Code advised.
Posted on 2002-05-09 22:21:37 by sluggy
The declaration was wrong. It had to be "Sub" not "Function"...
Posted on 2002-05-10 03:23:34 by Chris_vw
The Rect-Structure always seems to be empty.

If I use

mov tR.left, 1
mov ...

in the procedure, the function draws the correct text.


VB:
Declare Sub pDrawText Lib "test.dll" (ByVal hDC As Long, ByVal sText As String, ByRef lpRect As Rect, ...

MASM:
pDrawText proc hDC:DWORD,sText:DWORD,lpRect:RECT,lFlags:DWORD
invoke DrawText,hDC,sText,-1,ADDR lpRect,lFlags
...
...
pDrawText endp
Posted on 2002-05-10 03:32:33 by Chris_vw
change masm part to:



MASM:
pDrawText proc hDC:DWORD,sText:DWORD,lpRect:ptr RECT,lFlags:DWORD
invoke DrawText,hDC,sText,-1,lpRect,lFlags
Posted on 2002-05-10 05:27:07 by japheth
When I use ptr Rect, the program crashes...
Posted on 2002-05-10 08:33:02 by Chris_vw
Declare Sub pDrawText Lib "test.dll" (ByVal hDC As Long, ByVal sText As String, ByRef lpRect As Rect, ...
Change this to read:


Declare Sub pDrawText Lib "test.dll" (ByVal hDC As Long, ByVal sText As String, [color=red]ByRef lpRect As Any[/color]
Posted on 2002-05-10 09:08:11 by sluggy
x
Posted on 2002-05-10 09:38:32 by Chris_vw
I think my suggestion was right. Did you change the "Drawtext" invoke (delete the "addr" before lprect)?

Why don't you use a debugger or at least display some messages in the asm part?
Posted on 2002-05-10 12:44:27 by japheth
I should really use a debugger...
Posted on 2002-05-10 13:10:48 by Chris_vw