3

Hi I am trying to create a app that uses the msg.exe to send messages over a network.

When i execute msg from cmd everything works fine, but when i open cmd with the form i am unable to, went to the system32 folder with cmd and the file is not shown there, but when i browse or use cmd normally it is and evrything works

tested it on another computer and app works fine, running win 7 64 bit on this 1.

Here is a code sample that i use to open cmd:

Process.Start("cmd");

i am running as admin i have tried executed it directly from msg.exe aswell, it seems to be a problem on 64 bit works on all 32 bit systems but not on any 64bit

edit: ok i found the problem when running 64bit 32 bit applications cannot run 64 bit apps in the system 32 folder. when trying to access this folder it redirects you to %WinDir%\SysWOW64 a work around is to use this path C:\Windows\Sysnative\file (%windir%\Sysnative)

Splendid
  • 53
  • 1
  • 6
  • Add some code and make you question a little bit more clear. What is your problem? – Restuta Jan 13 '11 at 12:37
  • When you've tried executing `msg.exe` directly from the command prompt, are you logged in as an Administrator? – Cody Gray - on strike Jan 13 '11 at 12:52
  • Yes im running as admin, tried using msg.exe directly used Process.Start("msg.exe", "/C msg /server:xxx xxx message"); gave me error system cannot find file specified – Splendid Jan 13 '11 at 13:43

6 Answers6

2

The solution mentioned in the question was what did the trick for me - posting testable solution here for posterity:

public class Messenger : IMessenger
{
    private readonly IProcessWrapper _process;

    public Messenger(IProcessWrapper process)
    {
        _process = process;
    }

    public void SendMessage(string message)
    {
        var info = new ProcessStartInfo
            {
                WorkingDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "sysnative"),
                FileName = "msg.exe",
                Arguments = string.Format(@" * ""{0}""", message),
                WindowStyle = ProcessWindowStyle.Hidden,
                UseShellExecute = true,
                Verb = "runas"
            };
        _process.Start(info);
    }
}


public interface IProcessWrapper : IDisposable
{
    IEnumerable<Process> GetProcesses();
    void Start(ProcessStartInfo info);
    void Kill();

    bool HasExited { get; }
    int ExitCode { get; }
}
Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
1

Perhaps you shoud check the "target platform" to build to. On my 64-bit win7 pc, I should choose "x64" or "Any CPU" to let my code to find "msg.exe".

Ky Wu
  • 86
  • 1
  • 2
1

Do you need to use cmd at all? Why not use Process.Start to call msg.exe directly. If you know where it is, you should be able to run it.

Tor Haugen
  • 19,509
  • 9
  • 45
  • 63
1

If you are in an app, you are probably better off executing "msg" directly

Process.Start("msg");

or

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "msg.exe";
startInfo.Arguments = "/SERVER hellowword";
startInfo.WorkingDirectory = @"C:\Temp";
startInfo.WindowStyle = ProcessWindowStyle.Maximized;
startInfo.ErrorDialog = true;
Process process = Process.Start(startInfo);
djeeg
  • 6,685
  • 3
  • 25
  • 28
  • tried that code gives me a error windows cant find msg.exe, its a bit weird i did put code directly into it aswell, but it doesnt seem to find the file, so then i used cmd and checked the directory with it and it doesnt list it. If i use cmd normally or browse the folder the file is there. even tried making a shortcut and executing that says the shortcut isnt linked to anything. – Splendid Jan 13 '11 at 12:49
1
Process p = new Process();
System.Diagnostics.ProcessStartInfo sinfo = new System.Diagnostics.ProcessStartInfo("C:\\Windows\\System32\\msg.exe");
p.StartInfo.Arguments=String.Format("/server:{0} {1} {2}",toServer,string.Compare(toUser.Trim(), "") == 0 ? "*" : toUser,message);
p.StartInfo = sinfo;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.FileName = "msg.exe";
p.Start();

You may need to set the "Username" and "Password" for the StartInfo of the process.("msg.exe" is residing in a system folder and the user running the code not having the appropriate permissions to run from that folder.)

abmv
  • 7,042
  • 17
  • 62
  • 100
  • sorry im still a bit new to coding, do i need to change any values in that code section?, the toServer and toUser says it doesnt not exist in current context – Splendid Jan 13 '11 at 13:27
  • Ya u need to set stuff (Anything that appears red in VS) ! (Also maybe p.StartInfo.UserName | p.StartInfo.Password need to be set) – abmv Jan 13 '11 at 13:29
  • any chance u can give me a bit of an example? :P – Splendid Jan 13 '11 at 13:36
0

On some Windows editions (e.g. Home, not Professional/Business etc.) msg.exe is not included.

peenut
  • 3,366
  • 23
  • 24