启动时自动运行程序 [英] automatically run program on startup

查看:91
本文介绍了启动时自动运行程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个小程序,它将值及其相应的值插入到Windows注册表项中。



程序工作正常,但它没有插入值和它的对应值。



当我以管理员身份运行prog时,还有一件事就是RegSetValueEx()失败..但仍然只插入值而不是其数据。



请帮助我们找到问题。



我的代码如下:







I have written a small program which insert the value and its corresponding value into the Windows registry key.

Program is working fine but it is not inserting value and its corresponding value.

And one more thing when I run prog as an administrator RegSetValueEx() fails..but still inssert only the value not its data.

Please help for finding out the issue here.

My code is as follows..



#define WIN32_LEAN_AND_MEAN
#define WIN32_DEFAULT_LIBS
#ifndef _WIN32_WINNT
#define _WIN32_WINNT (0x0601)
#endif  /* _WIN32_WINNT */
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <tchar.h>
#include <unistd.h>
#include <stdbool.h>

#include<string.h>

    BOOL InstallRunOnStartup()
    {
      HKEY key;

      long result;
      BOOL ret = FALSE;
      LPTSTR val=L"12as3d12";
        LPTSTR a=L"zzz";

      TCHAR szBuf[20];


      result = RegOpenKeyEx(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", NULL, KEY_WRITE | KEY_WOW64_64KEY | KEY_SET_VALUE , &key);
      if (result == ERROR_SUCCESS)
      {

        printf("hi \n");
          if (RegSetValueEx(key, a, 0, REG_SZ,(LPBYTE)val, (DWORD)(lstrlen(val)+1) == ERROR_SUCCESS)){
            printf("success \n");
            ret = TRUE;
          }
            RegCloseKey(key);
      }
      return ret;
    }




    int main()
    {
        InstallRunOnStartup();
        getch();
    }

推荐答案

您正在混合使用Unicode和ANSI:



使用ANSI构建,您为 val 分配Unicode字符串到 char * 一个变量。



使用Unicode构建,您将ANSI字符串传递给 RegOpenKeyEx()



使用ANSI构建,您将值字符串和值的Unicode字符串传递给 RegSetValueEx()



使用Unicode构建时, RegSetValueEx() cbData 参数错误(总长度必须乘以sizeof(WHACR))。



解决方案:

使用Unicode设置dependend宏和函数:

You are mixing Unicode and ANSI:

With ANSI builds, you are assigning Unicode strings to char* for your val and a variables.

With Unicode builds, you are passing an ANSI string to RegOpenKeyEx().

With ANSI builds, you are passing Unicode strings for value name and value to RegSetValueEx().

With Unicode builds, the RegSetValueEx() cbData parameter is wrong (the total length must be multiplied with sizeof(WHACR)).

Solution:
Use Unicode setting dependend macros and functions:
LPTSTR val=_T("12as3d12");
LPTSTR a=_T("zzz");
RegOpenKeyEx(HKEY_CURRENT_USER, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run)", // ...
RegSetValueEx(key, a, 0, REG_SZ,(LPBYTE)val, (DWORD)(_tcslen(val)+1) * sizeof(TCHAR));


这篇关于启动时自动运行程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆