Solved

Using system ("net send ...")

Posted on 2004-04-01
12
301 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 45

Expert Comment

by:Kdo
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:Kdo
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
 
LVL 45

Accepted Solution

by:
Kdo 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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:Kdo
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

708 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now