Symantec logo

Example

The following example illustrates the use of the Windows-based primitives within an agent for FileOnOff.

/* @(#)src/agent/w2k/FileOnOff/FileOnOff.C 2.4 01/04/03 11:57:11 - */

static const char sccsid[] = "@(#)VCS:src/agent/w2k/FileOnOff/FileOnOff.C 2.4";

/*

* START COPYRIGHT-NOTICE: 1998, 2003

*

* Copyright 2006 Symantec Corporation.

* All rights reserved.

*

*/

#include <stdio.h>

#include <windows.h>

#include "VCSAgApi.h"

WCHAR *ConvertToUnicodeString(const char *attr_val)

{

int len = 0,outLen ;

WCHAR * szOutStr = NULL;

int num_bytes = 0, num_written = 0;

len = strlen(attr_val);

// Call the API to convert the UTF-8 encoded string into a

// UCS-2 encoded string

VCSAgEncodeString(VCSAgUTF8, /* convert from UTF8 */

(void *)attr_val,

len,

VCSAgUCS2, /* to the UCS2 format */

(void **)&szOutStr,

&outLen);

return szOutStr;

}

///////////////////////////////////////////////////////////////////////////////

// Function: res_monitor

//

// Description: Determine if the given file is online (file exists)

// or offline (file does not exist).

//

// Notes: None

///////////////////////////////////////////////////////////////////////////////

VCSAgResState res_monitor(const char *res_name, void **attr_val,

int *conf_level)

{

VCSAG_LOG_INIT("res_monitor");

VCSAgResState state;

DWORD attrs ;

// Temporary variables for sample code

WCHAR * szPath;

WCHAR *mymem = NULL;

WCHAR **mymem2 = NULL;

WCHAR **new_args = NULL;

int ret = 0;

int indx = 0;

int temp_number = 0;

// Dummy structure defined for the purpose of example code

struct mystruct{

int a;

WCHAR b[32];

} *inst1;

/*****************************************************************

* Sample code for using the new AGFW APIs

*****************************************************************/

// Allocate 32 bytes of type WCHAR for mymem

mymem = VCSAgNew(WCHAR, 32);

if (mymem) {

// Memory allocation succeeded

VCSAgStrlcpyW(mymem, L"copy ", 32);

VCSAgStrlcatW(mymem, L"concatenate", 32);

VCSAG_LOGDBG_MSG(VCS_DBG1, VCS_DEFAULT_FLAGS,

L"Sample string is %s", mymem);

temp_number = 10;

VCSAgSnprintfW(mymem, 32,

L"Testing VCSAgSnprintfW...temp_number is

%d\n", temp_number);

VCSAG_LOGDBG_MSG(VCS_DBG1, VCS_DEFAULT_FLAGS,

L"Sample string is %s", mymem);

// Delete memory allocated to mymem

VCSAgDelete((void *)mymem);

mymem = NULL;

} else {

// Memory allocation failed

VCSAG_LOG_MSG(VCS_NOTICE, 1005, VCS_DEFAULT_FLAGS,

L"Failed to allocate memory for mymem");

}

// Allocating memory for a struct now

inst1 = VCSAgNew(struct mystruct, 1);

if (inst1) {

// Memory allocation succeeded

VCSAgStrlcpyW(inst1->b,

L"Copying dummy string", 32);

VCSAG_LOGDBG_MSG(VCS_DBG1, VCS_DEFAULT_FLAGS,

L"Sample string is %s", inst1->b);

// Delete memory allocated to inst1

VCSAgDelete(inst1);

} else {

// Memory allocation failed

VCSAG_LOG_MSG(VCS_NOTICE, 1006, VCS_DEFAULT_FLAGS,

L"Failed to allocate memory for inst1");

}

// Allocating memory for an array of pointers

// Here, there will be four entries in the array

myptr = VCSAgNew(WCHAR *, 4);

if (myptr) {

// Memory allocation succeeded

myptr[0] = VCSAgNew(WCHAR, 16);

VCSAgStrlcpyW(myptr[0], L"One", 16);

myptr[1] = VCSAgNew(WCHAR, 16);

VCSAgStrlcpyW(myptr[1], L"Two", 16);

VCSAG_LOGDBG_MSG(VCS_DBG1, VCS_DEFAULT_FLAGS,

L"Contents of myptr: %s and %s",

myptr[0], myptr[1]);

// Free the memory allocated

VCSAgDelete(myptr[0]);

VCSAgDelete(myptr[1]);

VCSAgDelete(myptr);

}

// Call the function that uses the VCSAgEncodeString API

szPath = ConvertToUnicodeString((char *)attr_val[0]);

VCSAG_LOGDBG_MSG(VCS_DBG1, VCS_DEFAULT_FLAGS,

L"Encoded string is %s", szPath);

// Sample usage of VCSAgGetEncodedArgList API

ret = VCSAgGetEncodedArgList(VCSAgUTF8, attr_val,

VCSAgUCS2, (void ***)&new_args);

if (ret) {

// Encode API failed

VCSAG_LOG_MSG(VCS_NOTICE, 1007, VCS_DEFAULT_FLAGS,

L"Failed to encode the ArgList entries");

} else {

// Encode succeeded

VCSAG_LOGDBG_MSG(VCS_DBG1, VCS_DEFAULT_FLAGS,

L"The encoded arg list values are:");

while (new_args[indx] != NULL) {

VCSAG_LOGDBG_MSG(VCS_DBG1, VCS_DEFAULT_FLAGS,

L"arg %d is %s", indx, new_args[indx]);

indx++;

}

// Sample usage of API to delete the encoded

//ArgList entries

VCSAgDelEncodedArgList((void **)new_args);

}

/*****************************************************************

* End of sample code for using the new AGFW APIs

*****************************************************************/

:

// Actual code here

:

:

VCSAG_RES_LOG_MSG(VCS_DBG1, VCS_DEFAULT_FLAGS,

L"Monitor is successful");

if(szPath)

VCSAgDelString((void *)szPath);

return state;

}

:

// Rest of the agent code

:

: