473,396 Members | 1,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Converting WMI binary SD array to Security Descriptor

I have a C++ function in a COM object that I am trying to implement in
VB.NET (the functionality, NOT the COM object, I want to remove the
requirement for the COM DLL)

I am an experienced VB programmer, but this is my first .NET app...

Original Code (what I have been doing and works)
VB6 (extra stuff removed for clarity)
Set objWbem = CreateObject("WbemScripting.SWbemLocator")
Set objSvc = objWbem.ConnectServer(ComputerName, "root")
Set objSysSec = objSvc.Get("__SystemSecurity=@")
lngReturn = objSysSec.GetSD(SD_Array)

If lngReturn = 0 Then
Dim Dacl As AccessControlList
Dim Ace As AccessControlEntry
Dim SD As SecurityDescriptor
Dim SDConverter As Object
Dim lngErr As Long

On Error Resume Next

Set SDConverter = New ADS_SD_WRAPPERLib.SD_Converter
' ----- COM CALL
Set SD = SDConverter.GetADsSecurityDescriptor(SD_Array)
Set Dacl = SD.DiscretionaryAcl

Set Ace = CreateObject("AccessControlEntry")
Ace.AccessMask = WBEM_REMOTE_ACCESS _
+ WBEM_ENABLE _
+ READ_CONTROL _
+ WBEM_METHOD_EXECUTE
Ace.AceType = ADS_ACETYPE_ACCESS_ALLOWED
Ace.AceFlags = ADS_ACEFLAG_INHERIT_ACE
Ace.TRUSTEE = TRUSTEE ' String Value 'DOMAIN\USER'

Dacl.AddAce Ace
SD.DiscretionaryAcl = Dacl
' ----- COM CALL
SD_Array = SDConverter.GetBinarySecurityDescriptor(SD)

lngReturn = objSysSec.SetSD(SD_Array)
If lngReturn = 0 Then 'Successful setting permission
' Worked!
End If
End If
' ----- END VB6 CODE

The two calls being made in the C++ COM object are converting the byte
array received from SetSD into a Security Descriptor (so I can manipulate
it) then back again (so I can apply it)
The C++ code (bottom of this message) is manipulating the array via calls
to GetSafeArray (which are "arrays" in VB6 - I "assume" that won't be a
problem in .NET.) The problem I have is with the call to
"CoCreateInstance".

I don't think I'm supposed to use that call in .NET, but I don't know the
VB.NET equivelent. When I try to perform this same functionality, the
return code I get (from ym CoCreateInstance call in VB .NET) is -2147221164
(0x80040154) - according the error code lookup tool it means "Class not
registered". I'm assuming that they are talking about the reference to
CLSID_PropertyValue ("7b9e38b0-a97c-11d0-8534-00c04fd8d503" - which from
looking in the registry doesn't appear to have a "Class name", just a
reference to ActiveDS.dll)

Anyway, can someone tell me how I can do this in .NET? I've been reading
up on this stuff but I can't seem to find the "one piece" that I'm
missing...
Thanks
Chris
STDMETHODIMP CSD_Converter::GetADsSecurityDescriptor(VARIANT array_variant,
IUnknown **ADsSecurityDescriptor)
{
// TODO: Add your implementation code here

/* */
/* Create and fill SafeArray */
/* SafeArrayCreation code removed */

HRESULT hr;
long lbound = 0;
long ubound = 0;

VariantClear(&vt);
vt.vt = VT_ARRAY|VT_UI1;
vt.parray = psa;
// bind to directory object
IADsPropertyValue2 *pVal2 = NULL;

hr = CoCreateInstance
(CLSID_PropertyValue,NULL,CLSCTX_INPROC_SERVER,IID _IADsPropertyValue2,
(void**)&pVal2);
if (SUCCEEDED(hr))
{
hr = pVal2->PutObjectProperty(ADSTYPE_OCTET_STRING,vt);
VariantClear(&vt);
if (SUCCEEDED(hr))
{
long type = ADSTYPE_NT_SECURITY_DESCRIPTOR;
hr = pVal2->GetObjectProperty(&type,&vt);
if (SUCCEEDED(hr))
{
*ADsSecurityDescriptor = vt.punkVal;
}
}

pVal2->Release();
}
else
SafeArrayDestroy(psa);

CoUninitialize();

return hr;
}

STDMETHODIMP CSD_Converter::GetBinarySecurityDescriptor(IUnknow n
*ADsSecurityDescriptor, VARIANT* p_array_variant)
{
// TODO: Add your implementation code here

HRESULT hr;

VARIANT vt,vtbyte;
vt.vt = VT_DISPATCH;
hr = ADsSecurityDescriptor->QueryInterface(IID_IDispatch,(void**)&
(vt.pdispVal));

IADsPropertyValue2 *pVal2 = NULL;
hr = CoCreateInstance
(CLSID_PropertyValue,NULL,CLSCTX_INPROC_SERVER,IID _IADsPropertyValue2,
(void**)&pVal2);
if (SUCCEEDED(hr))
{
hr = pVal2->PutObjectProperty(ADSTYPE_NT_SECURITY_DESCRIPTOR, vt);
VariantClear(&vt);
if (SUCCEEDED(hr))
{
long type = ADSTYPE_OCTET_STRING;
hr = pVal2->GetObjectProperty(&type,&vt);
if (SUCCEEDED(hr) && (vt.vt == (VT_ARRAY|VT_UI1)))
{
/* */
/* Create and fill SafeArray */
/* */

long lbound = 0;
long ubound = 0;

if (FAILED(SafeArrayGetLBound(vt.parray,1,&lbound)))
return E_FAIL;

if (FAILED(SafeArrayGetUBound(vt.parray,1,&ubound)))
return E_FAIL;

SAFEARRAY *psa = NULL;
SAFEARRAYBOUND saBounds;
saBounds.cElements = ubound-lbound+1;
saBounds.lLbound = 0;
psa = SafeArrayCreate(VT_VARIANT, 1, &saBounds);
unsigned char bVal;
VariantClear(&vtbyte);
vtbyte.vt = VT_UI1;

long j = 0;
long index = 1;
for (long i=lbound; i<=ubound; i++)
{
if (SUCCEEDED(hr=SafeArrayGetElement(vt.parray,&i,(vo id*)&bVal)))
{
vtbyte.bVal = bVal;
hr=SafeArrayPutElement(psa,&j,(void*)&vtbyte);
j++;
}
else
{
SafeArrayDestroy(psa);
return hr;
}
}

VariantClear(p_array_variant);
p_array_variant->vt = VT_ARRAY|VT_VARIANT;
p_array_variant->parray = psa;

VariantClear(&vt);
}
}

pVal2->Release();
}
else
VariantClear(&vt);

return hr;
}
Nov 20 '05 #1
0 3815

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope...
8
by: Rick | last post by:
Hi, Does C have some handy functions to convert chars, ints and floats to bit arrays? I need to store that stuff binary so a few functions would be great. Converting chars and ints isn't...
4
by: Tom Van Ginneken | last post by:
Hi, I need to write binary data to a serial port. I am using this function: #include <unistd.h> ssize_t write(int fd, const void *buf, size_t count); I am able to write a alpha-numeric...
18
by: No Such Luck | last post by:
Hi all: I have an unsigned char array (size 4): unsigned char array; array = 0x00; array = 0x00; array = 0x02; array = 0xe7;
1
by: Jan Nielsen | last post by:
In a C# application I'm using the NetShareGetInfo API function to get some information about a share. This is working all right. Now I want my application to be able to display the contents of...
7
by: dlarock | last post by:
I wrote the following to do an MD5 hash. However, I have a problem (I think) with the conversion from the Byte MD5 hash back to string. Watching this through the debugger it appears as if the...
11
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
3
by: sreemati | last post by:
Hi Everyone, I have a store procedure returning 7 rows for 3 columns, basically I am retrieving: Descriptor Lower Range Upper Range D1 1 10 D2 ...
9
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.