Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1054050
  • 博文数量: 50
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 2037
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-05 08:03
文章分类
文章存档

2011年(1)

2010年(3)

2009年(17)

2008年(29)

我的朋友

分类:

2008-03-31 20:35:38

将程序资源转为16进制供程序调用。
 

.586
.model flat, stdcall
option casemap :none


include        windows.inc
include        user32.inc
include        kernel32.inc
include        masm32.inc
include comdlg32.inc
include shell32.inc
include comctl32.inc

includelib    user32.lib
includelib    kernel32.lib
includelib    masm32.lib
includelib comdlg32.lib
includelib shell32.lib
includelib comctl32.lib
 
IDD_Main equ 1000
IDC_FilePath equ 1002
IDC_Open equ 1003
IDC_VarName equ 1007
IDC_OutputName equ 1008
IDC_Save equ 1009
IDC_asm equ 1010
IDC_vc equ 1011
ico equ 2001
IDC_Start equ 1013
IDC_vb equ 1014
IDC_delphi equ 1015
IDM_About equ 10002
IDM_Exit equ 10003
IDM_Context equ 10004

DlgProc     proto        :DWORD,:DWORD,:DWORD,:DWORD
AllocEx     proto     :DWORD,:DWORD
Concat     proto     :DWORD,:DWORD
Buf        STRUCT
        p    dd    ?
        l    dd    ?
Buf        ENDS
CTEXT MACRO y:VARARG
        LOCAL sym
                CONST segment
                ifidni <y>,<>
                sym db 0
                else
                sym db y,0
                endif
                CONST ends
                exitm <offset sym>
ENDM
m2m        MACRO    M1,M2
        push    M2
        pop    dword ptr M1
        ENDM
return        MACRO    Arg
        mov    eax,Arg
        ret
        ENDM


.data
ofn OPENFILENAME <>
n        db    0
style        db    0    
szTagetName db 40 dup(0)
VarName db 40 dup(0)

.data?
ProgPath db 256 dup(?)
AppPath db 1024 dup (?)
BufSrc        Buf    <>    
BufTgt        Buf    <>    
hFile        dd    ?    
hTgt        dd    ?    
hInstance    dd    ?
hDlg dd ?
hMenu dd ?
hSubMenu1 dd ?
hSubMenu2 dd ?
hSubMenu3 dd ?
nRead        dd    ?    

.code
Start:    
        invoke GetModuleHandle, NULL
        mov hInstance,eax
        invoke DialogBoxParam, hInstance, IDD_Main, NULL, addr DlgProc, NULL
        invoke ExitProcess,eax     
DlgProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
        push hWnd
        pop hDlg
    .if uMsg == WM_INITDIALOG
        invoke GetWindowLong,hWnd,GWL_EXSTYLE
        or eax,WS_EX_LAYERED
        invoke    LoadIcon,hInstance,ico
     invoke    SendMessage,hWnd,WM_SETICON,1,eax
                invoke LoadMenu, hInstance, CTEXT("MyMenu")
                mov hMenu , eax
                invoke GetSubMenu, hMenu,0
                mov hSubMenu1, eax
                invoke GetSubMenu, hMenu,1
                mov hSubMenu2, eax
                invoke GetSubMenu, hMenu,2
                mov hSubMenu3, eax
                invoke    CheckDlgButton,hWnd,IDC_asm,BST_CHECKED
                invoke GetDlgItemText,hWnd,IDC_VarName,addr VarName,sizeof VarName
    .elseif uMsg==WM_COMMAND
        mov eax,wParam
        .if ax==IDC_Open
            call GetFilePath
            invoke SetDlgItemText,hWnd,IDC_FilePath,addr AppPath
        .elseif ax==IDC_Start
            call Covert
        .elseif ax==IDC_Save
         call SaveAs
        .elseif ax==IDM_Context            
            invoke MessageBox,NULL,CTEXT("这个小工具可以将任何文件转成16进制数据格式以供调用。") ,CTEXT("说明"),MB_OK
        .elseif ax==IDM_About
         invoke MessageBox,NULL,CTEXT("-数据格式转换工具 V0.1- ",13,"-== laomms 2007.7 ==--") ,CTEXT("关于"),MB_OK
        .elseif ax==IDM_Exit
         invoke    EndDialog,hWnd,0
        .endif
        .elseif uMsg==WM_CONTEXTMENU
            mov eax, lParam         
                and eax, 0ffffh
                mov ebx, lParam
                shr ebx, 16
                invoke TrackPopupMenu, hSubMenu1, TPM_LEFTALIGN, eax, ebx, 0, hWnd, 0
                invoke TrackPopupMenu, hSubMenu2, TPM_LEFTALIGN, eax, ebx, 0, hWnd, 0
                invoke TrackPopupMenu, hSubMenu3, TPM_LEFTALIGN, eax, ebx, 0, hWnd, 0
    .elseif    uMsg== WM_CLOSE
        invoke EndDialog,hWnd,0
    .endif
    xor eax,eax
    ret
DlgProc endp
GetFilePath proc
                mov ofn.lStructSize, sizeof ofn
                push hDlg
                pop ofn.hwndOwner
                push hInstance
                pop ofn.hInstance
                lea eax, offset AppPath
                mov ofn.lpstrFile, eax
                mov ofn.nMaxFile, offset AppPath
                mov ofn.Flags, OFN_FILEMUSTEXIST + OFN_PATHMUSTEXIST
                mov ofn.lpstrFilter, CTEXT("所有文件",0,"*.*",0 , 0)
                mov ofn.lpstrTitle, CTEXT("请打开一个PE可执行文件…")
                invoke GetOpenFileName, addr ofn
                .if !eax
                        invoke MessageBox,hDlg,CTEXT("打开程序错误!"),CTEXT("提示"),MB_OK
                .endif
                ret
GetFilePath endp
Covert proc
                invoke GetDlgItemText,hDlg,IDC_FilePath,addr AppPath,sizeof AppPath
                .if eax==0
                    invoke MessageBox,hDlg,CTEXT("请选择源文件!"),CTEXT("提示"),MB_OK
                    ret
                .endif
                invoke GetDlgItemText,hDlg,IDC_VarName,addr VarName,sizeof VarName
                .if eax==0
                    invoke MessageBox,hDlg,CTEXT("还未设置变量名称!"),CTEXT("提示"),MB_OK
                    ret
                .endif
                invoke GetDlgItemText,hDlg,IDC_OutputName,addr szTagetName,sizeof szTagetName
                .if eax==0
                    invoke MessageBox,hDlg,CTEXT("请设置输出的文件名!"),CTEXT("提示"),MB_OK
                    ret
                .endif
             invoke    IsDlgButtonChecked,hDlg,IDC_asm
             .if    eax == BST_CHECKED
              mov    style,0
                 jmp    @f
             .endif    
             invoke    IsDlgButtonChecked,hDlg,IDC_vc    
             .if    eax == BST_CHECKED
              mov    style,1
                 jmp    @f
             .endif    
             invoke    IsDlgButtonChecked,hDlg,IDC_vb    
             .if    eax == BST_CHECKED
              mov    style,2
                 jmp    @f
             .endif    
             invoke    IsDlgButtonChecked,hDlg,IDC_delphi    
             .if    eax == BST_CHECKED
              mov    style,3
                 jmp    @f
             .endif    

@@:        mov    eax,offset VarName
    @@lp:    inc    eax
        cmp    byte ptr [eax],0
        jne    @@lp
        sub    eax,offset VarName
        push    eax
        invoke     CreateFile,addr AppPath,GENERIC_READ,0,0,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0
        .if eax==INVALID_HANDLE_VALUE
            invoke MessageBox,hDlg,CTEXT("打开错误!"),CTEXT("提示"),MB_OK
            ret
        .endif
        mov    hFile,eax
        invoke     GetFileSize,eax,0
        .if eax==0
            invoke MessageBox,hDlg,CTEXT("源文件内容为空!"),CTEXT("提示"),MB_OK
            ret
        .endif
            mov    BufSrc.l,eax
        invoke     AllocEx,ADDR BufSrc,CTEXT("源文件")
        mov    ecx,eax
        and    ecx,0fh    ;剩余部分
        cmp    ecx,0
        je    @@skip
        lea    ecx,[ecx*4+ecx+3]
    @@skip:    shr    eax,4    ;栏目数
        imul    eax,84    
        add    eax,ecx    
        pop    ecx    ;变量名长度
        inc    ecx    
        add    eax,ecx    ;总长度
        mov    BufTgt.l,eax        
        invoke     AllocEx,ADDR BufSrc,CTEXT("源文件")    
        invoke     ReadFile,hFile,BufSrc.p,BufSrc.l,ADDR nRead,0
        .if eax==INVALID_HANDLE_VALUE
            invoke MessageBox,hDlg,CTEXT("读取文件错误!"),CTEXT("提示"),MB_OK
            ret
        .endif
        invoke     CreateFile,addr szTagetName,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0
        .if eax==INVALID_HANDLE_VALUE
            invoke MessageBox,hDlg,CTEXT("创建错误"),CTEXT("提示"),MB_OK
            ret
        .endif
        mov    hTgt,eax
     ;processing the file
        push    edi
        mov    edx,BufTgt.p
        mov    ebx,offset VarName
        movzx    edi,style
nextvarch:    mov    al,byte ptr [ebx]
        cmp    al,0
        je    vartab
        mov    byte ptr [edx],al
        inc    edx
        inc    ebx
        jmp    nextvarch
vartab:        mov    byte ptr [edx],20h
        mov    ecx,nRead
        mov    ebx,BufSrc.p ;读出的内容
        inc    edx
NextLine::    mov    word ptr [edx],"bd" ;db
        inc    edx
        inc    edx
        mov    byte ptr [edx],9
        inc    edx
NextByte:    movzx    ax,byte ptr [ebx] ;逐个取字符串
        inc    ebx ;取下一位
        ror    ax,4
        ror    ah,4
        add    ax,"00"
        cmp    al,":"
        jb    DontAdjAL
        add    al,7
DontAdjAL:    cmp    ah,":"
        jb    DontAdjAH
        add    ah,7
DontAdjAH:    cmp    edi,0
        je    asm
        cmp    edi,1
        je    vc
        cmp    edi,2
        je    vb
        cmp    edi,3
        je    delphi
    vc:    mov    word ptr [edx],"x0"
     jmp do
    vb: mov    word ptr [edx],"H&"
     jmp do
    do:     inc    edx
        inc    edx
        jmp    X1
    delphi: mov    word ptr [edx],"$"
     inc    edx
        jmp    X1
    asm:    mov    byte ptr [edx],"0"
        inc    edx
    X1:    mov    word ptr [edx],ax
        inc    edx
        inc    edx
        cmp    edi,0
        jne    X2
        mov    byte ptr [edx],"h"
        inc    edx
    X2:    inc    n
        cmp    n,16
        je    newline
        mov    byte ptr [edx],","
        inc    edx
        loop    NextByte
        dec    edx
        jmp    Done
newline:    mov    n,0
        mov    word ptr [edx],0A0Dh ;
        inc    edx
        inc    edx
        loop    Line
Line :: jmp NextLine
        dec    edx
        dec    edx
Done:        sub    edx,BufTgt.p
        pop    edi
     ;writing the file
        invoke     WriteFile,hTgt,BufTgt.p,edx,ADDR nRead,0
        .if eax==INVALID_HANDLE_VALUE
            invoke MessageBox,hDlg,CTEXT("写入错误!"),CTEXT("提示"),MB_OK
            ret
        .endif
        invoke CloseHandle,hTgt
Exit:        ret
Covert endp
SaveAs        proc
LOCAL @stOF:OPENFILENAME

        invoke    RtlZeroMemory,addr @stOF,sizeof @stOF
        mov    @stOF.lStructSize,sizeof @stOF
        push    hDlg
        pop    @stOF.hwndOwner
        mov    @stOF.lpstrFilter,CTEXT("文本文件 (*.txt)",0,'*.txt',0)
        mov    @stOF.lpstrFile,offset szTagetName
        mov    @stOF.nMaxFile,MAX_PATH
        mov    @stOF.Flags,OFN_PATHMUSTEXIST
        mov    @stOF.lpstrDefExt,CTEXT('txt',0)
        mov    @stOF.lpstrTitle,CTEXT("保存为文本文件")
        invoke    GetSaveFileName,addr @stOF
        .if    eax
            invoke SetDlgItemText,hDlg,IDC_OutputName,addr szTagetName
                        ret
        .else
            invoke MessageBox,NULL,CTEXT("设置路径错误!!!"),CTEXT("提示"),MB_OK            
        .endif
        mov    eax,FALSE
        ret

SaveAs        endp
Concat        proc    dest,src
        pushfd
        push    ecx
        push    esi
        push    edi
     ;looking for 0
        xor    eax,eax
        xor    ecx,ecx
        mov    edi,dest
        mov    esi,src
        cld
        repne    scasb
     ;storing src
    @@:    lodsb
        stosb
        cmp    al,0
        jne    @b
        mov    eax,edi
        dec    eax
        pop    edi
        pop    esi
        pop    ecx
        popfd
        ret
Concat        endp
AllocEx        proc    pBuf,pMsg
        push    eax
        push    ecx
        mov    eax,pBuf
        push    eax
        add    eax,4
        mov    eax,[eax]    ;size member of Buf structure
        invoke     VirtualAlloc,0,eax,MEM_RESERVE or MEM_COMMIT,PAGE_READWRITE
        pop    ecx
        .if eax==0
            invoke MessageBox,NULL,CTEXT("不能分配内存"),CTEXT("提示"),MB_OK
            ret
        .endif
        mov    [ecx],eax    ;ptr member is set
        pop    ecx
        pop    eax
        ret
AllocEx        endp
end        Start

 

 

#define IDR_MENU 10000
#define ico 2001
#define IDD_Main 1000
#define IDC_GRP1 1001
#define IDC_GRP2 1004
#define IDC_GRP3 1012
#define IDC_FilePath 1002
#define IDC_Open 1003
#define IDC_STC1 1005
#define IDC_STC2 1006
#define IDC_VarName 1007
#define IDC_OutputName 1008
#define IDC_Save 1009
#define IDC_asm 1010
#define IDC_vc 1011
#define IDC_Start 1013
#define IDC_vb 1014
#define IDC_delphi 1015
#define IDM_About 10002
#define IDM_Exit 10003
#define IDM_Context 10004
ico ICON DISCARDABLE "ico.ico"
1 MANIFEST DISCARDABLE "XPStyle.manifest"
IDD_Main DIALOGEX 6,6,215,103
CAPTION "数据格式转换工具 V1.0"
FONT 8,"MS Sans Serif",0,0
STYLE 0x10CE0800
BEGIN
  CONTROL "打开",IDC_GRP1,"Button",0x50000007,2,1,210,32
  CONTROL "",IDC_FilePath,"Edit",0x50010080,8,12,152,11,0x00000200
  CONTROL "浏 览",IDC_Open,"Button",0x50018000,170,12,38,11
  CONTROL "设置",IDC_GRP2,"Button",0x50000007,2,33,210,41
  CONTROL "输出变量名:",IDC_STC1,"Static",0x50000000,8,46,42,11
  CONTROL "输出文件名:",IDC_STC2,"Static",0x50000000,8,59,42,9
  CONTROL "",IDC_VarName,"Edit",0x50010080,58,44,110,11,0x00000200
  CONTROL "",IDC_OutputName,"Edit",0x50010080,58,57,110,11,0x00000200
  CONTROL "路径",IDC_Save,"Button",0x50018000,174,57,36,11
  CONTROL "ASM 格式",IDC_asm,"Button",0x50010009,4,84,44,13
  CONTROL "C/C++ 格式",IDC_vc,"Button",0x50010009,52,83,50,15
  CONTROL "风格",IDC_GRP3,"Button",0x50000007,2,75,212,26
  CONTROL "转换",IDC_Start,"Button",0x50018000,174,44,36,11
  CONTROL "VB格式",IDC_vb,"Button",0x50010009,108,84,50,11
  CONTROL "DELPHI格式",IDC_delphi,"Button",0x50010009,154,86,54,9
END
MyMenu MENUEX
BEGIN
  POPUP "File"
  BEGIN
    MENUITEM "说明",IDM_Context
    MENUITEM "关于",IDM_About
    MENUITEM "退出",IDM_Exit
  END
END
1 24 "XPStyle.manifest"

阅读(1521) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~