只问耕耘
分类: 数据库开发技术
2007-06-21 15:01:08
......Lotus C API 访问数据库存取控制列表ACL (一)
二,实现
1,VC++ 创建Win32 Console Application 工程
2,修改工程设置
--工程->设置->C/C++->General->添加W32到预处理程序定义,如:WIN32,_DEBUG,_CONSOLE,_MBCS,W32
--工程->设置->C/C++->Code Generation->Struct member alignment->选择"1 Byte"
--工程->添加工程->文件,把notes.lib添加到工程里
3,添加代码如下:
//acl.c
/* OS and C include files */
#include
/* Notes API include files */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DBName "acl.nsf"
#define Username "AddedbyCAPI"
void PrintAPIError (STATUS);
int main(int argc, char *argv[])
{
STATUS error=NOERROR;
DBHANDLE hDB;
HANDLE hACL;
NOTEHANDLE hNote;
WORD ClassView = NOTE_CLASS_VIEW;
char szViewName[] = "Test View";
char szSelFormula[] = "@ALL";
WORD wdc; /* "We Don't Care" - We're not interested in some of
* the info passed back by NSFFormulaCompile(), but
* the function call requires the addresses of several
* words to be passed in. The address of this word
* is used for all parameters in which we have no
* interest.
*/
FORMULAHANDLE hSelFormula = NULLHANDLE;
WORD wSelFormulaLen;
WORD wViewFormatBufLen;
HANDLE hViewFormatBuffer;
char *pViewFormatBuffer;
char *pVFBuf;
/*
* Variables pertaining to the $VIEWFORMAT item.
*/
VIEW_TABLE_FORMAT ViewTableFormat;
if (error = NotesInitExtended (argc, argv))
{
printf("\n Unable to initialize Notes.\n");
return (1);
}
if(error=NSFDbCreate(DBName,DBCLASS_NOTEFILE,FALSE))
{
PrintAPIError (error);
//NotesTerm();
//return (1);
goto dbexist;
}
if (error = NSFDbOpen (DBName, &hDB))
{
PrintAPIError (error);
NotesTerm();
return (1);
}
printf("database opened\n");
/*
* Now create a note in database
*/
error = NSFNoteCreate( hDB, &hNote );
if (error)
{
printf("Error: unable to create note in database.\n" );
NSFDbClose( hDB );
PrintAPIError (error);
NotesTerm();
return (1);
}
/*
* Set the NOTE_CLASS to NOTE_CLASS_VIEW.
*/
NSFNoteSetInfo( hNote, _NOTE_CLASS, &ClassView );
/*
* Set the view name. ($TITLE field)
*/
error = NSFItemSetText( hNote,
VIEW_TITLE_ITEM,
szViewName,
MAXWORD );
if (error)
{
printf("Error: unable to set text item '%s' in view note.\n",
VIEW_TITLE_ITEM );
NSFNoteClose( hNote );
NSFDbClose( hDB );
PrintAPIError (error);
NotesTerm();
return (1);
}
/*
* Compile and set the selection formula to show all notes.
*/
error = NSFFormulaCompile( NULL,
0,
szSelFormula,
(WORD) strlen(szSelFormula),
&hSelFormula,
&wSelFormulaLen,
&wdc, &wdc, &wdc,
&wdc, &wdc );
if (error)
{
printf("Error: unable to compile selection formula '%s'.\n",
szSelFormula );
NSFNoteClose( hNote );
NSFDbClose( hDB );
PrintAPIError (error);
NotesTerm();
return (1);
}
/*
* Get the size of the merged formula.
*/
error = NSFFormulaGetSize( hSelFormula, &wSelFormulaLen );
if (error)
{
printf("Error: unable to get size of selection formula.\n" );
if (hSelFormula != NULLHANDLE)
{
OSMemFree( hSelFormula );
}
NSFNoteClose( hNote );
NSFDbClose( hDB );
PrintAPIError (error);
NotesTerm();
return (1);
}
/*
* Append the selection formula item to the view note.
*/
error = NSFItemAppend( hNote,
ITEM_SUMMARY,
VIEW_FORMULA_ITEM,
(WORD) strlen(VIEW_FORMULA_ITEM),
TYPE_FORMULA,
OSLockObject(hSelFormula),
(DWORD) wSelFormulaLen);
OSUnlockObject( hSelFormula );
if (error)
{
printf("Error: unable to append item '%s' to view note.\n",
VIEW_FORMULA_ITEM );
NSFNoteClose( hNote );
NSFDbClose( hDB );
PrintAPIError (error);
NotesTerm();
return (1);
}
/*
* Create the $VIEWFORMAT item. The $VIEWFORMAT item is an item
* of TYPE_VIEW_FORMAT with name VIEW_VIEW_FORMAT_ITEM.
*
* The $VIEWFORMAT item for this view will consist of the following
* series of structures converted to Domino and Notes canonical format and packed
* together:
*
* VIEW_TABLE_FORMAT
* VIEW_COLUMN_FORMAT (for column 1)
* VIEW_COLUMN_FORMAT (for column 2)
* VIEW_COLUMN_FORMAT (for column 3)
* Item Name for column 1
* formula for column 1
* Item Name for column 2
* Title for column 2
* formula for column 2
* Item Name for column 3
* Title for column 3
* formula for column 3
* VIEW_TABLE_FORMAT2
* VIEW_COLUMN_FORMAT2 (for column 1)
* VIEW_COLUMN_FORMAT2 (for column 2)
* VIEW_COLUMN_FORMAT2 (for column 3)
* VIEW_TABLE_FORMAT3
*
*
* First, allocate a buffer, pViewFormatBuffer, that will contain the
* entire $VIEWFORMAT item.
*/
wViewFormatBufLen = ODSLength( _VIEW_TABLE_FORMAT ) ;
if (error = OSMemAlloc( 0, wViewFormatBufLen, &hViewFormatBuffer ))
{
printf("Error: unable to allocate %d bytes memory.\n",
wViewFormatBufLen);
if (hSelFormula != NULLHANDLE)
{
OSMemFree( hSelFormula );
}
NSFNoteClose( hNote );
NSFDbClose( hDB );
PrintAPIError (error);
NotesTerm();
return (1);
}
pViewFormatBuffer = (char*) OSLockObject( hViewFormatBuffer );
memset( pViewFormatBuffer, 0, (size_t) wViewFormatBufLen );
/*
* Initialize pVFBuf. pViewFormatBuffer will remain pointing to the top
* of the buffer. pVFBuf will move to point to the next available byte.
*/
pVFBuf = pViewFormatBuffer;
/*
* Initialize the VIEW_TABLE_FORMAT structure.
*/
/* init to all zeros */
memset (&ViewTableFormat, 0, sizeof(VIEW_TABLE_FORMAT));
ViewTableFormat.Header.Version = VIEW_FORMAT_VERSION;
ViewTableFormat.Header.ViewStyle = VIEW_STYLE_TABLE;
ViewTableFormat.ItemSequenceNumber = 0; /* Reserved - should be 0 */
ViewTableFormat.Flags = VIEW_TABLE_FLAG_FLATINDEX |
VIEW_TABLE_FLAG_DISP_UNREADDOCS |
VIEW_TABLE_FLAG_CONFLICT;
ViewTableFormat.Flags2 = 0;
/*
* Call ODSWriteMemory to convert the VIEW_TABLE_FORMAT structure from
* host-specific format to Domino and Notes canonical format, and copy it into the
* buffer at location pVFBuf. ODSWriteMemory increments pVFBuf to point
* to the next byte in the buffer after the written data structure.
*/
ODSWriteMemory( &pVFBuf, _VIEW_TABLE_FORMAT, &ViewTableFormat, 1 );
/*
* Done constructing the view note. Now store the view note
* in the database.
*/
error = NSFNoteUpdate( hNote, 0 );
if (error)
{
printf("Error: unable to update view note in database.\n" );
}
else
{
printf("Successfully created view note in database.\n" );
printf("\nProgram completed successfully.\n" );
}
dbexist:
/* Get the ACL - the sample database contains the default ACL */
if (error = ACLCreate(&hACL))
{
NSFDbClose (hDB);
PrintAPIError (error);
NotesTerm();
return (1);
}
printf("database ACL Created\n");
//Add user
if (error = ACLAddEntry (hACL, /* handle to ACL */
Username, /* Name to add */
ACL_LEVEL_EDITOR, /* Access level */
NULL, /* Role bits */
ACL_FLAG_NODELETE)) /* Access level privilege
flag -
do not allow user to delete
docs */
{
OSMemFree (hACL);
NSFDbClose (hDB);
PrintAPIError (error);
NotesTerm();
return (1);
}
printf("database ACL Entry added\n");
if (error = NSFDbStoreACL (hDB, /* database handle */
hACL, /* ACL handle */
0L, /* Reserved */
1)) /* Method - 0 to Update ACL
1 for store new
ACL */
{
OSMemFree (hACL);
NSFDbClose (hDB);
PrintAPIError (error);
NotesTerm();
return (1);
}
printf("database ACL Entry stored\n");
/* Free the memory associated with the ACL */
OSMemFree (hACL);
/* Close the database */
if (error = NSFDbClose (hDB))
{
PrintAPIError (error);
NotesTerm();
return (1);
}
/* if there's no error print success message */
printf("\n%s was successful added to %s.\n",Username,DBName);
printf("\n\nProgram completed successfully.\n");
NotesTerm();
return(0);
}
void PrintAPIError (STATUS api_error)
{
STATUS string_id = ERR(api_error);
char error_text[200];
WORD text_len;
/* Get the message for this Lotus C API for Domino and Notes error code
from the resource string table. */
text_len = OSLoadString (NULLHANDLE,
string_id,
error_text,
sizeof(error_text));
/* Print it. */
fprintf (stderr, "\n%s\n", error_text);
}