Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Commandline source code

Options
  • 31-01-2003 1:14pm
    #1
    Closed Accounts Posts: 1,567 ✭✭✭


    About my reply in the last thread, take it easy Typedef..

    I only asked, so I can learn from others.. yes, I am a 'noobie' as you put it.
    But even if I were a professional, I wouldn't take the same approach to
    other 'noobies' as you have to me.

    I would help if I could, because we've all been there, and we've all needed help at one
    stage whether it be from a teacher, which currently I don't have, or someone
    with more experience, which I believe you and others here to have.

    I don't doubt your skills as a programmer Typedef, I just simply asked a question and
    you gave a reply which was of no use to me.
    I don't want to sound sarcastic, but you really sound like a professional with your
    attitude..using words like 'lamer', 'troll', 'muppet' to describe someone as a person who asks a simple question.
    I made no reference to VB/C/C++ or Java...any language in fact.

    I just wanted to see how others would implement their own parsers using the API mentioned with their own logic..
    Someone suggested Google, which doesn't always get what you want.
    sjones assumed I had nothing done and was somehow..using you, to do
    my homework..right.
    I didn't post any code because I thought maybe I would get a better response to the question..

    Below is one version of the code I had done, I'm not happy with it, which
    is why I went in search of other examples to improve my own in the end
    for my own needs, I don't see anything wrong with that.

    I couldn't find any win32 asm examples on the internet that were suitable to what I needed.
    comment *
    	Compile with MASM 6
    	
    	ml /c /coff /Cp argcount.asm
    	link /SUBSYSTEM:CONSOLE argcount.obj
    	
    *
    .586
    .model flat,stdcall
    include	\masm32\include\kernel32.inc
    include	\masm32\include\user32.inc
    include	\masm32\include\windows.inc
    includelib	\masm32\lib\kernel32.lib
    includelib	\masm32\lib\user32.lib
    
    pushz	macro	pText
    	local	NextInstr
    	call	NextInstr
    	db	pText,0
    NextInstr:
    endm
    
    .data?
    szFormatBuf	db	512	dup	(?)
    .code
    main:
    	;###############################################
    	call	print_info
    	db	13,10,09,"Console Parsing Example",13,10,0
    print_info:
    	call	StdOut
    ;-----------------------------------------------------
    	call	SetupCmdLine
    	jecxz	usage				; ecx = zero if no arguements
    	mov	ebx, eax			; ebx = pointer to arg table
    	mov	edi, ecx			; move argcount into edi for loop
    	xor	esi, esi			; esi = index to table, zero = first arguement
    showarg:
    	push	ebx				; wsprintf modifies ebx, save it first
    	;-------------------------
    	push	dword ptr[ebx+esi*4]	; [table offset+arg number*scale]
    	push	esi				; arguement number
    	call	format
    	db	13,10,"Argv[%d]:%s", 0
    format:
    	push	offset szFormatBuf
    	call	wsprintf
    	add	esp, 4*4				; fixup stack
    	
    	push	offset szFormatBuf
    	call	StdOut
    	;--------------------------
    	pop	ebx					; offset to arg table
    	inc	esi					; increment for next arg number
    	dec	edi
    	jne	showarg
    exit:
    	invoke ExitProcess, \
    		eax
    usage:
    	pushz	"No CommandLine Found"
    	call	StdOut
    	jmp	exit
    ;##############################################
    ;
    ; Input:None
    ; 
    ; Output:eax = Pointer to ArgTable
    ;        ecx = Number of Arguements in ArgTable
    ;
    ; If Error:eax = NULL
    ; No Commandline found:eax = -1, ecx = 0
    ;
    ;----------------------------------------------
    SetupCmdLine	proc
    	local	pCmdLine		:dword
    	local	pCmdLen		:dword
    	local	pArgTable		:dword
    	local	pArgCount		:dword
    	;---------------------------
    	invoke GetCommandLine
    	mov	[pCmdLine], eax
    	push	eax
    	push	eax
    	pop	edi
    	xor	eax, eax
    	or	ecx, -1
    	repnz	scasb				; scan for null
    	xor	ecx, -1			; invert for total commandline length
    	mov	[pCmdLen], ecx
    	pop	edi
    	mov	al, 20h			; scan for first space
    	repnz	scasb
    	sldt	ax				; if no LDT present,assume NT
    	or	ax, ax
    	je	_winnt			; avoid 'dec ecx' problem in NT kernel
    	dec	ecx
    _winnt:
    	jecxz	nocmd
    	or	ax, ax
    	je	_skipinc
    	inc	ecx
    _skipinc:		
    	;inc	edi					; uncomment this for TD32 in WinME
    	xor	ebx, ebx				; zero arg counter
    	push	00000020h				; space
    	pop	eax
    argcount:
    	mov	byte ptr[edi-1], ah		; replace space with null byte
    	inc	ebx					; increment arg counter when space found
    	repnz	scasb					; scan for spaces
    	jecxz	endargcount				; end when ecx = zero
    	jmp	argcount
    endargcount:
    	mov	[pArgCount], ebx
    	shl	ebx, 2				; ebx*4 = total bytes for table
    	;---------------------------
    	invoke LocalAlloc, \
    		GMEM_FIXED, \
    		ebx					; allocate memory for table
    	test	eax, eax				; eax = zero if error
    	je	cmderr
    	;---------------------------
    	invoke LocalLock, \
    		eax
    	test	eax, eax
    	je	cmderr
    	;---------------------------
    	mov	[pArgTable], eax			; save pointer to arg table
    	mov	esi, eax				; address esi with pointer
    	mov	edi, [pCmdLine]
    	mov	ecx, [pCmdLen]
    	xor	eax, eax				; scan for null byte, zero
    saveargs:
    	mov	dword ptr[esi], edi		; save offset of cmdline in table
    	add	esi, 4
    	repnz	scasb					; scan edi for null
    	test	ecx, ecx				; keep scanning until ecx = zero
    	jne	saveargs
    endsave:
    	mov	eax, [pArgTable]			; return pointer to table in eax
    	mov	ecx, [pArgCount]			; arg count in ecx
    	inc	ecx					; increment for modulename
    	ret
    nocmd:
    	or	eax, -1
    cmderr:
    	ret
    SetupCmdLine	endp
    ;###########################################
    StdOut	proc	szText:dword
    	invoke lstrlen, \
    		szText
    	xchg	eax, ebx
    	invoke GetStdHandle, \
    		STD_OUTPUT_HANDLE
    	invoke WriteFile, \
    		eax, \
    		szText, \
    		ebx, \
    		esp, \
    		0
    	ret
    StdOut	endp
    ;##################################################
    end main
    


Comments

  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Right. Yes, I think Typedef was out of line. It is also however suggested in the FAQ that you should post code, or at least pseudo-code of an algorithm, and it is indeed common for people to look for us to do their homework for them.

    Your point has been made so now you can drop it. If this thread becomes another flamewar between yourself and Typedef (or anyone else) I'll just lock it again.


    Caveat, not sure about this next bit:

    You use wsprintf in the code above. IIRC GetCommandLine is generally defined GetCommandLine equ GetCommandLineA on Win9x so unless you have defined it yourself as GetCommandLine equ GetCommandLineW or altered the Unicode switch you would want to either use GetCommandLineW explicitly (I think the 9x API does include the Unicode versions of that one), or else use sprintf if you are using the ANSI version of the API.

    Anyway, I'm not too sure about that, but it would be worth looking into if you've only tested on NT or 2000.


  • Closed Accounts Posts: 1,567 ✭✭✭Martyr


    It says on MSDN that GetCommandLineW exists on all platforms.
    But if you trace the GetCommandLineA API on NT, it eventually calls GetCommandLineW
    This doesn't seem to happen on Win9x/ME..atleast I haven't seen it on my system.
    In kernel32.inc:
    GetCommandLine equates GetCommandLineA from what I remember.
    So..maybe I should use GetCommandLineW instead from now on, and instead of write a parsing routine use CommandLineToArgvW

    Probably solve alot of problems.


  • Closed Accounts Posts: 5,564 ✭✭✭Typedef


    About my reply in the last thread, take it easy Typedef..

    Ground control to someone who cares....
    Ground control to someone who cares....

    Please don't flame me from afar.

    Ground control to someone who cares....


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Take it easy Typedef..


  • Closed Accounts Posts: 5,564 ✭✭✭Typedef


    Average Joe : Aside from the flamery, it's good code.

    kudos to you flameboy.


  • Advertisement
Advertisement