Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    [VC++] String vs string

    Sto portando una parte di codice da .NET a C++ "standard", ma non sono molto esperto del primo...

    Questo è il codice che ho:

    codice:
    PBYTE PropertyValueBuffer;
    DWORD dwRegSize;
    
    //...
    
    PropertyValueBuffer = (BYTE *) malloc (dwRegSize);
    
    //...
    
    //PropertyValueBuffer gets filled with a 
    //REG_MULTI_SZ (array of null terminated strings).
    //To find a device, we only care about the very first string in the
    //buffer, which will be the "device ID".
    
    String^ DeviceIDFromRegistry = gcnew String((char *)PropertyValueBuffer);
    L'unico punto dolente è l'ultima riga... io vorrei sostituirla con qualcosa del genere:

    codice:
    string DeviceIDFromRegistry = ...
    Qualcuno potrebbe darmi un suggerimento?

    Grazie in anticipo!

  2. #2
    Utente di HTML.it L'avatar di Stoicenko
    Registrato dal
    Feb 2004
    Messaggi
    2,254
    codice:
    string* DeviceIDFromRegistry = new string((char *)PropertyValueBuffer);
    prova così

  3. #3
    Grazie, così è a posto, non avevo inteso che fosse così facile!

  4. #4
    Ricordati però del delete quando non ti serve più la stringa, o se la stringa serve solo nella funzione corrente evita del tutto l'uso di new. Dico questo perché traducendo codice .NET è facile dimenticarsi di aggiungere i delete, visto che in managed C++ non servono.
    Amaro C++, il gusto pieno dell'undefined behavior.

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 © 2024 vBulletin Solutions, Inc. All rights reserved.