Write an ADSL automatic dial program with MASM32

xiaoxiao2021-03-06  192

The automatic dialing program is very simple. It can be found soon in MSDN (Remote Access Service), and the implementation is also very fast with VC, because MSDN has basically given source code. But as a person who has just started researching Win32 ASM, it is difficult to use Masm32.

First give an implementation under VC:

#include "stdafx.h" #include #include #pragma Comment (lib, "rasapi32.lib")

int APIENTRY WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {DWORD dwCb = sizeof (RASENTRYNAME); DWORD dwEntries = 0; RASENTRYNAME ren = {0}; ren.dwSize = dwCb;

if (ERROR_SUCCESS == RasEnumEntries (NULL, NULL, & ren, & dwCb, & dwEntries)) {RASDIALPARAMS rp = {0}; rp.dwSize = sizeof (RASDIALPARAMS); lstrcpy (rp.szEntryName, ren.szEntryName); BOOL fPass; RasGetEntryDialParams (NULL, & RP, & FPASS); HRASCONN HCONN = NULL; IF (Error_Success == Rasdial (NULL, NULL, & RP, 0, NULL, & HCONN) {OutputDebugstring ("OK");}} Return 0;}

This procedure is very simple. It is basically nothing to do. The main reason is that in my machine last debugging, you can use it if it is ^ _ ^. I don't do anything, have anything you don't understand MSDN

Ok, now it is now changed to assembly code. Just started to make it very simple, first find a Win32 ASM example, then modify on it, remove the creation window, message loop, message processing, etc., and then fill the compilation of the above code modified. I spent more than two hours to complete with my compilation of my pole.

The first code is like the above C code, I declare a RasentryName structure object, then call RaseNuMentries, but always return the error value of 278h, it turns out that there is not space. Under the VC, the RasentryName structure size is 214h, but under ASM, the structure is only 105h. why? Originally in the definition in the C environment, additional two parameters dwflags and szphonebookpath are added, and the macro definition is separated. Wanzhi, I had to write a RasentryName structure:

RasentryName5a Struct DWSIZE DD? SZENTRYNAME DB RAS_MAXENTRYNAME DB RAS_MAXENTRYNAME 1 DUP (?) DWFLAGS DD? SzphoneBookPath DB MAX_PATH 1 DUP (?) RaseTryName5a Ends However, in the assembly environment, SIZE of the structure is not right. At this time, I remembered it. It turned out that the byte alignment of structures was re-modified.

RasentryName5a Struct DWSIZE DD? SZENTRYNAME DB 260 DUP (?) DWFLAGS DD? SZPHONEBOOKPATH DB 264 DUP (?) RasentryName5a Ends

RasentryName5 EQU

This time RaseNumentries is not wrong. Next, the RasDialParams structure is not right. With the last experience, this time I quickly spent (fight?!) Structure of RasdialParams4:

RasdialParams4a Struct DWSIZE DD? SZENTRYNAME DB 101H DUP (?) SZCALLBACKNUMBER DB 81H DUP (?) Szusername DB 101H DUP (?) Szpassword DB 101H DUP (?) SZDOMAIN DB 13H DUP (?)

DWSUBENTRY DD? DWCALLBACKID DD? RasdialParams4a Ends

RasdialParams4 EQU

OK, this time no problem? Run, did not respond? what happened? Check the return value of Rasdial, 668, find it in VC, error_no_connection, why?

In other cases, try to assign the last parameter hconn of RasDial to 0, Yeah, success! It's really fascinated by Faint.

Hereinafter, the source code:

; ############################################################################### ################# ;; autodial.asm ;; ####################### ######################################################################################################################################################################################################################################################################################################## ####

Assembler Specific Instructions for 32 Bit ASM CODE

.386; Minimum Processor Needed for 32 Bit .Model Flat, Stdcall; Flat Memory Model & Stdcall Calling Option CaseMap: None; Set Code To Case Sensitive

; ############################################################################### ################################

Include /masm32/include/windows.incinclude /masm32/include/kernel32.inc include /masm32/include/rapi32.inc

INCLUDELIB /MASM32/LIB/kernel32.lib includelib /masm32/lib/rasapi32.lib

; ############################################################################### ################################

Winmain Proto

RasentryName5a Struct DWSIZE DD? SZENTRYNAME DB 260 DUP (?) DWFLAGS DD? SZPHONEBOOKPATH DB 264 DUP (?) RasentryName5a Ends

RasentryName5 EQU

RasdialParams4a Struct DWSIZE DD? SZENTRYNAME DB 101H DUP (?) SZCALLBACKNUMBER DB 81H DUP (?) Szusername DB 101H DUP (?) Szpassword DB 101H DUP (?) SZDOMAIN DB 13H DUP (?)

DWSUBENTRY DD? DWCALLBACKID DD? RasdialParams4a Ends

RasdialParams4 EQU

.DATA

.code

Start:

Invoke WinMain

Invoke EXITPROCESS, EAX

WinMain Proc

Local Local DWCB: DWORD LOCAL DWENTRIES: DWORD LOCAL RP: RASDIALPARAMS4 LOCAL FPASS: BOOL LOCAL HCONN: DWORD

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;; MOV DWCB, SIZEOF RasentryName5 MOV LPRASENTRYNAME, 0

AllocaterasentryNameloP:

CMP LPRASENTRYNAME, 0 JE AllocaterasentryName Invoke GetProcessHeap Invoke Heapfree, Eax, 0, LPRASENTRYNAME

AllocateRasEntryName: invoke GetProcessHeap invoke HeapAlloc, eax, 0, dwCb cmp eax, 0 je RetFail mov lpRasEntryName, eax invoke RtlZeroMemory, lpRasEntryName, dwCb mov ebx, sizeof RASENTRYNAME5 mov eax, lpRasEntryName mov (RASENTRYNAME5 PTR [eax]) dwSize, ebx.

Invoke Rasenumentries, NULL, NULL, LPRASENTRYNAME, ADDR DWCB, ADDR DWENTRIES CMP EAX, 278H JE AllocaterasentryNameloopcmp Eax, 0 JNE Retfail

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;; MOV DWCB, SIZEOF RASDIALPARAMS4 INVOKE RTLZERMEMORY, ADDR RP, DWCB MOV EAX, DWCB MOV RP.DWSIZE, EAX

MOV EAX, LPRASENTRYNAME INVOKE LSTRCPY, ADDR RP.SZENTRYNAME, ADDR (RasentryName5 PTR [EAX]). SzenTryName

Invoke RasgeTryDialParams, NULL, ADDR RP, AddR Fpass CMP Eax, 0 JNE Retfail

Mov HCONN, 0 Invoke Rasdial, Null, NULL, ADDR RP, 0, NULL, ADDR HCONN CMP EAX, 0 JNE RETFAIL

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;, Retfail:

CMP LPRASENTRYNAME, 0 JE AllocaterasentryName Invoke GetProcessHeap Invoke Heapfree, Eax, 0, LPRASENTRYNAME

RET

Winmain ENDP

End Start

For objects of structural RasentryName5, I actually declare a memory pointer, lprasentryName, then enforce this memory (such a statement in assembly?) Into RasentryName5. This method is consistent with MSDN.

When WinMain is complete, you need to return with RET, otherwise the program ends with INT 3.

The above code compiled in Masm V8.0, Softice debugging, running in XP SP1 ADSL

Welcome someone to discuss compilation with me, your younger brother has just learned a few days, your interest is very interesting.

转载请注明原文地址:https://www.9cbs.com/read-128747.html

New Post(0)