Visualizzazione dei risultati da 1 a 9 su 9
  1. #1
    Utente di HTML.it
    Registrato dal
    Nov 2013
    Messaggi
    98

    [C++] Parametri RegSetValueExW

    Sto sviluppando un programma con autorun. Mi sono bloccato però con la funzione che crea la chiave di registro. Come ultimo parametro della funzione sottostante, cosa dovrei mettere?

    codice:
    RegSetValueExW(hKey, RegValue,0,REG_SZ, (BYTE*)"C:\\Windows\\prova.exe", ???);

  2. #2
    Utente di HTML.it L'avatar di Alex'87
    Registrato dal
    Aug 2001
    residenza
    Verona
    Messaggi
    5,802
    SpringSource Certified Spring Professional | Pivotal Certified Enterprise Integration Specialist
    Di questo libro e degli altri (blog personale di recensioni libri) | ​NO M.P. TECNICI

  3. #3
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Appunto ...

    cbData [in]The size of the information pointed to by the lpData parameter, in bytes. If the data is of type REG_SZ, REG_EXPAND_SZ, or REG_MULTI_SZ, cbData must include the size of the terminating null character or characters.
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  4. #4
    Utente di HTML.it
    Registrato dal
    Nov 2013
    Messaggi
    98
    Quote Originariamente inviata da oregon Visualizza il messaggio
    Appunto ...

    cbData [in]The size of the information pointed to by the lpData parameter, in bytes. If the data is of type REG_SZ, REG_EXPAND_SZ, or REG_MULTI_SZ, cbData must include the size of the terminating null character or characters.
    Ho visto, il problema è che non capisco cosa voglia dire. Da cosa è determinato il tipo di REG?

  5. #5
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Cerca con google che tipi sono

    REG_SZ
    REG_EXPAND_SZ
    REG_MULTI_SZ
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  6. #6
    In buona sostanza è il conteggio dei byte necessari a memorizzare la stringa(compreso il carattere terminatore '\0').

    Puoi ottenerlo così:
    codice:
    (_tcslen(data) + 1) * sizeof(TCHAR)
    Esempio:
    codice:
    #include "windows.h"
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        LPCTSTR RegValue = TEXT("myKey");
        LPCTSTR data = TEXT("C:\\Windows\\prova.exe");
        HKEY hKey;
        LPCTSTR sk = TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
    
        LONG Res = RegOpenKeyEx(HKEY_CURRENT_USER, sk, 0, KEY_ALL_ACCESS , &hKey);
    
        if ( Res != ERROR_SUCCESS )
        {
            printf("Errore nell'apertura della chiave del registro.\n");
            return -1;
        }
    
        Res = RegSetValueEx(hKey, RegValue, 0, REG_SZ, (LPBYTE)data, (_tcslen(data) + 1) * sizeof(TCHAR));
    
        if ( Res != ERROR_SUCCESS )
        {
            printf("Errore nella scrittura dei dati nella chiave del registro.\n");
            return -1;
        }
    
        return 0;
    }

  7. #7
    Utente di HTML.it
    Registrato dal
    Nov 2013
    Messaggi
    98
    Quote Originariamente inviata da Vincenzo1968 Visualizza il messaggio
    In buona sostanza è il conteggio dei byte necessari a memorizzare la stringa(compreso il carattere terminatore '\0').

    Puoi ottenerlo così:
    codice:
    (_tcslen(data) + 1) * sizeof(TCHAR)
    Esempio:
    codice:
    #include "windows.h"
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        LPCTSTR RegValue = TEXT("myKey");
        LPCTSTR data = TEXT("C:\\Windows\\prova.exe");
        HKEY hKey;
        LPCTSTR sk = TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
    
        LONG Res = RegOpenKeyEx(HKEY_CURRENT_USER, sk, 0, KEY_ALL_ACCESS , &hKey);
    
        if ( Res != ERROR_SUCCESS )
        {
            printf("Errore nell'apertura della chiave del registro.\n");
            return -1;
        }
    
        Res = RegSetValueEx(hKey, RegValue, 0, REG_SZ, (LPBYTE)data, (_tcslen(data) + 1) * sizeof(TCHAR));
    
        if ( Res != ERROR_SUCCESS )
        {
            printf("Errore nella scrittura dei dati nella chiave del registro.\n");
            return -1;
        }
    
        return 0;
    }
    Ho alcune domande.
    Perché "tmain" e non "main"?
    "LPCTSTR" cosa indica?
    Perché il percorso è preceduto da "TEXT"?

  8. #8
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    _tmain is a Microsoft extension. If Unicode is enabled, is compiled as wmain, and otherwise as main.

    LPCTSTR
    = Long Pointer to a Const TCHAR STRing

    TEXT macro Identifies a string as Unicode when UNICODE is defined by a preprocessor directive during compilation. Otherwise, the macro identifies a string as an ANSI string.
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  9. #9
    Utente di HTML.it
    Registrato dal
    Nov 2013
    Messaggi
    98
    Risolto, si può chiudere

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.