1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
| // Utilisateurs.cpp*: définit le point d'entrée pour l'application console.
//
#include <windows.h>
#include <stdio.h>
//Link to Active Directory Services Interfaces
#pragma comment(lib, "ActiveDs.lib")
#pragma comment(lib, "ADsIID.lib")
#include <stdio.h>
#include <iads.h>
#include <activeds.h>
#include <ntldap.h>
#include <comdef.h>
/*******************************************************************
AddDomainUserToLocalGroup()
Adds a user from a domain to a local group using the WinNT
provider.
Parameters:
pwszComputerName - Contains the name of the computer
that the group belongs to.
pwszGroupName - Contains the name of the group to add a member
to. This group must exist on the target computer.
pwszDomainName - Contains the name of domain that contains
the user to add to the group.
pwszUserToAdd - Contains the name of the user in the domain
to add to the group.
*******************************************************************/
HRESULT AddDomainUserToLocalGroup(LPCWSTR pwszComputerName,
LPCWSTR pwszGroupName,
LPCWSTR pwszDomainName,
LPCWSTR pwszUserToAdd)
{
HRESULT hr = E_FAIL;
IADsContainer* pComputer;
// Build the binding string.
_bstr_t sbstrBindingString;
sbstrBindingString = "WinNT://";
sbstrBindingString += pwszComputerName;
sbstrBindingString += ",computer";
// Bind to the computer that contains the group.
hr = ADsOpenObject( sbstrBindingString,
NULL,
NULL,
ADS_SECURE_AUTHENTICATION,
IID_IADsContainer,
(void**)&pComputer);
if(FAILED(hr))
{
return hr;
}
// Bind to the group object.
IDispatch* pDisp;
hr = pComputer->GetObject(_bstr_t("group"),
_bstr_t(pwszGroupName),
&pDisp);
if(FAILED(hr))
{
pComputer->Release();
return hr;
}
IADsGroup* pGroup;
hr = pDisp->QueryInterface(IID_IADs, (LPVOID*)&pGroup);
if(FAILED(hr))
{
pComputer->Release();
pDisp->Release();
return hr;
}
// Bind to the member to add.
sbstrBindingString = "WinNT://";
sbstrBindingString += pwszDomainName;
sbstrBindingString += "/";
sbstrBindingString += pwszUserToAdd;
hr = pGroup->Add(sbstrBindingString);
pComputer->Release();
pDisp->Release();
pGroup->Release();
return hr;
}
/* Main function
------------- */
int main()
{
HRESULT hr;
hr = CoInitialize(NULL);
if(FAILED(hr))
{
puts("erreur dans CoInitialize()");
return hr;
}
hr = AddDomainUserToLocalGroup(
L"BUREAU_1", //Nom de l'ordinateur
L"Administrateurs", //Nom du groupe
L"BUREAU_1", //Nom de l'ordinateur aussi
L"Conseillere" //Num d'utilisateur
);
if(FAILED(hr))
{
puts("erreur dans AddDomainUserToLocalGroup()");
return hr;
}
CoUninitialize();
return 0;
} |
Partager