Click here to Skip to main content
16,010,650 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralCComVariant to c-style string Pin
suresh_sathya14-Oct-02 20:29
suresh_sathya14-Oct-02 20:29 
GeneralRe: CComVariant to c-style string Pin
alex.barylski14-Oct-02 20:55
alex.barylski14-Oct-02 20:55 
GeneralRe: CComVariant to c-style string Pin
Paul M Watt14-Oct-02 20:58
mentorPaul M Watt14-Oct-02 20:58 
QuestionChange wnd style of a dialog?!?!? Pin
Daniel Strigl14-Oct-02 20:18
Daniel Strigl14-Oct-02 20:18 
AnswerRe: Change wnd style of a dialog?!?!? Pin
alex.barylski14-Oct-02 20:37
alex.barylski14-Oct-02 20:37 
AnswerRe: Change wnd style of a dialog?!?!? Pin
Paul M Watt14-Oct-02 21:02
mentorPaul M Watt14-Oct-02 21:02 
GeneralRe: Change wnd style of a dialog?!?!? Pin
adamUK15-Oct-02 1:51
adamUK15-Oct-02 1:51 
QuestionHow to set File/Dir owner (on NTFS)? Pin
Qiang.Fu14-Oct-02 18:59
Qiang.Fu14-Oct-02 18:59 
<code>
include "stdafx.h"
#include <iostream.h>
#include <windows.h>
#include <tchar.h>

#define ErrorHandler(s) _ErrorHandler(s, __FILE__, __LINE__)

void _ErrorHandler(LPCSTR lpszLocation, LPCTSTR lpszSrcFile, UINT nSrcLine)
{
LPVOID lpMsgBuf;
DWORD dwError = GetLastError();

FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPSTR) &lpMsgBuf,
0,
NULL );

// Process any inserts in lpMsgBuf.
cout<<"ERROR @ "<<lpszLocation<<" : "<<'('<<dwError<<')'<<(LPCSTR)lpMsgBuf;
cout<<"SOURCE @ Line = "<<nSrcLine<<", File = "<<lpszSrcFile<<'\n';

// Free the buffer.
LocalFree( lpMsgBuf );
}

BOOL SetFileOwner(LPCTSTR lpszFileName, LPCSTR lpszAccount, LPCSTR lpszGroup)
{
PSECURITY_DESCRIPTOR pSD;

PSID pSID, pSIDGroup;
DWORD cbSID = 1024;
LPSTR lpszDomain;
DWORD cchDomainName = 80;
PSID_NAME_USE psnuType;

// Initialize a security descriptor.

pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR,
SECURITY_DESCRIPTOR_MIN_LENGTH); // defined in WINNT.H
if (pSD == NULL) {
ErrorHandler("LocalAlloc");
goto Cleanup;
}

if (!InitializeSecurityDescriptor(pSD,
SECURITY_DESCRIPTOR_REVISION)) { // defined in WINNT.H
ErrorHandler("InitializeSecurityDescriptor");
goto Cleanup;
}

// Retrieve the SID for User.

pSID = (PSID) LocalAlloc(LPTR, cbSID);
psnuType = (PSID_NAME_USE) LocalAlloc(LPTR, 1024);
lpszDomain = (LPSTR) LocalAlloc(LPTR, cchDomainName);
if (pSID == NULL || psnuType == NULL ||
lpszDomain == NULL) {
ErrorHandler("LocalAlloc");
goto Cleanup;
}

if (!LookupAccountName((LPSTR) NULL, // local name
lpszAccount,
pSID,
&cbSID,
lpszDomain,
&cchDomainName,
psnuType)) {
ErrorHandler("LookupAccountName");
goto Cleanup;
}

if (!IsValidSid(pSID))
{
ErrorHandler("IsValidSid");
goto Cleanup;
}

// Sets the owner to the SD

if (!SetSecurityDescriptorOwner(pSD, pSID, FALSE)) { // not a default SID
ErrorHandler("SetSecurityDescriptorOwner");
goto Cleanup;
}

// Retrieve the SID for Group.
cbSID = 1024;

pSIDGroup = (PSID) LocalAlloc(LPTR, cbSID);
if (pSIDGroup == NULL) {
ErrorHandler("LocalAlloc");
goto Cleanup;
}

if (!LookupAccountName((LPSTR) NULL, // local name
lpszGroup,
pSIDGroup,
&cbSID,
lpszDomain,
&cchDomainName,
psnuType)) {

pSIDGroup = (PSID)LocalReAlloc((HLOCAL) pSIDGroup, cbSID, LPTR);
lpszDomain = (LPSTR) LocalReAlloc((HLOCAL) lpszDomain, cchDomainName, LPTR);
if (pSIDGroup == NULL || lpszDomain == NULL) {
ErrorHandler("LocalReAlloc");
goto Cleanup;
}

if (!LookupAccountName((LPSTR) NULL, // local name
lpszGroup,
pSIDGroup,
&cbSID,
lpszDomain,
&cchDomainName,
psnuType)) {
ErrorHandler("LookupAccountName");
goto Cleanup;
}
}

if (!IsValidSid(pSIDGroup))
{
ErrorHandler("IsValidSid");
goto Cleanup;
}

// Sets the group to the SD
if (!SetSecurityDescriptorGroup(pSD, pSIDGroup, FALSE)) { // not a default SID
ErrorHandler("SetSecurityDescriptorGroup");
goto Cleanup;
}

// Use the new SD as the file's security info.
if (!SetFileSecurity(lpszFileName, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION, pSD)) {
ErrorHandler("SetFileSecurity");
goto Cleanup;
}

return TRUE;

Cleanup:
FreeSid(pSID);
FreeSid(pSIDGroup);

if(pSD != NULL)
LocalFree((HLOCAL) pSD);
if(psnuType != NULL)
LocalFree((HLOCAL) psnuType);
if(lpszDomain != NULL)
LocalFree((HLOCAL) lpszDomain);

return FALSE;
}








int _tmain(int argc, TCHAR *argv[], TCHAR *envp[])
{
if (argc < 4) {
cout<<"ERROR @ main : Not enough parameters\n";
return 0;
}

TCHAR tszFileName[MAX_PATH];
char szUserName[256];
char szGroupName[256];

_tcscpy(tszFileName, argv[1]);

#if defined(_UNICODE) || defined(UNICODE)
if (!WideCharToMultiByte(CP_ACP, 0, argv[2], -1, szUserName, 256, NULL, NULL)) {
ErrorHandler("WideCharToMultiByte");
return 0;
}
if (!WideCharToMultiByte(CP_ACP, 0, argv[3], -1, szGroupName, 256, NULL, NULL)) {
ErrorHandler("WideCharToMultiByte");
return 0;
}
#else
strcpy(szUserName, argv[2]);
strcpy(szGroupName, argv[3]);
#endif

if (!SetFileOwner(tszFileName, szUserName, szGroupName)) {
cout<<"Operation Failed!\n";
return 0;
}

cout<<"Operation Succeeded!\n";
return 0;
}
</code>





=======================================================================
This program can set file and dir owner for "administrator"
or "administrators", but can't set for other user, else return
the "ERROR_INVALID_OWNER(1703L)"...
Why??? Please help me ~!!!
=======================================================================
AnswerRe: How to set File/Dir owner (on NTFS)? Pin
Anonymous15-Oct-02 22:22
Anonymous15-Oct-02 22:22 
QuestionHow to DrawText on top of child window? Pin
Hiusing14-Oct-02 18:39
Hiusing14-Oct-02 18:39 
AnswerRe: How to DrawText on top of child window? Pin
alex.barylski14-Oct-02 20:49
alex.barylski14-Oct-02 20:49 
GeneralUI Thread question Pin
RobJones14-Oct-02 16:55
RobJones14-Oct-02 16:55 
GeneralKnown bug.. Pin
Anonymous15-Oct-02 4:55
Anonymous15-Oct-02 4:55 
GeneralHELP PLZ Pin
B4u14-Oct-02 16:41
B4u14-Oct-02 16:41 
Generalfind of STL Pin
IMiracle14-Oct-02 15:23
IMiracle14-Oct-02 15:23 
GeneralRe: find of STL Pin
Chris Losinger14-Oct-02 15:41
professionalChris Losinger14-Oct-02 15:41 
GeneralRe: find of STL Pin
IMiracle14-Oct-02 16:20
IMiracle14-Oct-02 16:20 
GeneralRe: find of STL Pin
Chris Losinger14-Oct-02 16:25
professionalChris Losinger14-Oct-02 16:25 
GeneralRe: find of STL Pin
IMiracle14-Oct-02 19:43
IMiracle14-Oct-02 19:43 
GeneralRe: find of STL Pin
IMiracle15-Oct-02 1:34
IMiracle15-Oct-02 1:34 
GeneralRe: find of STL Pin
Chris Losinger15-Oct-02 4:18
professionalChris Losinger15-Oct-02 4:18 
Questionretrieving locations of icons/shortcuts on desktop? Pin
Anonymous14-Oct-02 12:46
Anonymous14-Oct-02 12:46 
AnswerRe: retrieving locations of icons/shortcuts on desktop? Pin
alex.barylski14-Oct-02 20:39
alex.barylski14-Oct-02 20:39 
QuestionCFormView - Mouse over, mouse out? Pin
dazinith14-Oct-02 11:12
dazinith14-Oct-02 11:12 
AnswerRe: CFormView - Mouse over, mouse out? Pin
valikac14-Oct-02 11:47
valikac14-Oct-02 11:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.