Hi, I am having a C++ application. It works fine when I run it in Debug mode, and debugs the code. But when I execute it in comand line mode I get an Unhandled exception.

Unhandled exception at 0x77012016 in test.exe: 0xC0000005: Access violation reading location 0x6cfd018d.

LPTSTR getFileDetails(LPTSTR pszObjName, SE_OBJECT_TYPE ObjectType, BOOL getOwner, BOOL getGroup)
{
	PSID pSidG = (PSID)HeapAlloc(sizeof(PSID));
	PSID pSidO = (PSID)HeapAlloc(sizeof(PSID));
	LPTSTR AcctName = (LPTSTR)"", DomainName = (LPTSTR)"";
	DWORD dwAcctName = 1, dwDomainName = 1;
	SID_NAME_USE eUse = SidTypeUnknown;
	PSECURITY_DESCRIPTOR pSD = NULL;
	BOOL dwRes = FALSE , bRtnBool = FALSE;
	DWORD ccv = GetLastError();

	dwRes = GetNamedSecurityInfo(pszObjName, ObjectType, DACL_SECURITY_INFORMATION,	NULL, NULL, NULL, NULL, &pSD);

	if(getGroup){
		dwRes = GetNamedSecurityInfo(pszObjName, ObjectType, GROUP_SECURITY_INFORMATION, NULL, &pSidG, NULL, NULL, &pSD);
		ccv = GetLastError();
		bRtnBool = LookupAccountSid(NULL, pSidG, AcctName, (LPDWORD)&dwAcctName, DomainName, (LPDWORD)&dwDomainName, &eUse);  
	}

	if(getOwner){
		dwRes = GetNamedSecurityInfo(pszObjName, ObjectType, OWNER_SECURITY_INFORMATION,&pSidO, NULL, NULL, NULL, &pSD);
 		ccv = GetLastError();
[B]//here crashes and return error [/B]
	[B]	bRtnBool = LookupAccountSid(NULL, pSidO, AcctName, (LPDWORD)&dwAcctName, DomainName, (LPDWORD)&dwDomainName, &eUse);  [/B]
	}

	ccv = GetLastError();

	AcctName = (LPTSTR)GlobalAlloc(GMEM_FIXED, dwAcctName);

	if(AcctName == NULL)
	{
		printf("GlobalAlloc() error = %d\n", GetLastError());
		return NULL;
	}

	DomainName = (LPTSTR)GlobalAlloc(GMEM_FIXED, dwDomainName);

	if(DomainName == NULL)
	{
		printf("GlobalAlloc() error = %d\n", GetLastError());
		return NULL;
	}

	if(getGroup)
		bRtnBool = LookupAccountSid(NULL, pSidG, AcctName, (LPDWORD)&dwAcctName, DomainName, (LPDWORD)&dwDomainName, &eUse); 

	if(getOwner)
		bRtnBool = LookupAccountSid(NULL, pSidO, AcctName, (LPDWORD)&dwAcctName, DomainName, (LPDWORD)&dwDomainName, &eUse);  

	return AcctName;
}

http://msdn.microsoft.com/en-us/library/aa379166%28v=vs.85%29.aspx
If you're calling it the first time to retrieve the length of the name, then you should pass NULL for the name, and make sure the length is initialised to zero.

Otherwise, you're passing a pointer to a string in read-only memory, and it attempts to write 1 byte to it (and tells you you can't as a result).

Holy thread revival batman!

I am today having this exact same issue :(

I have a loop that goes:

get a directory
for each subdirectory
    get DACL from subdirectory
    for each ace
        get the sid
        if allow or deny
            isValidSid() ? YES






            LPWSTR myTrusteeName = _T(""), myDomainName = _T("");
            DWORD myDwordNameLength = 0, myDwordDomLength = 0;
            SID_NAME_USE myNameUse = SidTypeUnknown;

            //Make an initial lookup to find out how big the names are
            LookupAccountSidW(
                NULL
                , mySid
                , NULL
                , (LPDWORD)&myDwordNameLength
                , NULL
                , (LPDWORD)&myDwordDomLength
                , &myNameUse
                ); //at this point error 122 is thrown as the length of myTrusteeName is too short.


            //alter the size of these variables rather than just getting the 1st letter and some gibberish
            myTrusteeName = (LPWSTR)GlobalAlloc(GMEM_FIXED, myDwordNameLength);
            myDomainName = (LPWSTR)GlobalAlloc(GMEM_FIXED, myDwordDomLength);

            //do the lookup again this time with genuine sizes
            BOOL it_dun_gud_ya = LookupAccountSidW(
                NULL
                , mySid
                , myTrusteeName
                , (LPDWORD)&myDwordNameLength
                , myDomainName
                , (LPDWORD)&myDwordDomLength
                , &myNameUse
                );

            //find out if we got a valid SID
            if (it_dun_gud_ya){
                if ((_tcslen(myTrusteeName) == 0) && (_tcslen(myDomainName) > 0)){
                    myOutputRow << myDomainName << "\"," << type;
                }
                else if ((_tcslen(myTrusteeName) > 0) && (_tcslen(myDomainName) == 0)){
                    myOutputRow << myTrusteeName << "\"," << type;
                }
                else if ((_tcslen(myTrusteeName) > 0) && (_tcslen(myDomainName) > 0)){
                    myOutputRow << myDomainName << "\\" << myTrusteeName << "\"," << type;
                }
                //wcout << myOutputRow.str() << endl;

            }

ELSE NOT a valid sid ? continue...

On the first iteration it works fine but subsequent iterations I get an access violation on what looks like the FIRST call to to LookupAccountSid. It's as if something is not being cleared down properly which doesn't make any sense given that the variables being used are local to the inside of the iteration...

Am I being some sort of retard here?

Ta

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.