Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using system ("net send ...")

Posted on 2004-04-01
12
Medium Priority
?
311 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
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 46

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 46

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 46

Accepted Solution

by:
Kent Olsen earned 2000 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:Avik Dasgupta
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 46

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays 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.
Suggested Courses

926 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