Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

access NET SEND using C++

Posted on 2004-09-20
4
Medium Priority
?
303 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

610 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