Can not get procedure to return
Posted: Wed Mar 28, 2012 10:55 pm
OK, I feel really stupid for asking this,,,
Problem: Can not get this to return the result string (SIDString.s). I can debug the result in the procedure, and its correct by the way, but the second I type the procedure to return the string the result is empty but the functions indicate they were sucessful so the functions aren't failing. Maybe some memory thing?
Below is the whole code i'm testing with, I have to use these functions so i can't substitute for something else. If you execute it as is you will notice the string 'SIDString' debugs fine in the procedure. The second you type the procedure for a string return (Procedure.s ....) the string is suddenly empty. In addition if you run the code as is and simply put a 'Debug' in front of the procedure call the string is empty. If you type the procedure for a string return and try to return the string it crashes at the 'FreeMemory(*secDescPtr)' line. In short the only way its working right now is without trying to return anything from it.
What am I missing?
(yes, thats the actual API name - ConvertSecurityDescriptorToStringSecurityDescriptor - probably the longest API name there is
)
Problem: Can not get this to return the result string (SIDString.s). I can debug the result in the procedure, and its correct by the way, but the second I type the procedure to return the string the result is empty but the functions indicate they were sucessful so the functions aren't failing. Maybe some memory thing?
Below is the whole code i'm testing with, I have to use these functions so i can't substitute for something else. If you execute it as is you will notice the string 'SIDString' debugs fine in the procedure. The second you type the procedure for a string return (Procedure.s ....) the string is suddenly empty. In addition if you run the code as is and simply put a 'Debug' in front of the procedure call the string is empty. If you type the procedure for a string return and try to return the string it crashes at the 'FreeMemory(*secDescPtr)' line. In short the only way its working right now is without trying to return anything from it.
Code: Select all
#SDDL_REVISION_1 = 1
#DACL_SECURITY_INFORMATION = $00000004
Prototype PConvertSecurityDescriptorToStringSecurityDescriptor(SecurityDescriptor, requestedStringSDRevision, SecurityInformation, StringSecurityDescriptor, StringSecurityDescriptorLen)
Global ConvertSecurityDescriptorToStringSecurityDescriptor.PConvertSecurityDescriptorToStringSecurityDescriptor
Lib_Advapi32 = OpenLibrary(#PB_Any,"Advapi32.dll")
If IsLibrary(Lib_Advapi32) : LibAdvapi = #True
CompilerIf #PB_Compiler_Unicode ; If compiled in unicode
ConvertSecurityDescriptorToStringSecurityDescriptor.PConvertSecurityDescriptorToStringSecurityDescriptor=GetFunction(Lib_Advapi32,"ConvertSecurityDescriptorToStringSecurityDescriptorW")
CompilerElse ; if not compiled in unicode
ConvertSecurityDescriptorToStringSecurityDescriptor.PConvertSecurityDescriptorToStringSecurityDescriptor=GetFunction(Lib_Advapi32,"ConvertSecurityDescriptorToStringSecurityDescriptorA")
CompilerEndIf
Else
LibAdvapi = #False
EndIf
Procedure.q TopHiveKey(HiveKeyConvert$)
Protected HiveKeyx.q
If Right(HiveKeyConvert$, 1) = "\"
HiveKeyConvert$ = RemoveString(HiveKeyConvert$, "\", #PB_String_NoCase, Len(HiveKeyConvert$) - 1, 1)
Else
HiveKeyConvert$ = HiveKeyConvert$
EndIf
HiveKeya$=StringField(HiveKeyConvert$,1,"\")
HiveKeyTop$=UCase(HiveKeya$)
Select HiveKeyTop$
Case "HKEY_CLASSES_ROOT"
HiveKeyx = #HKEY_CLASSES_ROOT
Case "HKEY_CURRENT_USER"
HiveKeyx = #HKEY_CURRENT_USER
Case "HKEY_LOCAL_MACHINE"
HiveKeyx = #HKEY_LOCAL_MACHINE
Case "HKEY_USERS"
HiveKeyx = #HKEY_USERS
Case "HKEY_CURRENT_CONFIG"
HiveKeyx = #HKEY_CURRENT_CONFIG
Default
HiveKeyx = #Null
EndSelect
ProcedureReturn HiveKeyx
EndProcedure
Procedure.s KeyConvert(KeyToConvert$)
If Right(KeyToConvert$, 1) = "\"
KeyToConvert$ = RemoveString(KeyToConvert$, "\", #PB_String_NoCase, Len(KeyToConvert$) - 1, 1)
Else
KeyToConvert$ = KeyToConvert$
EndIf
GetBackSlash=FindString(KeyToConvert$,"\",1)
KeyNameX$=Right(KeyToConvert$,(Len(KeyToConvert$)-GetBackSlash))
If Left(KeyNameX$, 1) = "\"
KeyNameX$ = Right(KeyNameX$, Len(KeyNameX$) - 1)
EndIf
ProcedureReturn KeyNameX$
EndProcedure
Procedure Reg_GetKeySecurity(Key.s, secInfo.i)
Protected hKey.l, HiveKey.q, KeyName$, SIDString.s, retfunc.i
cbLength = 0
HiveKey = TopHiveKey(Key)
KeyName$ = KeyConvert(Key)
*secDescPtr.SECURITY_DESCRIPTOR = #Null
FuncRet = RegOpenKeyEx_(HiveKey, @KeyName$, 0, #KEY_ALL_ACCESS|#READ_CONTROL, @hKey)
If FuncRet = #ERROR_SUCCESS
retfunc = RegGetKeySecurity_(hKey, @secInfo, 0, @cbLength) ; get buffer size
If retfunc = #ERROR_INSUFFICIENT_BUFFER
*secDescPtr = AllocateMemory(cbLength)
retfunc = RegGetKeySecurity_(hKey, @secInfo, *secDescPtr, @cbLength)
EndIf
EndIf
*pSID=AllocateMemory(255)
FuncRet = ConvertSecurityDescriptorToStringSecurityDescriptor(*secDescPtr, #SDDL_REVISION_1, @secInfo, @*pSID, #Null); output in SDDL format
memlen = MemoryStringLength(*pSID)
SIDString=PeekS(*pSID,memlen)
LocalFree_(*pSID)
Debug SIDString
FreeMemory(*secDescPtr)
RegCloseKey_(hKey)
ProcedureReturn ;SIDString
EndProcedure
Reg_GetKeySecurity("HKEY_CURRENT_USER\Control Panel", #DACL_SECURITY_INFORMATION)
CloseLibrary(Lib_Advapi32)
(yes, thats the actual API name - ConvertSecurityDescriptorToStringSecurityDescriptor - probably the longest API name there is