Hey, I have a slight problem. I have managed to figure out how to copy and move files to XP's start up folder, and in some accounts Vista's startup folder. Now with Vista I can successfully copy and move the file to my own user account, but how do I move it to a another users account if I don't know the name?

CopyFile("welcome.exe","C:\\Users\\miclo\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\welcome.exe",0);

That works fine, but like I said, how do I do it if I don't know the users name? I have googled and found very few places that mention something about it but I'm finding it very hard to understand.

I hope you understand what I am trying to do, if not as a small example:

C:\\Users\\*\\AppData\\Roaming\\
C:\\Users\\%s\\AppData\\Roaming\\

Kind of like wildcards and matches, but what actually works in C++, this is a win32 app just in case you need to know, not an MFC.

Dani AI

Generated

raised a common Vista-era problem: trying to drop an executable into another user's Startup folder when the user name is unknown. Directly touching other users' profiles is brittle on Vista+ (UAC, ACLs and roaming profiles) and is usually avoidable. and already pointed toward per-user techniques and the simpler "install-for-all-users-and-check-at-runtime" idea; the latter is generally the safest and most maintainable.

Three practical options, with trade-offs:

  • Install once for all accounts (machine-wide startup) and let the program decide at run time whether the current account is a "pro" member. This avoids enumerating/altering other profiles and reduces permission problems. The Known Folder APIs are the right way to locate the common Startup folder programmatically: see Known Folder IDs and SHGetKnownFolderPath.

  • If existing user profiles must be targeted, enumerate profiles (ProfileList or account enumeration), obtain a token or the profile path for each account, then query that account's Startup known folder and copy the file. This requires elevation, correct impersonation, and careful ACL handling; see GetUserProfileDirectory and the SHGetKnownFolderPath guidance above.

  • Use a Scheduled Task that runs at logon (configured for all users or a group). Tasks can be created by an elevated installer and avoid repeatedly modifying profile folders.

Tiny sketch (concept only) showing the SHGetKnownFolderPath pattern with a user token:

HANDLE hToken = /* token for the target user (impersonation / LogonUser) */;
PWSTR pPath = NULL;
HRESULT hr = SHGetKnownFolderPath(FOLDERID_Startup, 0, hToken, &pPath);
if (SUCCEEDED(hr)) {
    // copy the EXE into pPath
    CoTaskMemFree(pPath);
}

Important cautions: modifying other users' profiles without consent affects privacy and can break updates; UAC/elevation is required on Vista and later; an installer (MSI or similar) is the correct place to add machine-wide startup entries. For background on elevation and UAC implications see the User Account Control overview.

Recommended Answers

All 3 Replies

Hey, I have a slight problem. I have managed to figure out how to copy and move files to XP's start up folder, and in some accounts Vista's startup folder. Now with Vista I can successfully copy and move the file to my own user account, but how do I move it to a another users account if I don't know the name?

CopyFile("welcome.exe","C:\\Users\\miclo\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\welcome.exe",0);

That works fine, but like I said, how do I do it if I don't know the users name? I have googled and found very few places that mention something about it but I'm finding it very hard to understand.

I hope you understand what I am trying to do, if not as a small example:

C:\\Users\\*\\AppData\\Roaming\\
C:\\Users\\%s\\AppData\\Roaming\\

Kind of like wildcards and matches, but what actually works in C++, this is a win32 app just in case you need to know, not an MFC.

To get user name:

char COMPNAME[MAX_COMPUTERNAME_LENGTH +1];
DWORD cbCOMPNAME = sizeof(compNAME);

GetComputerName(COMPNAME, &CBCOMPNAME);

or just add it here: HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run\\YourProjectName

also to get AppData path its as simple as char *appdata = getenv("APPDATA")

Why are you trying to do this? I hope you aren't trying to write a virus :\

Thanks for the help.

Also no, I am not writing a virus. I'm just trying to make a few small exclusive programs for our pro account members and one idea I had was for a simple welcome speech message, and while I don't know that much about C++ yet, I thought it would be a good idea to add to starup so they can easily remove it. Plus it helps me learn more.

Thanks again for the help.

It might be easier to create your program for everyone, and when it loads, check to see if the current user is a pro account. If he/she is, then display the message, otherwise exit. Just put the check before the main form appears, so nothing pops on the screen if you don't want to be seen.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.