News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

how to build a 32-bit windows program?

Started by zhoujian, March 14, 2009, 08:33:49 AM

Previous topic - Next topic

zhoujian

anyone can help me with compailling the following code?
i have a difficult in linking:
when i entered:
ml t386.asm
it returned:

/z2
"t386.obj"
"t386.exe"
NULL
LINK : warning LNK4044: unrecognized option "z2"; ignored
t386.obj: warning LNK4033: converting object format from OMF to COFF
LINK: fatal error LNK1180: cannot open input file "t386.exe"


.386
.model  flat,stdcall
option  casemap:none
include \masm32\include\windows.inc
include \masm32\include\masm32.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\gdi32.inc
include \masm32\include\comctl32.inc
include \masm32\include\oleaut32.inc
includelib \masm32\lib\masm32.lib
includelib  \masm32\lib\user32.lib
includelib  \masm32\lib\kernel32.lib
includelib  \masm32\lib\gdi32.lib
includelib  \masm32\lib\comctl32.lib
includelib  \masm32\lib\oleaut32.lib
WinMain proto:dword,:dword,:dword,:dword
WndProc proto:dword,:dword,:dword,:dword
FileOpen    proto:dword
FileSave    Proto:dword
;------------------------------------------------
.data
szDisplayName   db "Editor",0
CommandLine     dd 0
hWnd            dd 0
hInstance       dd 0
hWndEdit        dd 0
szFileError     db "Read or Write File ERROR.",0
szError         db "Error!",0
szOpenFileName  db "T386.ASM",0
szSaveFileName  db "T.ASM",0
szEditClass     db "EDIT",0
szClassName     db "MainWndClass",0
AboutMsg        db "Open File Name:T386,ASM;Save File Name;T.ASM",0
TheText         db "Save the Texe?",0
TheExit         db "Please Confirm Exit",0
MAXSIZE         = 2000
Buf             db MAXSIZE+1 dup(0)
.code

;--------------------------------------------
start:
invoke  GetModuleHandle,NULL
mov     hInstance,eax
invoke  InitCommonControls
invoke  GetCommandLine
mov     CommandLine,eax
invoke  WinMain,hInstance,NULL,CommandLine,SW_SHOWDEFAULT
invoke  ExitProcess,eax

;---------------------------------------------
WinMain proc hInst  :dword,
        hPrevInst   :dword,
        CmdLine     :dword,
        CmdShow     :dword
;
local   wc  :WNDCLASSEX
local   msg :MSG
local   Wwd :dword
local   Wht :dword
local   Wtx :dword
local   Wty :dword
local   rectClient  :RECT
;
mov     wc.cbSize,sizeof WNDCLASSEX
mov     wc.style,CS_VREDRAW+CS_HREDRAW+CS_DBLCLKS\
        +CS_BYTEALIGNCLIENT+CS_BYTEALIGNWINDOW
mov     wc.lpfnWndProc,offset WndProc
mov     wc.cbClsExtra,NULL
mov     wc.cbWndExtra,NULL
push    hInst
pop     wc.hInstance
mov     wc.hbrBackground,COLOR_WINDOW+1
mov     wc.lpszMenuName,NULL
mov     wc.lpszClassName,offset szClassName
mov     wc.hIcon,0
invoke  LoadCursor,NULL,IDC_ARROW
mov     wc.hCursor,eax
mov     wc.hIconSm,0
;
invoke  RegisterClassEx,addr wc
mov     Wwd,600
mov     Wht,400
mov     Wtx,10
mov     Wty,10
invoke  CreateWindowEx,
            WS_EX_ACCEPTFILES+WS_EX_APPWINDOW,
            addr szClassName,
            addr szDisplayName,
            WS_OVERLAPPEDWINDOW+WS_VISIBLE,
            Wtx,Wty,Wwd,Wht,
            NULL,NULL,hInst,NULL
mov     hWnd,eax
invoke  LoadMenu,hInst,600
invoke  SetMenu,hWnd,eax
invoke  GetClientRect,hWnd,addr rectClient
invoke  CreateWindowEx,
            WS_EX_ACCEPTFILES+WS_EX_CLIENTEDGE,
            addr szEditClass,
            NULL,
            WS_CHILD+WS_VISIBLE+WS_HSCROLL+WS_VSCROLL\
            +ES_MULTILINE+ES_AUTOHSCROLL+ES_AUTOHSCROLL,
            rectClient.left,
            rectClient.top,
            rectClient.right,
            rectClient.bottom,
            hWnd,
            0,hInst,0
mov     hWndEdit,eax
StartLoop:
    invoke  GetMessage,addr msg,NULL,0,0
    cmp     eax,0
    je      ExitLoop
    invoke  TranslateMessage,addr msg
    invoke  DispatchMessage,addr msg
    jmp     StartLoop
ExitLoop:
    mov     eax,msg.wParam
    ret
WinMain endp

;-----------------WndProc------------------
WndProc proc hWin:dword,
        uMsg    :dword,
        wParam  :dword,
        lParam  :dword
.if uMsg == WM_COMMAND
    .if wParam == 1000
        invoke  FileOpen,addr szOpenFileName
    .elseif wParam == 1100
        invoke  MessageBox,hWin,addr TheText,addr szDisplayName,MB_YESNO
        .if eax ==IDNO
            mov eax,0
            ret
         .endif
         invoke FileSave,addr szSaveFileName
     .elseif wParam == 1900
        invoke  MessageBox,hWin,addr AboutMsg,addr szDisplayName,MB_OK
     .endif
.elseif uMsg == WM_DESTROY
    invoke PostQuitMessage,NULL
.else
    invoke DefWindowProc,hWin,uMsg,wParam,lParam
    ret
.endif
    mov     eax,0
    ret
WndProc endp

;----------------FileOpen proc--------------------
FileOpen proc lpszFileName:dword
    local   nBytesToRead:dword
    local   nBytesRead:dword
    local   hFile:dword
;
invoke  CreateFile,
            lpszFileName,
            GENERIC_READ,
            FILE_SHARE_READ,
            NULL,
            OPEN_EXISTING,
            NULL,
            NULL
cmp     eax,INVALID_HANDLE_VALUE
jz      error
mov     hFile,eax
invoke  GetFileSize,hFile,NULL
cmp     eax,INVALID_HANDLE_VALUE
jz      error
mov     hFile,eax
invoke  GetFileSize,hFile,NULL
cmp     eax,MAXSIZE
ja      error_c
mov     nBytesToRead,eax
invoke  ReadFile,
            hFile,
            addr Buf,
            nBytesToRead,
            addr nBytesRead,
            NULL
test    eax,eax
jz      error_c
mov     eax,nBytesToRead
mov     Buf[eax],0
invoke  CloseHandle,hFile
invoke  SendMessage,hWndEdit,WM_SETTEXT,0,addr Buf
mov     eax,1
ret
error_c:
invoke  CloseHandle,hFile
error:
invoke  MessageBox,hWnd,addr szFileError,addr szError,MB_OK or MB_ICONWARNING
xor     eax,eax
ret
FileOpen endp

;----------------FileSave proc--------------------
FileSave proc lpszFileName:dword
local nBytesToWrite:dword
local nBytesWritten:dword
local hFile:dword
;
invoke  SendMessage,hWndEdit,WM_GETTEXTLENGTH,0,0
cmp     eax,MAXSIZE
ja      error
mov     nBytesToWrite,eax
inc     eax
invoke  SendMessage,hWndEdit,WM_GETTEXT,eax,addr Buf
invoke  CreateFile,
            lpszFileName,
            GENERIC_WRITE,
            FILE_SHARE_WRITE,
            NULL,
            CREATE_ALWAYS,
            FILE_ATTRIBUTE_ARCHIVE,
            NULL
cmp     eax,INVALID_HANDLE_VALUE
jz      error
mov     hFile,eax
invoke  WriteFile,
        hFile,
        addr Buf,
        nBytesToWrite,
        addr nBytesWritten,
        NULL
test    eax,eax
jz      error_c
invoke  CloseHandle,hFile
mov     eax,1
ret
error_c:
invoke  CloseHandle,hFile
error:
invoke  MessageBox,hWnd,addr szFileError,addr szError,MB_OK or MB_ICONWARNING
xor     eax,eax
ret
FileSave endp
;--------------------------------------------------------
end start



MichaelW

The source built OK for me. I copied the posted source, opened the MASM32 Quick Editor, pasted the source into it, saved it to a .asm file, then selected Assemble & Link from the Projects menu.
eschew obfuscation

Vortex

Hi zhoujian,

Try this one, it works fine :


\masm32\bin\ml /c /coff t386.asm
\masm32\bin\polink /SUBSYSTEM:WINDOWS t386.obj

PBrennick

Hi zhoujian,

By your attempt to use /z2, I assume you wish to add debugging code. You can accomplish this in the following manner:

Quote
\masm32\bin\ml.exe /c /coff /Zi /Zd t386.asm
\masm32\bin\link.exe /SUBSYSTEM:WINDOWS /DEBUG /DEBUGTYPE:CV t386.obj

Paul
The GeneSys Project is available from:
The Repository or My crappy website