Programming - cpueblo.com

NET SEND API 예제


글쓴이 : 유광희 날짜 : 2002-09-11 (수) 17:15 조회 : 16202
API 로 만든 예제 (첨부 파일 포함)








#define UNICODE
#define _UNICODE

#include
#include
#include
#include

#define MESGLEN 50
PSERVER_INFO_503 GetServerInfo(void); // gets & displays this systems name
void ListUsers(void); // lists users alias on this system
PWKSTA_USER_INFO_1 GetCurrentUser(void);

int wmain(int argc, wchar_t *argv[])
{
NET_API_STATUS nasRc; // return code from NetXxxx()
PWKSTA_USER_INFO_1 wks_user; // holds the ptr to the users name
PSERVER_INFO_503 si503; // Server info structure
LPWSTR awcFromName; // default machine name for NetMsgBufrSend()

//
// Get and display the server name, do this regardless
//
si503 = GetServerInfo();
wprintf(_T("You are logged into domain: %s.\\n"), si503->sv503_domain);

//
// Check command line arguments.
//
if(argc != 3)
{
if(argc == 2)
//
// List users on this system
//
if((argv[1][0] == '-' || argv[1][0] == '/') &&
(argv[1][1] == 'd' || argv[1][1] == 'D'))
ListUsers();

fwprintf(stderr, L"Usage: %s UserName_to_send_to \\"quoted message\\"\\n", argv[0]);
exit(1);
}

//
// Get and display the name of the user currently logged in
//
wks_user = GetCurrentUser();
wprintf(_T("You are user: %s\\n"), wks_user->wkui1_username);
awcFromName = wks_user->wkui1_username;

//
// Send a message
//
wprintf(_TEXT("Preparing to send message, please wait... this make take up to 1 minute.\\n"));
nasRc = NetMessageBufferSend(NULL, // server name (null means this machine)
argv[1], // name (alias) of recipient
awcFromName, // who this is from
(LPBYTE)argv[2], // message
wcslen(argv[2]) * sizeof(WCHAR));

//
// Display the return code (use nasRc-NERR_Base to lookup in lmerr.h)
//
switch(nasRc)
{
case ERROR_ACCESS_DENIED:
wprintf(_TEXT("ERROR: You do not have access to the requested function.\\n"));
break;

case ERROR_INVALID_PARAMETER:
wprintf(_TEXT("ERROR: One of the passed parameters is invalid.\\n"));
break;

case ERROR_NOT_SUPPORTED:
wprintf(_TEXT("ERROR: This network request is not supported.\\n"));
break;

case NERR_NameNotFound:
wprintf(_TEXT("ERROR: The user name could not be found or is not logged in."));
break;

case NERR_NetworkError:
wprintf(_TEXT("ERROR: A general failure occurred in the network hardware.\\n"));
break;

case NERR_Success:
wprintf(_TEXT("Message successfully sent!\\n"));
break;

default:
wprintf(_TEXT("ERROR %d: Unknown error\\n"), nasRc);
}

//
// Certain NetXxx() allocate memory that we must free
//
GlobalFree(wks_user);
GlobalFree(si503);
return(nasRc);
}


PWKSTA_USER_INFO_1 GetCurrentUser(void)
{
NET_API_STATUS nasRc; // return code from NetXxxx()
LPBYTE buff;

nasRc = NetWkstaUserGetInfo(NULL, 1, &buff);
if(nasRc != NERR_Success)
{
fwprintf(stderr, L"Error %d getting logged in user name.\\n", nasRc);
exit(2);
}
return((PWKSTA_USER_INFO_1) buff);
}


Downloads

  • alert.zip

  •