Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"reference a token that does not exist" esebcli2 HrESEBackupPrepare 3

Status
Not open for further replies.

btaber

Programmer
May 26, 2002
307
US
I am trying to complete a custom backup solution. I am using the esebcli2.dll interfaces for this. I am able to call MNUBackupRestoreGetNodes with no problem. When I try to prepare the backup using HrESEBackupPrepare, the command fails with "An attempt was made to reference a token that does not exist." I have found many posts regarding this, but no solutions. Does anyone have any ideas?

Here is some of the code:


Code:
HrESEBackupPrepare fnHrESEBackupPrepare = (HrESEBackupPrepare) GetProcAddress (hMod,"HrESEBackupPrepare"); 
ESEBackupFreeInstanceInfo fnESEBackupFreeInstanceInfo = (ESEBackupFreeInstanceInfo) GetProcAddress (hMod,"ESEBackupFreeInstanceInfo"); 


WCHAR *MyServer = L"MYSERVER";
WCHAR *MyStore = L"Microsoft Information Store";

unsigned long InstanceInfo = 0;
INSTANCE_BACKUP_INFO *aInstanceInfo;
HCCX Handle;

hr = (fnHrESEBackupPrepare) (
        MyServer,
        MyStore,
        &InstanceInfo,
        &aInstanceInfo,
        &Handle);

if (hr != S_OK)
{
    printf("BackupPrepare Error: %-8lx", hr);
    FreeLibrary (hMod);
    return -1;
}
 
Read The Manual perhaps?
"Each element in the array is a structure of type INSTANCE_BACKUP_INFO Structure."
Your 4th parameter makes no sense compared to this description (did you even get any warnings from your compiler about mis-matched types?).

If the service tries to use the memory you're giving it (a measly 4 bytes), then it's going to trash some other memory, which probably leads to the rather odd status result.

And since there seems to be no way to tell it how many results to return, I'm guessing there is another API call which will tell you how many INSTANCE_BACKUP_INFO's are present.

Eg.
Code:
INSTANCE_BACKUP_INFO *aInstanceInfo = malloc ( num * sizeof *aInstanceInfo );
hr = (fnHrESEBackupPrepare) (
        MyServer,
        MyStore,
        &InstanceInfo,
        aInstanceInfo,
        &Handle);

Also, this question should probably have been on the Microsoft Visual C++ forum.

--
 
I am retrieving the number of INSTANCE_BACKUP_INFO's present (cRegisteredInfo) from a previous call to HrESEBackupRestoreGetRegistered. The INSTANCE_BACKUP_INFO structure looks like:

Code:
	typedef HRESULT (ESEBACK_API *HrESEBackupPrepare)(
		IN  WCHAR *  		wszBackupServer,
		IN  WCHAR *  		wszBackupAnnotation,
		OUT unsigned long *				pcInstanceInfo,
		OUT INSTANCE_BACKUP_INFO ** 	paInstanceInfo,
		OUT HCCX * 			phccxBackupContext
		);

How does INSTANCE_BACKUP_INFO ** work?

I tried to write aInstanceInfo as

Code:
INSTANCE_BACKUP_INFO **aInstanceInfo  = (INSTANCE_BACKUP_INFO **) malloc ( cRegisteredInfo * sizeof ** aInstanceInfo );

but same error.

Am I missing something?
 
> present (cRegisteredInfo) from a previous call to HrESEBackupRestoreGetRegistered
How did you call it?

> The INSTANCE_BACKUP_INFO structure looks like:
Post what you did, I can read the manual page for anything you use quite happily on MSDN

> Am I missing something?
Yes.
For one thing, I don't believe you're getting compilations without any warnings.

Maybe
Code:
INSTANCE_BACKUP_INFO *aInstanceInfo = malloc( cRegisteredInfo * sizeof * aInstanceInfo );
I'll ask again, are you programming in C or C++?

--
 
Sorry, did not see the previous question about environment, I have using Visual C++ (but the program is a simple command line program) I can repost in the Visual C++ thread if needed, but I would hate to loose the conversation...

I did try that INSTANCE_BACKUP_INFO, but same result...

Supprisingly, I did not get compile errors before, but I tried that INSTANCE_BACKUP_INFO and I get compile errors:

"Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast"


Here is my complete code:

Code:
// Backup.cpp : Defines the entry point for the console

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "esebcli2.h"
#include <iostream>
#include <wchar.h>


bool GetProcAddresses( HINSTANCE *hLibrary, LPCSTR lpszLibrary, INT nCount, ... )
{
	va_list va;
	va_start( va, nCount );

	if ( ( *hLibrary = LoadLibrary( lpszLibrary ) ) != NULL )
	{
		FARPROC * lpfProcFunction = NULL;
		LPSTR lpszFuncName = NULL;
		INT nIdxCount = 0;
		while ( nIdxCount < nCount )
		{
			lpfProcFunction = va_arg( va, FARPROC* );
			lpszFuncName = va_arg( va, LPSTR );
			if ( ( *lpfProcFunction = 
				GetProcAddress( *hLibrary, 
					lpszFuncName ) ) == NULL )
			{
				lpfProcFunction = NULL;
				return FALSE;
			}
			nIdxCount++;
		}
	}
	else
	{
		va_end( va );
		return FALSE;
	}
	va_end( va );
	return TRUE;
}



int main(int argc, char* argv[])
{

    char lszValue[200];
    LONG lRet;
    HKEY hKey;
    DWORD dwLength=200;
    DWORD dwType=REG_SZ;
    DWORD dwSize=255;

    lRet = RegOpenKeyEx (HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\BackupRestore\\DLLPaths", 0L, KEY_READ , &hKey);
    if(lRet == ERROR_SUCCESS)
    {
	    lRet = RegQueryValueEx(hKey, "Esebcli2", NULL, &dwType,(LPBYTE)&lszValue, &dwSize);
    }

    RegCloseKey(hKey);




	typedef HRESULT (ESEBACK_API *HrESEBackupRestoreGetNodes) (
		IN WCHAR* wszComputerName,
		OUT PBACKUP_NODE_TREE *ppBackupNodeTree); 

	typedef void (ESEBACK_API *ESEBackupRestoreFreeNodes)(
		IN PBACKUP_NODE_TREE pBackupNodeTree
		);

	typedef HRESULT (ESEBACK_API *HrESEBackupPrepare)(
		IN  WCHAR *  		wszBackupServer,
		IN  WCHAR *  		wszBackupAnnotation,
		OUT unsigned long *				pcInstanceInfo,
		OUT INSTANCE_BACKUP_INFO ** 	paInstanceInfo,
		OUT HCCX * 			phccxBackupContext
		);

	typedef void (ESEBACK_API *ESEBackupFreeInstanceInfo)(
		IN unsigned long 				cInstanceInfo,
		IN INSTANCE_BACKUP_INFO * 		aInstanceInfo
		);

	typedef HRESULT (ESEBACK_API *HrESEBackupRestoreGetRegistered)(
		IN  WCHAR *  					wszServerName,
		IN  WCHAR *  					wszDisplayName,
		IN 	unsigned long  				fFlags,
		OUT unsigned long * 			pcRegisteredInfo,
		OUT ESE_REGISTERED_INFO ** 		paRegisteredInfo
		);

	typedef void (ESEBACK_API *ESEBackupRestoreFreeRegisteredInfo)(
		IN 	unsigned long 			cRegisteredInfo,
		IN  ESE_REGISTERED_INFO * 	aRegisteredInfo
		);

	
	typedef wchar_t WCHAR;

	LPVOID lpMsgBuf;

	HINSTANCE hMod;

	HrESEBackupRestoreGetNodes fnMNUBackupRestoreGetNodes = NULL;
	ESEBackupRestoreFreeNodes fnESEBackupRestoreFreeNodes = NULL;
	HrESEBackupPrepare fnHrESEBackupPrepare = NULL;
	ESEBackupFreeInstanceInfo fnESEBackupFreeInstanceInfo = NULL;
	HrESEBackupRestoreGetRegistered fnHrESEBackupRestoreGetRegistered = NULL;
	ESEBackupRestoreFreeRegisteredInfo fnESEBackupRestoreFreeRegisteredInfo = NULL;


	if (GetProcAddresses( &hMod, lszValue, 6,
		  &fnMNUBackupRestoreGetNodes, "HrESEBackupRestoreGetNodes",
		  &fnESEBackupRestoreFreeNodes, "ESEBackupRestoreFreeNodes",
		  &fnHrESEBackupPrepare, "HrESEBackupPrepare",
		  &fnESEBackupFreeInstanceInfo, "ESEBackupFreeInstanceInfo",
		  &fnHrESEBackupRestoreGetRegistered, "HrESEBackupRestoreGetRegistered",
		  &fnESEBackupRestoreFreeRegisteredInfo, "ESEBackupRestoreFreeRegisteredInfo"
	) == FALSE){
		printf ("--ERROR LoadLibrary failed\n");
		FormatMessage( 
			FORMAT_MESSAGE_ALLOCATE_BUFFER | 
			FORMAT_MESSAGE_FROM_SYSTEM | 
			FORMAT_MESSAGE_IGNORE_INSERTS,
			NULL,
			GetLastError(),
			0, // Default language
			(LPTSTR) &lpMsgBuf,
			0,
			NULL 
		);
		printf( (LPCTSTR)lpMsgBuf);
		LocalFree( lpMsgBuf );
		return 1;
	}



	BACKUP_NODE_TREE* pBackupNodeTree = NULL; 

	HRESULT hr; 

	hr = fnMNUBackupRestoreGetNodes (L"localhost", &pBackupNodeTree); 

    if (hr != S_OK)
    {
		printf("HRESULT GetNodes : %x ,GetLastError : %d\n" , hr,GetLastError()); 
		FormatMessage( 
			FORMAT_MESSAGE_ALLOCATE_BUFFER | 
			FORMAT_MESSAGE_FROM_SYSTEM | 
			FORMAT_MESSAGE_IGNORE_INSERTS,
			NULL,
			GetLastError(),
			0, // Default language
			(LPTSTR) &lpMsgBuf,
			0,
			NULL 
		);
		printf( (LPCTSTR)lpMsgBuf);
		LocalFree( lpMsgBuf );
		FreeLibrary (hMod);
		return -1;
	}
     

	WCHAR *Server = pBackupNodeTree->wszName;
	WCHAR *ServerName = pBackupNodeTree->pChildNode->wszName;


	wprintf(L"Server: %s\n", Server);
	wprintf(L"Server Name: %s\n", ServerName);


	unsigned long cRegisteredInfo;
	ESE_REGISTERED_INFO *paRegisteredInfo;

	hr = fnHrESEBackupRestoreGetRegistered( 
		L"localhost" , 
		Server ,
		0x00000001 ,
		&cRegisteredInfo, 
		&paRegisteredInfo); 
	
    if (hr != S_OK)
    {
		printf("--ERROR HRESULT GetRegistered : %x ,GetLastError : %d\n" , hr,GetLastError()); 
		FormatMessage( 
			FORMAT_MESSAGE_ALLOCATE_BUFFER | 
			FORMAT_MESSAGE_FROM_SYSTEM | 
			FORMAT_MESSAGE_IGNORE_INSERTS,
			NULL,
			GetLastError(),
			0, // Default language
			(LPTSTR) &lpMsgBuf,
			0,
			NULL 
		);
		printf( (LPCTSTR)lpMsgBuf);
		LocalFree( lpMsgBuf );
		FreeLibrary (hMod);
		return -1;
	}

	
	
	printf("Number Registered: %d\n", cRegisteredInfo );
	wprintf(L"Registered: %s\n", paRegisteredInfo->wszEndpointAnnotation );



    unsigned long InstanceInfo = 0;
    //INSTANCE_BACKUP_INFO *aInstanceInfo;
	//INSTANCE_BACKUP_INFO **aInstanceInfo  = (INSTANCE_BACKUP_INFO **) malloc ( cRegisteredInfo * sizeof ** aInstanceInfo );
	INSTANCE_BACKUP_INFO *aInstanceInfo = malloc( cRegisteredInfo * sizeof * aInstanceInfo );


    HCCX Context;

    hr = fnHrESEBackupPrepare (
                    ServerName,
                    paRegisteredInfo->wszEndpointAnnotation,
                    &InstanceInfo,
                    &aInstanceInfo,
                    &Context);

    if (hr != S_OK)
    {
		printf("--ERROR HRESULT BackupPrepare : %x ,GetLastError : %d\n" , hr,GetLastError()); 
		FormatMessage( 
			FORMAT_MESSAGE_ALLOCATE_BUFFER | 
			FORMAT_MESSAGE_FROM_SYSTEM | 
			FORMAT_MESSAGE_IGNORE_INSERTS,
			NULL,
			GetLastError(),
			0, // Default language
			(LPTSTR) &lpMsgBuf,
			0,
			NULL 
		);
		printf( (LPCTSTR)lpMsgBuf);
		LocalFree( lpMsgBuf );
		FreeLibrary (hMod);
		return -1;
	}
	
	fnESEBackupRestoreFreeNodes(pBackupNodeTree);
	fnESEBackupFreeInstanceInfo(InstanceInfo, aInstanceInfo);


	printf("\n\n\nThank You!\n");
	
	FreeLibrary (hMod);

	
	return 0;

}

esebcli2.h can be viewed at:


Thanks Again!
 
> printf( (LPCTSTR)lpMsgBuf);
Always do printf( "%s", lpMsgBuf);
If there are any % characters in lpMsgBuf, then printf() is in big trouble as it will try and convert parameters which don't exist.

> printf("Number Registered: %d\n", cRegisteredInfo );
Is this printing out what looks like a correct answer?

And this should just be
Code:
    hr = fnHrESEBackupPrepare (
                    ServerName,
                    paRegisteredInfo->wszEndpointAnnotation,
                    &InstanceInfo,
                    aInstanceInfo,  /* No & here */
                    &Context);

Also, your error printing code is essentially duplicated in many places. Can you make it a function?

--
 
I was going to clean up my code after (error functions)...

cRegisteredInfo is returning the correct value, "Microsoft Information Store" (that is the only one possible to return)...

I still get the same compile error "Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
 
Which line is giving you that error?

If it's the malloc line, then you're going to have to cast it if you're compiling it as C++.



--
 
I can't seam to get the casting right.... The line generating the error is:

INSTANCE_BACKUP_INFO *aInstanceInfo = malloc( cRegisteredInfo * sizeof * aInstanceInfo );

What should be the proper casing?

I remeber reading somewhere that INSTANCE_BACKUP_INFO is a pointer to a pointer... what type of issues woud this cause?
 
Try
Code:
INSTANCE_BACKUP_INFO *aInstanceInfo = (INSTANCE_BACKUP_INFO *)malloc( cRegisteredInfo * sizeof * aInstanceInfo );

Now I'm really confused.

The manual page I first quoted shows this
Code:
HRESULT HrESEBackupPrepare
(
    wchar*                 wszBackupServer,
    wchar*                 wszBackupAnnotation,
    unsigned long*         pcInstanceInfo,
    INSTANCE_BACKUP_INFO*  paInstanceInfo,
    HCCX*                  phccxBackupContext
);

The esebcli2.h in your link shows this - it has an extra STAR!!!
Code:
HRESULT ESEBACK_API HrESEBackupPrepare(
    IN  WCHAR *             wszBackupServer,
    IN  WCHAR *             wszBackupAnnotation,
    OUT unsigned long *	    pcInstanceInfo,
    OUT INSTANCE_BACKUP_INFO *[b][red]*[/red][/b]      paInstanceInfo,
    OUT HCCX *              phccxBackupContext
);

I don't know which one is correct, but for sure one of them is wrong. Nor do I know how to go about finding out which one of them to believe. I suppose the first question is where you got your header file from?

If your header file is correct, then my guess is you don't call malloc at all, but just invoke the function with [tt]&aInstanceInfo[/tt] as the parameter.
If the manual is correct, then malloc and just [tt]aInstanceInfo[/tt] as the parameter, and a cast to "fool" the prototype into doing the right thing.

--
 
That extra star has been getting me too... Leave it to Micro$oft... that header file is from Micor$oft also... I have tried that INSTANCE_BACKUP_INFO declaration... still no luck, same strange error from fnHrESEBackupPrepare... I don't think I am going to figure this one out...
 
You could
- fill in the "send feedback" link at the bottom of the MSDN help page I posted.
- search the Knowledge base to see if the problem has been mentioned, or if there is a patch to the SDK to fix it.


--
 
I have solved my problem with the token. It was a permssions issue (even though the account it was running from was Administrator which has Backup and Restore priv) and I had to use AdjustTokenPrivileges with a privilege of SE_BACKUP_NAME. (my original code worked no problem after that)

I thought I would post this because I came across so many threads with this issue with no resolution posted...


 
I've got the same problem, but i can't solve the problem with AdjustTokenPrivileges(), it doesnt work.

Can you explain me how u are using the function ?

thanks
 
the function:

HRESULT ModifyPrivilege(
IN LPCTSTR szPrivilege,
IN BOOL fEnable)
{
HRESULT hr = S_OK;
TOKEN_PRIVILEGES NewState;
LUID luid;
HANDLE hToken = NULL;

// Open the process token for this process.
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
&hToken ))
{
printf("Failed OpenProcessToken\n");
return ERROR_FUNCTION_FAILED;
}

// Get the local unique id for the privilege.
if ( !LookupPrivilegeValue( NULL,
szPrivilege,
&luid ))
{
CloseHandle( hToken );
printf("Failed LookupPrivilegeValue\n");
return ERROR_FUNCTION_FAILED;
}

// Assign values to the TOKEN_PRIVILEGE structure.
NewState.PrivilegeCount = 1;
NewState.Privileges[0].Luid = luid;
NewState.Privileges[0].Attributes =
(fEnable ? SE_PRIVILEGE_ENABLED : 0);

// Adjust the token privilege.
if (!AdjustTokenPrivileges(hToken,
FALSE,
&NewState,
sizeof(NewState),
NULL,
NULL))
{
printf("Failed AdjustTokenPrivileges\n");
hr = ERROR_FUNCTION_FAILED;
}

// Close the handle.
CloseHandle(hToken);

return hr;
}


and usage:

HRESULT hrmp = ModifyPrivilege(SE_BACKUP_NAME, TRUE);
if(hrmp!=S_OK)
{
printf("error modify privilege for backup\n");
return -1;
}
hrmp = ModifyPrivilege(SE_RESTORE_NAME, TRUE);
if(hrmp!=S_OK)
{
printf("error modify privilege for backup\n");
return -1;
}


hope that helps...
 
yes it helps :)

i try that and it worked:

HANDLE t;
if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &t)) {
printf("OpenProcessToken failed: %ld\n", GetLastError()); return false;
}
TOKEN_PRIVILEGES tp;
if(!LookupPrivilegeValue(NULL, SE_BACKUP_NAME, &tp.Privileges[0].Luid)) {
printf("LookupPrivilegeValue failed: %ld\n", GetLastError()); return false;
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(t, FALSE, &tp, 0, NULL, NULL);
if (GetLastError() != ERROR_SUCCESS) {
printf("AdjustTokenPrivileges failed: %ld\n", GetLastError()); return false;
}
CloseHandle(t);
printf(SE_BACKUP_NAME" enabled!\n");
return true;

I have other problems further in the process of the backup, in particular with the buffers.
i don't understand how it works, and msdn didn't help me a lot :(

is it possible (i totally understand if you refuse) to take a look at your complete source ?

after all is said, thanks a lot !
have a nice day

Olivier

olivier-huez@ NOSPAM wanadoo.fr
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top