Importing a C++ function problems

BlitzMax Forums/BlitzMax Programming/Importing a C++ function problems

Grey Alien(Posted 2007) [#1]
Hi, I have this C++ function:

bool CreateDirectoryUserFullAccess(LPCTSTR lpPath)
{
int f = CreateDirectory(lpPath,NULL);
if(!f)
return false;

HANDLE hDir = CreateFile(lpPath,READ_CONTROL|WRITE_DAC,0,NULL,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,NULL);
if(hDir == INVALID_HANDLE_VALUE)
return true;

ACL* pOldDACL=NULL;
SECURITY_DESCRIPTOR* pSD = NULL;
GetSecurityInfo(hDir,SE_FILE_OBJECT,DACL_SECURITY_INFORMATION,NULL,NULL,&pOldDACL,NULL,(void**)&pSD);

EXPLICIT_ACCESS ea={0};
ea.grfAccessMode = GRANT_ACCESS;
ea.grfAccessPermissions = GENERIC_ALL;
ea.grfInheritance = CONTAINER_INHERIT_ACE|OBJECT_INHERIT_ACE;
ea.Trustee.TrusteeType = TRUSTEE_IS_GROUP;
ea.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
ea.Trustee.ptstrName = TEXT("Users");

ACL* pNewDACL = NULL;
SetEntriesInAcl(1,&ea,pOldDACL,&pNewDACL);

SetSecurityInfo(hDir,SE_FILE_OBJECT,DACL_SECURITY_INFORMATION,NULL,NULL,pNewDACL,NULL);

LocalFree(pSD);
LocalFree(pNewDACL);
CloseHandle(hDir);
return true;
}


Save in a file called vista.cpp

Then in Bmax I go:

Strict

Import "vista.cpp"


But when I compile, I get a page full of errors like this:

Compiling:vista.cpp
C:/BlitzMax/Jake/demo/vista.cpp:1: `LPCTSTR' was not declared in this scope
C:/BlitzMax/Jake/demo/vista.cpp:1: parse error before `)' token
C:/BlitzMax/Jake/demo/vista.cpp: In function `bool 
   CreateDirectoryUserFullAccess(...)':
C:/BlitzMax/Jake/demo/vista.cpp:3: `lpPath' undeclared (first use this 
   function)
C:/BlitzMax/Jake/demo/vista.cpp:3: (Each undeclared identifier is reported only 
   once for each function it appears in.)
C:/BlitzMax/Jake/demo/vista.cpp:3: `NULL' undeclared (first use this function)
C:/BlitzMax/Jake/demo/vista.cpp:3: `CreateDirectory' undeclared (first use this 
   function)
C:/BlitzMax/Jake/demo/vista.cpp:7: `HANDLE' undeclared (first use this 
   function)
C:/BlitzMax/Jake/demo/vista.cpp:7: parse error before `=' token
C:/BlitzMax/Jake/demo/vista.cpp:8: `hDir' undeclared (first use this function)
C:/BlitzMax/Jake/demo/vista.cpp:8: `INVALID_HANDLE_VALUE' undeclared (first use 
   this function)
C:/BlitzMax/Jake/demo/vista.cpp:11: `ACL' undeclared (first use this function)
C:/BlitzMax/Jake/demo/vista.cpp:11: `pOldDACL' undeclared (first use this 
   function)
C:/BlitzMax/Jake/demo/vista.cpp:12: `SECURITY_DESCRIPTOR' undeclared (first use 
   this function)
C:/BlitzMax/Jake/demo/vista.cpp:12: `pSD' undeclared (first use this function)
C:/BlitzMax/Jake/demo/vista.cpp:13: `SE_FILE_OBJECT' undeclared (first use this 
   function)
C:/BlitzMax/Jake/demo/vista.cpp:13: `DACL_SECURITY_INFORMATION' undeclared 
   (first use this function)
C:/BlitzMax/Jake/demo/vista.cpp:13: `GetSecurityInfo' undeclared (first use 
   this function)
C:/BlitzMax/Jake/demo/vista.cpp:15: `EXPLICIT_ACCESS' undeclared (first use 
   this function)
C:/BlitzMax/Jake/demo/vista.cpp:15: parse error before `=' token
C:/BlitzMax/Jake/demo/vista.cpp: At global scope:
C:/BlitzMax/Jake/demo/vista.cpp:16: syntax error before `.' token
C:/BlitzMax/Jake/demo/vista.cpp:17: syntax error before `.' token
C:/BlitzMax/Jake/demo/vista.cpp:18: syntax error before `.' token
C:/BlitzMax/Jake/demo/vista.cpp:19: syntax error before `.' token
C:/BlitzMax/Jake/demo/vista.cpp:20: syntax error before `.' token
C:/BlitzMax/Jake/demo/vista.cpp:21: syntax error before `.' token
C:/BlitzMax/Jake/demo/vista.cpp:23: syntax error before `*' token
C:/BlitzMax/Jake/demo/vista.cpp:24: `ea' was not declared in this scope
C:/BlitzMax/Jake/demo/vista.cpp:24: `pNewDACL' was not declared in this scope
C:/BlitzMax/Jake/demo/vista.cpp:24: ISO C++ forbids declaration of `
   SetEntriesInAcl' with no type
C:/BlitzMax/Jake/demo/vista.cpp:24: initializer list being treated as compound 
   expression
C:/BlitzMax/Jake/demo/vista.cpp:26: `pNewDACL' was not declared in this scope
C:/BlitzMax/Jake/demo/vista.cpp:26: ISO C++ forbids declaration of `
   SetSecurityInfo' with no type
C:/BlitzMax/Jake/demo/vista.cpp:26: initializer list being treated as compound 
   expression
C:/BlitzMax/Jake/demo/vista.cpp:28: ISO C++ forbids declaration of `LocalFree' 
   with no type
C:/BlitzMax/Jake/demo/vista.cpp:29: `pNewDACL' was not declared in this scope
C:/BlitzMax/Jake/demo/vista.cpp:29: ISO C++ forbids declaration of `LocalFree' 
   with no type
C:/BlitzMax/Jake/demo/vista.cpp:29: redefinition of `int LocalFree'
C:/BlitzMax/Jake/demo/vista.cpp:28: `int LocalFree' previously defined here
C:/BlitzMax/Jake/demo/vista.cpp:30: ISO C++ forbids declaration of `CloseHandle
   ' with no type
C:/BlitzMax/Jake/demo/vista.cpp:31: parse error before `return'
Build Error: failed to compile C:/BlitzMax/Jake/demo/vista.cpp


I've NO idea where to start. I must be doing something really basic wrong. Any ideas? Thanks.

Oh an dI tried saving the file as .c and importing that but got similar errors.


Azathoth(Posted 2007) [#2]
Your c file is including the headers right?
Try putting '#include <windows.h>' at the top in the c file.


Grey Alien(Posted 2007) [#3]
That helped thanks and I found some other things I needed to include. It still doesn't compile though. Some other errors I may post soon...


Grey Alien(Posted 2007) [#4]
OK cool, so I added these three:

#include <windows.h>
#include "accctrl.h"
#include "aclapi.h"


to the top and now I only get these errors:

C:/BlitzMax/Jake/demo/.bmx/vista.cpp.release.win32.x86.o(.text+0x71):vista.cpp: undefined reference to `GetSecurityInfo@32'
C:/BlitzMax/Jake/demo/.bmx/vista.cpp.release.win32.x86.o(.text+0xbe):vista.cpp: undefined reference to `SetEntriesInAclA@16'
C:/BlitzMax/Jake/demo/.bmx/vista.cpp.release.win32.x86.o(.text+0xd1):vista.cpp: undefined reference to `SetSecurityInfo@28'



Those three are declared in Aclapi.h so I don't understand why it won't find them. They all have the correct parameters...


Grey Alien(Posted 2007) [#5]
I even tried compiling this code straight off of MSDN, unaltered:



and I get:


C:/BlitzMax/Jake/demo/.bmx/vista2.cpp.console.release.win32.x86.o(.text+0x130):vista2.cpp: undefined reference to `GetSecurityInfo@32'
C:/BlitzMax/Jake/demo/.bmx/vista2.cpp.console.release.win32.x86.o(.text+0x167):vista2.cpp: undefined reference to `LookupAccountSidA@28'
C:/BlitzMax/Jake/demo/.bmx/vista2.cpp.console.release.win32.x86.o(.text+0x19c):vista2.cpp: undefined reference to `LookupAccountSidA@28'


This should work. Any idea why not? It's weird...


gman(Posted 2007) [#6]
greetings :) try this:
dir_func.cpp
#include <windows.h>
#include <aclapi.h>

extern "C" bool CreateDirectoryUserFullAccess(LPCTSTR lpPath)
{
	int f = CreateDirectory(lpPath,NULL);
	if(!f) return false;
	
	HANDLE hDir = CreateFile(lpPath,READ_CONTROL|WRITE_DAC,0,NULL,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,NULL);
	if(hDir == INVALID_HANDLE_VALUE) return true;
	
	ACL* pOldDACL=NULL;
	SECURITY_DESCRIPTOR* pSD = NULL;
	GetSecurityInfo(hDir,SE_FILE_OBJECT,DACL_SECURITY_INFORMATION,NULL,NULL,&pOldDACL,NULL,&pSD);
	
	EXPLICIT_ACCESS ea={0};
	ea.grfAccessMode = GRANT_ACCESS;
	ea.grfAccessPermissions = GENERIC_ALL;
	ea.grfInheritance = CONTAINER_INHERIT_ACE|OBJECT_INHERIT_ACE;
	ea.Trustee.TrusteeType = TRUSTEE_IS_GROUP;
	ea.Trustee.TrusteeForm = TRUSTEE_IS_NAME;
	ea.Trustee.ptstrName = TEXT("Users");
	
	ACL* pNewDACL = NULL;
	SetEntriesInAcl(1,&ea,pOldDACL,&pNewDACL);
	
	SetSecurityInfo(hDir,SE_FILE_OBJECT,DACL_SECURITY_INFORMATION,NULL,NULL,pNewDACL,NULL);
	
	LocalFree(pSD);
	LocalFree(pNewDACL);
	CloseHandle(hDir);
	return true;
}

dir_func.bmx

Framework BRL.Blitz

Import "dir_func.cpp"
Import "-ladvapi32"

DebugLog(CreateDirectoryUserFullAccess("c:\temp\testdirectory"))

Extern
	Function CreateDirectoryUserFullAccess:Int(lpPath$z)
EndExtern



Grey Alien(Posted 2007) [#7]
Thanks gman, whatever jiggery pokery you did, it compiles, woo. I shall be trying this out on Vista tomorrow. I'll post back my results.

Well I couldn't sleep so I tried it anyway and IT TOTALLY WORKS. So we now have a valid solution for storing shared highscores and data on Vista!

Thanks GMan, you are my hero!


gman(Posted 2007) [#8]
hero? lol. just drop me a virtual beer sometime :) very glad i could be of assistace. keep up the great work! it was a nice little break from my current project :)


Grey Alien(Posted 2007) [#9]
You found the problem with the (void**) then, I found that too. No idea why Indiepath put that in there.


TartanTangerine (was Indiepath)(Posted 2007) [#10]
You found the problem with the (void**) then, I found that too. No idea why Indiepath put that in there.

It's common practice when dealing with Security Descriptors - don't ask me why but every example I have come across uses it.


Grey Alien(Posted 2007) [#11]
It just didn't compile with it for some reason.