user1418820
user1418820

Reputation: 23

C program - Structure marked as undeclared in compiler

I am trying to write a program to collect security information about a file and convert it to human readable information. However, I am facing a problem with initializing a pointer to structure:

#include <stdio.h>
#include <aclapi.h>
#pragma comment(lib, "advapi32.lib")
struct file_perms {
    char user_domain[2050];
    unsigned long user_mask;
};
static myfunc (){

PSECURITY_DESCRIPTOR pSD = NULL;
PACL pDACL = NULL;
char *file = "D:/code/test.c";
ACL_SIZE_INFORMATION aclSize;

ULONG result = GetNamedSecurityInfo(file,SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, &pDACL, NULL, &pSD);
if (ERROR_SUCCESS != result) {
    printf( "GetNamedSecurityInfo Error %u\n", result );
}
if(pDACL != NULL){printf ("2\n");}
//ACL_SIZE_INFORMATION aclSize = {0};
ZeroMemory(&aclSize, sizeof(ACL_SIZE_INFORMATION));
    if(pDACL != NULL){   
        if(!GetAclInformation(pDACL, &aclSize, sizeof(aclSize),
            AclSizeInformation)){
            printf("GetAclInformation Error \n");
            return 0;
        }
        printf("AceCount %d\n",aclSize.AceCount);
    }
file_perms *fp = new file_perms[aclSize.AceCount];

}

While compiling, I am getting the following error. getnamed.c

getnamed.c(34) : error C2065: 'file_perms' : undeclared identifier
getnamed.c(34) : error C2065: 'fp' : undeclared identifier
getnamed.c(34) : error C2065: 'new' : undeclared identifier
getnamed.c(34) : error C2106: '=' : left operand must be l-value
getnamed.c(34) : error C2146: syntax error : missing ';' before identifier 'file
_perms'
getnamed.c(34) : error C2065: 'file_perms' : undeclared identifier
getnamed.c(34) : error C2109: subscript requires array or pointer type

Can someone help me understand why is file_perms marked as undeclared identifier? While it is declared as a structure already?

Thank you for your help.

Upvotes: 1

Views: 6811

Answers (3)

arrowd
arrowd

Reputation: 34411

Because you are compiling your code as C code. And it's C++.

If you wish to compile it as C, try this:

typedef struct file_perms_ {
    char user_domain[2050];
    unsigned long user_mask;
} file_perms;

Upvotes: 2

rkosegi
rkosegi

Reputation: 14678

You should have

struct file_perms *fp = new file_perms[aclSize.AceCount];

or create type at begining:

typedef struct file_perms {
    char user_domain[2050];
    unsigned long user_mask;
}file_perm;

and later you can use it like

file_perms *fp;
fp = (file_perms*)malloc(aclSize.AceCount * sizeof(file_perms));

BTW : operator new is c++ syntax, not pure C, you are most probably trying to compile C++ code as C

Upvotes: 2

mox
mox

Reputation: 6324

Change

struct file_perms{ 
    char user_domain[2050]; 
    unsigned long user_mask; 
}; 

to this will solve your problem:

struct{ 
    char user_domain[2050]; 
    unsigned long user_mask; 
}file_perms; 

Upvotes: -1

Related Questions