Win32汇编实现修改注册表设置自启动程序

若不是很熟悉Win32汇编代码的一些API,可以先写C语言,再将其进行转换。

具体代码实现

    .386
    .model flat,stdcall
    option casemap:none

include         windows.inc
include         user32.inc
includelib      user32.lib
include         kernel32.inc
includelib      kernel32.lib
include         advapi32.inc
includelib      advapi32.lib

    .data?
hKey   dd ?

    .const 
szRegOpenKeyError     db "RegOpenKeyEx error!",0
szRegSetValueError    db "RegSetValueEx error!",0
szRegRun              db "Software\Microsoft\Windows\CurrentVersion\Run",0
szModule              db "c:\Users\yurin\Desktop\auto.exe",0
szErrorCaption        db "Error",0
szRegKeyValue         db "Start",0
szSUCCESS             db "AutoRun SUCCESS!",0

    .code
strlen proc uses edi ebx,szMsg 
        xor eax,eax
        xor ebx,ebx
        mov edi,szMsg
        mov bl,byte ptr[edi]
        .while  bl != 0
            inc eax
            inc edi
            mov bl,byte ptr[edi]
        .endw
        ret
strlen endp

ErrorExit proc ErrorMsg 
        invoke MessageBox,NULL,ErrorMsg,offset szErrorCaption,MB_OK
        invoke ExitProcess,NULL 
ErrorExit endp

start:
    ;打开注册表启动项
    invoke RegOpenKeyEx,HKEY_CURRENT_USER,offset szRegRun,0,KEY_ALL_ACCESS,offset hKey
        .if eax != ERROR_SUCCESS
            invoke ErrorExit,offset szRegOpenKeyError
        .endif
        .if eax == ERROR_SUCCESS
            invoke strlen,offset szModule
            ;添加一个子Key,并设置值
            invoke RegSetValueEx,hKey,offset szRegKeyValue,0,REG_SZ,offset szModule,eax
            .if eax != ERROR_SUCCESS
                invoke ErrorExit,offset szRegSetValueError
            .endif
            ;关闭注册表
            invoke RegCloseKey,hKey
        .endif
    ;测试是否成功自启动
    invoke MessageBox,NULL,offset szSUCCESS,offset szRegKeyValue,MB_OK
    invoke ExitProcess,NULL
end start

参考C语言

#include <windows.h>
#include <stdio.h>
int main()
{
    
    	
//找到系统的启动项 	
char *szSubKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";	
char *szModule ="c:\Users\yurin\Desktop\auto.exe";	
HKEY hKey;		
//打开注册表启动项 	
if(RegOpenKeyEx(HKEY_CURRENT_USER, szSubKey, 0, KEY_ALL_ACCESS, &hKey)== ERROR_SUCCESS)	
{
    
    
//添加一个子Key,并设置值,"Mytest"并不一定是应用程序名字(不加后缀.exe) ,可以自己设置;	
RegSetValueEx(hKey, "Mytest", 0, REG_SZ, (BYTE *)szModule, strlen(szModule));
		//关闭注册表	
RegCloseKey(hKey);	
}	
else {
    
    return -1;}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44029550/article/details/104099266