The example code is written in CBuilder,but I cannot make it in masm,please help me!
The .cpp is:
//---------------------------------------------------------------------------
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
char* (__stdcall *GetNumber)(int, char*);
int (__stdcall *GetSize)(int, char*);
HINSTANCE DllInst = NULL;
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
  char* Code = "012345678910";
  int i;
 
  i = edt->Text.ToInt();

  if (DllInst == NULL) DllInst = LoadLibrary("Get.dll");
  if (DllInst)
  {
    GetNumber = (char* (__stdcall*)(int, char*))GetProcAddress(DllInst,"GetNumber");
    GetSize = (int (__stdcall*)(int, char*))GetProcAddress(DllInst,"GetSize");

    //Call GetNumber
    Edit1->Text = GetNumber(i, Code);

    //Call GetSize
    Edit2->Text = GetSize(i, Code); 
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
  Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
  if ( DllInst ) FreeLibrary (DllInst);
}
//---------------------------------------------------------------------------



How can I change it to MASM format, I cannot make the "GetNumber = (char* (__stdcall*)(int, char*))GetProcAddress(DllInst,"GetNumber");" and "Edit1->Text = GetNumber(i, Code);" clear.
Posted on 2006-03-21 21:43:04 by Eric4ever
the first statement should look like this:
	.data

pfnGet dd ?
szGetFunc db "GetNumber", 0

.code

push offset szGetFunc
push DllInst
call GetProcAddress
mov pfnGet, eax
the second statement needs the structure offset of the "Text" variable, if you have access to this code in a debugger / disassembler, you need to check out the offset there. If you have it, then it will be probably:
	push	Code
push i
call pfnGet

mov ecx, Edit1
mov , eax
Posted on 2006-03-23 04:21:40 by beaster

the first statement should look like this:
	.data

pfnGet dd ?
szGetFunc db "GetNumber", 0

.code

push offset szGetFunc
push DllInst
call GetProcAddress
mov pfnGet, eax
the second statement needs the structure offset of the "Text" variable, if you have access to this code in a debugger / disassembler, you need to check out the offset there. If you have it, then it will be probably:
	push	Code
push i
call pfnGet

mov ecx, Edit1
mov , eax



beaster,

the "push  Code"  error:      error A2070: invalid instruction operands

and I use "invoke pfnGet,i,addr Code"    error:  error A2190: INVOKE requires prototype for procedure
Posted on 2006-03-23 20:55:14 by Eric4ever
I tried "push offset Code", it link well but run error!
The code is:

  .data?
dllInst  dd ?
i        dd ?
GetNumber dd ?
GetSize  dd ?
  .data
szDll        db "Get.dll",0 
szVerbOpen  db "open",0
  .const
szError  db 'Get.dll Missing!',0
Code      db '000000000000',0       
strGetNumber  db 'GetNumber',0
strGetSize  db 'GetSize',0

  invoke GetDlgItemInt,hWnd,IDC_NUM,NULL,FALSE      ; IDC_NUM is a EDITTEXT
  mov i,eax 
  invoke LoadLibrary,addr szDll
  .if eax
    mov dllInst,eax
    invoke GetProcAddress,dllInst,addr strGetNumber
    mov GetNumber,eax
    invoke GetProcAddress,dllInst,addr strGetSize
    mov GetSize,eax
  .else
    invoke MessageBox,hWnd,addr szError,NULL,MB_OK or MB_ICONWARNING
  .endif

    push Offset Code                     
    push i
    call dword ptr
    invoke SetDlgItemText,hWnd,IDC_Edit,addr GetNumber    ; IDC_Edit is a EDITTEXT(display the result)
Posted on 2006-03-27 19:11:41 by Eric4ever

I tried "push offset Code", it link well but run error!
The code is:


/ -- snip -- /

  .const
szError  db 'Get.dll Missing!',0
Code      db '000000000000',0       
strGetNumber  db 'GetNumber',0
strGetSize  db 'GetSize',0

/ -- snip -- /

    push Offset Code                     
    push i
    call dword ptr
    invoke SetDlgItemText,hWnd,IDC_Edit,addr GetNumber    ; IDC_Edit is a EDITTEXT(display the result)



Well, what exactly is the "GetNumber" function supposed to do? What does it return? What is the second parameter? Is it IN-buffer or OUT-buffer? If OUT, then I guess putting it in .const section might be a problem here.

Anyway, here's how you can INVOKE dynamically loaded functions - just two typedefs do it possible:

  __GetNumber typedef proto integer :DWORD, character :LPSTR
  _GetNumber  typedef ptr __GetNumber

.data?
  GetNumber _GetNumber ?

;then somewhere in the code
  invoke GetProcAddress, hLib, addr szGetNumber
  mov GetNumber, eax

;...

  invoke GetNumber, your_integer, addr your_charbuffer
Posted on 2006-03-28 05:52:30 by Morris


I tried "push offset Code", it link well but run error!
The code is:


/ -- snip -- /

?  .const
szError?  ? db 'Get.dll Missing!',0
Code?  ?  ?  db '000000000000',0?  ?  ?  ?  ? 
strGetNumber?  db 'GetNumber',0
strGetSize?  ? db 'GetSize',0

/ -- snip -- /

?  ?  push Offset Code?  ?  ?  ?  ?  ?  ?  ?  ?  ?  ? 
?  ?  push i
?  ?  call dword ptr
?  ?  invoke SetDlgItemText,hWnd,IDC_Edit,addr GetNumber?  ?  ; IDC_Edit is a EDITTEXT(display the result)



Well, what exactly is the "GetNumber" function supposed to do? What does it return? What is the second parameter? Is it IN-buffer or OUT-buffer? If OUT, then I guess putting it in .const section might be a problem here.

Anyway, here's how you can INVOKE dynamically loaded functions - just two typedefs do it possible:

?  __GetNumber typedef proto integer :DWORD, character :LPSTR
?  _GetNumber?  typedef ptr __GetNumber

.data?
?  GetNumber _GetNumber ?

;then somewhere in the code
?  invoke GetProcAddress, hLib, addr szGetNumber
?  mov GetNumber, eax

;...

?  invoke GetNumber, your_integer, addr your_charbuffer



Hi Morris,

Thanks for your kindly help and explanation :D
It works!

Regards,
Eric
Posted on 2006-03-28 19:45:29 by Eric4ever