Novell Home

How to open a file with NWOpenNSEntry()

From Developer Community

This sample shows how to use NWOpenNSEntry() to open a file. You can build it as a Win32 program or as an NLM.

Sample Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef __MWERKS__
#pragma ANSI_strict off
#endif
#include <nwcalls.h>
#include <nwclxcon.h>
#ifdef __MWERKS__
#pragma ANSI_strict reset
#endif

int main( int argc, char* argv[]) {
	char const* server_name;
#ifdef N_PLAT_NLM
	char const* login_user_name;
	char const* password;
#endif
	char const* filename;
	NW_NS_OPENCREATE open_info;
	NWFILE_HANDLE file_handle;
	NWRCODE rcode;
	NWCCODE ccode;
	NWCONN_HANDLE conn_handle;
#ifdef N_PLAT_NLM
	if ( argc != 5) {
		printf( "Usage: %s <servername> <username> <password> <filename>\n", *argv);
		return EXIT_FAILURE;
	}
#else
	if ( argc != 3) {
		printf( "Usage: %s <servername> <filename>\n", *argv);
		return EXIT_FAILURE;
	}
#endif
	ccode = NWCallsInit( NULL, NULL);
	if ( ccode != 0) {
		printf( "NWCallsInit failed, ccode = %x\n", ccode);
		return EXIT_FAILURE;
	}
	strupr( *++argv);
	server_name = *argv;
#ifdef N_PLAT_NLM
	strupr( *++argv);
	login_user_name = *argv;
	strupr( *++argv);
	password = *argv;
#endif
	strupr( *++argv);
	filename = *argv;
	rcode = NWCCOpenConnByName( 0,
		server_name,
		NWCC_NAME_FORMAT_BIND,
		NWCC_OPEN_LICENSED,
		NWCC_TRAN_TYPE_WILD,
		&conn_handle);
	if ( rcode != 0) {
		printf( "NWCCOpenConnByName failed, rcode = %x\n", rcode);
		NWCallsTerm( NULL);
		return EXIT_FAILURE;
	}
#ifdef N_PLAT_NLM
	ccode = NWLoginToFileServer( conn_handle, login_user_name, OT_USER, password);
	if ( ccode != 0) {
		printf( "NWLoginToFileServer failed, ccode = %x\n", ccode);
		NWCCCloseConn( conn_handle);
		NWCallsTerm( NULL);
		return EXIT_FAILURE;
	}
#endif
	open_info.openCreateMode = OC_MODE_OPEN;
	open_info.searchAttributes = SA_ALL;
	open_info.reserved = 0;
	open_info.createAttributes = A_NORMAL;
	open_info.accessRights = AR_READ;
	open_info.NetWareHandle = 0;
	open_info.openCreateAction = OC_ACTION_OPEN;
	ccode = NWOpenNSEntry( conn_handle, 0, NW_NS_LONG, 0, filename, &open_info, &file_handle);
	if ( ccode != 0) {
		printf( "NWOpenNSEntry failed, ccode = %x\n", ccode);
		NWCCCloseConn( conn_handle);
		NWCallsTerm( NULL);
		return EXIT_FAILURE;
	}
	NWCCCloseConn( conn_handle);
	NWCallsTerm( NULL);
	return EXIT_SUCCESS;
}

--Dmitry Mityugov

Novell® Making IT Work As One

© 2008 Novell, Inc. All Rights Reserved.