Solved

access NET SEND using C++

Posted on 2004-09-20
4
288 Views
Last Modified: 2010-04-01
Hello,

I am using windows 2000.
I want to write c++ program that use "NET" program with "SEND" command.
so far I success by using
system("NET SEND d213213 jashdjkhasjkd");

1)
but I need to get the error, and I can not do it maybe It can be done by getting the data from cerr
if so I want to take it char[], but I don't now elegant way to do this.

OR

2) API of NET SEND will help

Thanks in advance
Nir
0
Comment
Question by:nir2002
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 22

Expert Comment

by:grg99
ID: 12101525
A bit baroque, but you could wrap it in a batch file:

net send ... yadda...  1>ErrorFile   2>ErrorFile
echo %ErrorLevel% >>ErrorFile

0
 
LVL 48

Expert Comment

by:AlexFM
ID: 12101574
This sample shows how to create process and programmatically read it's output. If NET returns meaningfull exit code, you can read it using GetExitCodeProcess function.
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 12102036
Don't use 'net send', use the respective Win32 API 'NetMessageBufferSend()':

http://win32.mvps.org/network/nmbs.html

#include <windows.h>
#include <lm.h>
#include <stdio.h>
#include <stdlib.h>
#pragma hdrstop
#pragma comment( lib, "netapi32.lib" )



#define MAXLEN 256



void enum_names( const wchar_t *server );



int main( int argc, char *argv[] )
{
      DWORD rc;
      wchar_t server[MAXLEN], name[MAXLEN], from[MAXLEN], msg[MAXLEN];

      if ( argc != 5 && argc != 2 )
      {
            puts( "\nusage: nmbs \\\\server message-name from-name \"message\"" );
            puts( "       nmbs \\\\server\n" );
            puts( "First form: Sends the message to the <message-name> alias on <server>." );
            puts( "       Use whatever you like as <fromname>." );
            puts( "Second form: Lists <message-name>s on <server>. Note that not all of" );
            puts( "       them may work." );

            return 1;
      }

      mbstowcs( server, argv[1], MAXLEN );
      server[MAXLEN - 1] = L'\0';

      if ( argc == 2 )
      {
            enum_names( server );
      }
      else
      {
            mbstowcs( name, argv[2], MAXLEN );
            name[MAXLEN - 1] = L'\0';
            mbstowcs( from, argv[3], MAXLEN );
            from[MAXLEN - 1] = L'\0';
            mbstowcs( msg, argv[4], MAXLEN );
            msg[MAXLEN - 1] = L'\0';

            printf( "\nTrying ... " );
            rc = NetMessageBufferSend( server, name, from, (byte *) &msg[0], wcslen( msg ) * 2 );

            if ( rc != NERR_Success )
            {
                  printf( "NMBS() returned %lu\n", rc );
                  return 1;
            }

            puts( "Done." );
      }

      return 0;
}



void enum_names( const wchar_t *server )
{
      MSG_INFO_1 *buf, *cur;
      DWORD read, total, resumeh, rc, i;

      printf( "\nAvailable message-names on server %S:\n", server );
      resumeh = 0;
      do
      {
            buf = NULL;
            rc = NetMessageNameEnum( server, 1, (BYTE **) &buf,
                  512, &read, &total, &resumeh );
            if ( rc != ERROR_MORE_DATA && rc != ERROR_SUCCESS )
                  break;

            for ( i = 0, cur = buf; i < read; ++ i, ++ cur )
            {
                  // Note: the capital S in the format string will expect Unicode
                  // strings, as this is a program written/compiled for ANSI.
                  printf( "%S\n", cur->msgi1_name );
            }

            if ( buf != NULL )
                  NetApiBufferFree( buf );

      } while ( rc == ERROR_MORE_DATA );

      if ( rc != ERROR_SUCCESS )
            printf( "NMNE() returned %lu\n", rc );
}

Calling external programs to do the work is always the worst of two choices.

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question