Solved

access NET SEND using C++

Posted on 2004-09-20
4
278 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
  • 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: 12101551
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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

829 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