Click here to Skip to main content
16,011,804 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Member initialization Pin
David Fedolfi19-Jan-01 3:38
David Fedolfi19-Jan-01 3:38 
Generalshare folder Pin
derhackler19-Jan-01 1:51
derhackler19-Jan-01 1:51 
GeneralRe: share folder Pin
Ghazi H. Wadi19-Jan-01 4:27
Ghazi H. Wadi19-Jan-01 4:27 
GeneralRe: share folder Pin
derhackler19-Jan-01 5:15
derhackler19-Jan-01 5:15 
GeneralRe: share folder Pin
Ghazi H. Wadi19-Jan-01 7:18
Ghazi H. Wadi19-Jan-01 7:18 
GeneralRe: share folder Pin
derhackler19-Jan-01 11:19
derhackler19-Jan-01 11:19 
GeneralRe: share folder Pin
Ghazi H. Wadi23-Jan-01 9:27
Ghazi H. Wadi23-Jan-01 9:27 
GeneralRe: share folder Pin
Ghazi H. Wadi23-Jan-01 9:43
Ghazi H. Wadi23-Jan-01 9:43 
Hi,
I have tried the following code and was able to see the sharing.
This simple function will add sharing for the specified user on the specified machine
Hope it will help.
P.S in the following example the machine Name, and Domain was changed to protect the yada yada yada. Smile | :)
cheers
Alfadhly
// NetShareMFC.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "NetShareMFC.h"
#include < LM.h >
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define _UNICODE
#define RTN_OK 0
#define RTN_ERROR 13
//
#pragma comment ( lib, "Netapi32.lib")
CWinApp theApp;
using namespace std;
int CleanUp( PSECURITY_DESCRIPTOR pSid, PACL pAcl, BOOL bSuccess);
int  ShareFolder( LPCSTR szDirectory, LPCSTR szShareName, LPCSTR szUserName, LPCSTR szServer);
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;
	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		nRetCode = 1;
	}
	else
	{
	// Just Share the folder simple and clear
		cout << "Share folder returned " ;
		cout << 	ShareFolder( 
			"c:\\temp"  // folder  full path name
			, "alfadhly44$" // The share name 
			, "MYDOMAIN\\alfadhlyG" // who do we want to grant access to this directory
			,"\\\\MYMACHINE" // Which machine NULL if we want it for local machine
			) ; 
	cout << endl;
	}
	return nRetCode;
}
int ShareFolder( LPCSTR szDirectory, LPCSTR szShareName, LPCSTR szUserName, LPCSTR szServer)
{
    LPWSTR DirectoryToShare; 
    LPWSTR Sharename;
    LPWSTR Username;
    LPWSTR Server;
    PSID pSid = NULL;
    DWORD cbSid;
    WCHAR RefDomain[DNLEN + 1];
    DWORD cchDomain = DNLEN + 1;
    SID_NAME_USE peUse;
    SECURITY_DESCRIPTOR sd;
    PACL pDacl = NULL;
    DWORD dwAclSize;
    SHARE_INFO_502 si502;
    NET_API_STATUS nas;
    BOOL bSuccess = FALSE; // assume this function fails
    DWORD   dwString;
    // Convert the parameter to Unicode
    dwString = MultiByteToWideChar(CP_ACP, 0, szDirectory, -1, NULL, 0);
    DirectoryToShare = (PWSTR)LocalAlloc(LMEM_FIXED, dwString * sizeof(WCHAR));
    if(dwString == NULL)
	return -1;//OUT_OF_MEMORY;
    dwString = MultiByteToWideChar(CP_ACP, 0, szDirectory, -1, DirectoryToShare, dwString);
    if(dwString == 0)
	return -1;  //PARAMETER_INVALID_VALUE;
    // Convert the parameter to Unicode
    dwString = MultiByteToWideChar(CP_ACP, 0, szShareName, -1, NULL, 0);
    Sharename = (PWSTR)LocalAlloc(LMEM_FIXED, dwString * sizeof(WCHAR));
    if(dwString == NULL)
	return -1;//OUT_OF_MEMORY;
    dwString = MultiByteToWideChar(CP_ACP, 0, szShareName, -1, Sharename, dwString);
    if(dwString == 0)
	return -1;  //PARAMETER_INVALID_VALUE;
    dwString = MultiByteToWideChar(CP_ACP, 0, szUserName, -1, NULL, 0);
    Username = (PWSTR)LocalAlloc(LMEM_FIXED, dwString * sizeof(WCHAR));
    if(dwString == NULL)
	return -1;//OUT_OF_MEMORY;
    dwString = MultiByteToWideChar(CP_ACP, 0, szUserName, -1, Username, dwString);
    if(dwString == 0)
	return -1;  //PARAMETER_INVALID_VALUE;
    if (lstrlen(szServer)>0)
    {
	dwString = MultiByteToWideChar(CP_ACP, 0, szServer, -1, NULL, 0);
	Server = (PWSTR)LocalAlloc(LMEM_FIXED, dwString * sizeof(WCHAR));
	if(dwString == NULL)
	 return -1;//OUT_OF_MEMORY;
	dwString = MultiByteToWideChar(CP_ACP, 0, szServer, -1, Server, dwString);
	if(dwString == 0)
	 return -1;  //PARAMETER_INVALID_VALUE;
    }
    else
	Server = NULL; // local machine
	//
    // initial allocation attempt for Sid
#define SID_SIZE 96
    cbSid = SID_SIZE;
    pSid = (PSID)HeapAlloc(GetProcessHeap(), 0, cbSid);
    if(pSid == NULL) {
        TRACE("HeapAlloc error!\n");
        return RTN_ERROR;
    }
   // get the Sid associated with the supplied user/group name
    // force Unicode API since we always pass Unicode string
    if(!LookupAccountNameW(
        NULL,       // default lookup logic
        Username,   // user/group of interest from commandline
        pSid,       // Sid buffer
        &cbSid,     // size of Sid
        RefDomain,  // Domain account found on (unused)
        &cchDomain, // size of domain in chars
        &peUse
        )) {
        // if the buffer wasn't large enough, try again
        //
        if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
            pSid = (PSID)HeapReAlloc(GetProcessHeap(), 0, pSid, cbSid);
            if(pSid == NULL) {
                TRACE("HeapReAlloc error!\n");
                return CleanUp( pSid,  pDacl, FALSE);
            }
            cchDomain = DNLEN + 1;
            if(!LookupAccountNameW(
                NULL,       // default lookup logic
                Username,   // user/group of interest from commandline
                pSid,       // Sid buffer
                &cbSid,     // size of Sid
                RefDomain,  // Domain account found on (unused)
                &cchDomain, // size of domain in chars
                &peUse
                )) 
               {
                    TRACE("LookupAccountName error! (rc=%lu)\n", GetLastError());
                    return CleanUp( pSid,  pDacl, FALSE);
                }
        } else 
          {
            TRACE("LookupAccountName error! (rc=%lu)\n", GetLastError());
            return CleanUp( pSid,  pDacl, FALSE);
        }
    }
    //
    // compute size of new acl
    dwAclSize = sizeof(ACL) +
        1 * ( sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD) ) +
        GetLengthSid(pSid) ;
    // allocate storage for Acl
    pDacl = (PACL)HeapAlloc(GetProcessHeap(), 0, dwAclSize);
    if(pDacl == NULL) return CleanUp( pSid,  pDacl, FALSE);
    if(!InitializeAcl(pDacl, dwAclSize, ACL_REVISION))
        return CleanUp( pSid,  pDacl, FALSE);
    // grant GENERIC_ALL (Full Control) access
    if(!AddAccessAllowedAce(
        pDacl,
        ACL_REVISION,
        GENERIC_ALL,
        pSid
        )) return CleanUp( pSid,  pDacl, FALSE);
    if(!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION))
        return CleanUp( pSid,  pDacl, FALSE);
    if(!SetSecurityDescriptorDacl(&sd, TRUE, pDacl, FALSE)) {
        TRACE( "SetSecurityDescriptorDacl error! (rc=%lu)\n", GetLastError());
        return CleanUp( pSid,  pDacl, FALSE);
    }
    // setup share info structure
    si502.shi502_netname = (LPTSTR) Sharename;
    si502.shi502_type = STYPE_DISKTREE;
    si502.shi502_remark = NULL;
    si502.shi502_permissions = 0;
    si502.shi502_max_uses = SHI_USES_UNLIMITED;
    si502.shi502_current_uses = 0;
    si502.shi502_path = (LPTSTR) DirectoryToShare;
    si502.shi502_passwd = NULL;
    si502.shi502_reserved = 0;
    si502.shi502_security_descriptor = &sd;
    nas = NetShareAdd(
        (LPTSTR) Server,         // share is on local machine
        502,            // info-level
        (LPBYTE)&si502, // info-buffer
        NULL            // don't bother with parm
        );
    if(nas != NO_ERROR) {
        TRACE("NetShareAdd error! (rc=%lu)\n", nas);
        return CleanUp( pSid,  pDacl, FALSE);
    }
       return CleanUp( pSid,  pDacl, TRUE);
}
int CleanUp( PSECURITY_DESCRIPTOR pSid, PACL pAcl, BOOL bSuccess)
{
    if(pAcl != NULL)
        HeapFree(GetProcessHeap(), 0, pAcl);
    if(pSid != NULL)
        HeapFree(GetProcessHeap(), 0, pSid);
    if(!bSuccess) {
        return RTN_ERROR;
    }
    return RTN_OK;
}

GeneralRe: share folder Pin
derhackler23-Jan-01 11:26
derhackler23-Jan-01 11:26 
GeneralRe: share folder Pin
derhackler22-Jan-01 2:32
derhackler22-Jan-01 2:32 
Generalinvalid page fault Pin
P. S Thakur19-Jan-01 0:48
P. S Thakur19-Jan-01 0:48 
GeneralInvalid page fault in msvcrt.dll Pin
P. S Thakur19-Jan-01 0:45
P. S Thakur19-Jan-01 0:45 
GeneralRe: Invalid page fault in msvcrt.dll Pin
David Fedolfi19-Jan-01 4:20
David Fedolfi19-Jan-01 4:20 
GeneralMidi - Programming Pin
.::RockNix::.18-Jan-01 21:44
.::RockNix::.18-Jan-01 21:44 
GeneralError in release mode Pin
18-Jan-01 21:28
suss18-Jan-01 21:28 
GeneralMove Controls in Run Time Pin
Mani Kandan.K18-Jan-01 20:46
Mani Kandan.K18-Jan-01 20:46 
GeneralRe: Move Controls in Run Time Pin
AlexMarbus19-Jan-01 6:42
AlexMarbus19-Jan-01 6:42 
GeneralRe: Move Controls in Run Time Pin
Joan M20-Jan-01 1:10
professionalJoan M20-Jan-01 1:10 
GeneralError ocurs when I use DoVerb of a COleClientItem in a drag-drop operation. Pin
niel18-Jan-01 16:30
niel18-Jan-01 16:30 
GeneralCRect Question Pin
Frank Deo18-Jan-01 11:34
Frank Deo18-Jan-01 11:34 
GeneralRe: CRect Question Pin
Frank Deo18-Jan-01 14:36
Frank Deo18-Jan-01 14:36 
GeneralRe: CRect Question Pin
Christian Graus18-Jan-01 14:37
protectorChristian Graus18-Jan-01 14:37 
QuestionIs there a way to select a line drawn by LineTo? Pin
18-Jan-01 6:41
suss18-Jan-01 6:41 
AnswerRe: Is there a way to select a line drawn by LineTo? Pin
18-Jan-01 8:11
suss18-Jan-01 8:11 
QuestionODBC or OLE DB? Pin
18-Jan-01 3:44
suss18-Jan-01 3:44 

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.