The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: jojo on June 14, 2005, 07:50:37 AM

Title: Passing a pointer to a DLL
Post by: jojo on June 14, 2005, 07:50:37 AM
Hi, I am new to this forum but have some experience in programming (check Google for mdg dashboard).
My problem: I am working with a wonderful old 16-bit language called GFA-Basic that is close to Pascal. Some things I prefer to do in MASM, though, e.g. calling 32-bit APIs. I pass strings between the 16-bit Basic and the 32-bit MASM code with...
~PostMessage(Handle2Masm&,PrivateMessage%,AnyNumber&,MyWindow&)
... and MyWindow& can subsequently be used to exchange info via ~SendMessage(MyWindow&,WM_SETTEXT,LEN(t$),t$)
All that works perfectly, but now I had the bright idea to send a pointer to a string from Basic to MASM.
I do it with a MASM DLL, called via GetProcAddress32W etc, then I have code such as:

   lea   edi,MyPointer   'I put a nice little number into [MyPointer]
   mov   eax,[edi]
   invoke   dwtoa,eax,ADDR sizeBuffer
   invoke   MessageBox,0,ADDR szMsg,ADDR AppName,MB_OK

Now what happens is that I get indeed a message box, and it shows me a number, but yep! it's not the number I expected, i.e. it does not read the number that is stored at the location MyPointer. What I get instead is - the pointer itself, exactly as I passed it from Basic!! Digging deeper with lea edi,[edi] produces a GPF.

What's wrong? I am sure it's something pretty stupid, but I am lost and hope you can show me the way.


Title: Re: Passing a pointer to a DLL
Post by: Jeff on June 14, 2005, 08:00:38 AM
hi jojo,
i have a pretty good feeling that MyPointer contains, well... the pointer to your number.  so rather loading the address of the address of the number into edi, why not move the address of the number into edi instead?  :wink   mov   edi,MyPointer   'I put a nice little number into [MyPointer]
   mov   eax,[edi]
   invoke   dwtoa,eax,ADDR sizeBuffer
   invoke   MessageBox,0,ADDR szMsg,ADDR AppName,MB_OK
Title: Re: Passing a pointer to a DLL
Post by: jojo on June 14, 2005, 08:32:51 AM
Thanks Jeff, that sounds straight. I guess I was confused by the usage of LEA in 68k (a long time ago I owned an Atari...).

I tried the mov edi, MyPointer, and it produced a GPF. Now the GPF might mean that either
- what I am passing to the DLL is not a vaild 32-bit pointer; the 16-bit Basic may have pointers in 16:16 notation (but it's clearlyt a 32-bit value that I am passing on)
- or I am generally not allowed to pass pointers to a DLL; I am uncertain about that, although from the literature it seems that calling process and DLL share the same address space - is that correct, and is it correct also between a 16-bit calling process and a 32-bit DLL??
Title: Re: Passing a pointer to a DLL
Post by: jojo on June 14, 2005, 06:42:24 PM
Problem solved - thanks to Jeff!

16-bit side:

    lpf32%=GetProcAddress(GetModuleHandle("kernel"),"CallProc32W")
    hDll%=^LoadLibraryEx32W("D:\masm32\My32.dll",0,0)
    PA%=^GetProcAddress32W(hDll%,"GiveMeANumber")
    IF PA%
      ret%=C:(lpf32%)(L:1,L:%1,L:PA%,L:MyPointer%)
    ENDIF
    REM   ; my number is in [MyPointer%]
    REM The tricky bit is the L:%1 - see the fAddressConvert para at
    http://msdn.microsoft.com/library/en-us/winprog/winprog/calling_the_dll_function.asp?frame=true
...

32-bit DLL:
GiveMeANumber proc MyPara:DWORD
   mov   eax,MyPara   ; my number is inside there
   mov   eax,[eax]   ; let me get it out
   invoke   dwtoa,eax,ADDR dwtoaBuf   ; translate it to string
   invoke MessageBox,NULL,addr NumBuf,addr AppName,MB_OK   ; and show it
   ret   
GiveMeANumber endp