• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 316
  • Last Modified:

Using system ("net send ...")

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
scuzz1
Asked:
scuzz1
  • 4
  • 3
  • 2
  • +3
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:
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
 
scuzz1Author Commented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:
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
The IT Degree for Career Advancement

Earn your B.S. in Network Operations and Security and become a network and IT security expert. This WGU degree program curriculum was designed with tech-savvy, self-motivated students in mind – allowing you to use your technical expertise, to address real-world business problems.

 
Kent OlsenData Warehouse Architect / DBACommented:
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
 
scuzz1Author Commented:
I think the strcat (message, "\""); is what I'm looking for. I'll try it and get back to you.
0
 
Avik DasguptaCommented:
Hi shouldn't u insert a * i.e
net send * message

Avik.
0
 
sunnycoderCommented:
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
 
sunnycoderCommented:
grrr ... I recommend snprintf and write down a sprintf

snprintf ( message, 1000, " net send %s %s", receiver, send_this );
 ^                                                   ^^    
0
 
ankuratvbCommented:
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
 
Kent OlsenData Warehouse Architect / DBACommented:
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
 
sunnycoderCommented:
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
 
mdhmiCommented:

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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

  • 4
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now