Solved

Using system ("net send ...")

Posted on 2004-04-01
12
306 Views
Last Modified: 2010-04-15
I inherited a Access DB that uses a bunch of .bat files that net sends all the users of this DB when it needs repaired and when it is back up. I would like to consolidate these into a c prog. that reads from a list of users instead. I'm having problems getting the message into a string variable and then sending it.

does anyone know how to do this?

Thanks in advance.

Jim
0
Comment
Question by:scuzz1
[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
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 10734170
Hi scuzz1,

There are several problem areas here concerning strings.  In a logical sense, you're really going to have to build several strings.  Here are the basic steps that you'll need to take:

1.  Build the string the you want to send.
2.  Encapsulate it with quotes.
3.  Append the string after the "net " command string.
    The result will be 'net "some message"'.
4.  Convert the embedded double quotes to backslash double-quote.

The resulting string will look like "net \"some message\"".

If the string that you're going to send has embedded quotes, you'll need to perform another round of conversion on the quotes.

Kent
0
 

Author Comment

by:scuzz1
ID: 10734223
Hi Kent,

I have all the messages I need pasted together into string vars (including the "net send" command) I was trying to use it like this

  system ( varname )

Can I do it like that?
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 10734250
Hi scuzz1,

Sure.  The code should resemble this:


  char Message[1000];
  char *Text = "Send This";

  strcpy (Message, "net ");
  strcat (Message, "\"");
  strcat (Message, Text);
  strcat (Message, "\"");

  system (Message);


Kent
0
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!

 
LVL 45

Accepted Solution

by:
Kent Olsen earned 500 total points
ID: 10734263
Hi scuzz1,

Make that:


  char Message[1000];
  char *Text = "Send This";

  strcpy (Message, "net send ");   /* Missed this the first time around  :()  */
  strcat (Message, "\"");
  strcat (Message, Text);
  strcat (Message, "\"");

  system (Message);


Kent
0
 

Author Comment

by:scuzz1
ID: 10734295
I think the strcat (message, "\""); is what I'm looking for. I'll try it and get back to you.
0
 
LVL 2

Expert Comment

by:Avik77
ID: 10734367
Hi shouldn't u insert a * i.e
net send * message

Avik.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10738493
you can combine all these statements into a single snprintf and it is safer too

sprintf ( message, 1000, " net send %s", send_this );

you can read the string send_this from the user at run time

system (message );
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10738510
grrr ... I recommend snprintf and write down a sprintf

snprintf ( message, 1000, " net send %s %s", receiver, send_this );
 ^                                                   ^^    
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 10739797
Hi,
if i remember correctly,the syntax of net send was:
 net send terminal number "message"
e.g. net send 4-33 "hello world"

char str[1000];
char term[100];

char msg[100];
strcpy(msg,"Repair needed etc....");

for(i=0;i<limit;i++)
{
strcpy(str,"net send ");
strcat(str,term); //where term stores the terminal number
strcat(str,"\"");
strcat(str,msg);//where msg stores the message which is the same for all users.
strcat(str,"\"");
system(str);
//change the terminal number for the next terminal in the string term
//if ur terminal numbers are just normal integer type numbers,use an integer
//keep incrementing them and use the itoa() function to convert them into string
//or as u said,u can store them in a double dimensional array of char(but that would take
//more space if the number of terminals is large).In that case,use term[i] where term[][] is where u store the terminal number
}

U said that u stored the entire command in strings.
the message is common to everyone so the only thing that changes in the command is the terminal number,just store those in a char[][] array and if they are numbers in some order
u can also use integers and the itoa() function
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 10740113
Hi Sunny,

The "wild" card in all of this is special characters.  That's why I suggested that the poster contain the data to be sent in quotes.

net send <destination> S t r i n g  w i t h  d o u b l e  s p a c i n g

Works only when the message is quoted.  And of course, any time that the string contains embedded quotes, the string must be converted to pass them properly.


Sure would be easier in *nix, huh?  A simple script would do all of the heavy lifting and keep the poster from having to maintain another program.  :)

Kent
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10740399
Hi Kent,

You are right ... *nix is far more suitable for this kind of stuff ...
Perhaps the reason why programmer's prefer it ... or is it too addictive ;o)
0
 
LVL 1

Expert Comment

by:mdhmi
ID: 10742303

Which OS is this running on?  You could construct your 'net send' using the Windows API, however, that would require much more effort then just calling the 'system' function with your 'net send'.

Mark

0

Featured Post

Technology Partners: 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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

627 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