Jason N. Gaylord
Jason N. Gaylord

Reputation: 8324

Use PInvoke to Capture CD Rom Information of Remote Machine

I'm using C# to call GetVolumeInformation on a remote machine. I can hit the remote harddrives easily as there is a default share setup of c$ or whatever. However, CD/DVD do not have a default setup. How can I read the remote CD/DVD drive using a PInvoke call or something else?

If I can't do it using C#, I could always use PowerShell or WMI.

Upvotes: 1

Views: 344

Answers (2)

RRUZ
RRUZ

Reputation: 136441

The WMI allows you to get system information of a remote machine without problems, only you need set the remote WMI access in the machine and use a valid user and password. on this case you can use the Win32_LogicalDisk and Win32_CDROMDrive classes to retrieve the info which you need.

Try this C# sample.

using System;
using System.Collections.Generic;
using System.Management;
using System.Text;

namespace GetWMI_Info
{
    class Program
    {

        static void Main(string[] args)
        {
            try
            {
                string ComputerName = "localhost";//set the remote machine name here
                ManagementScope Scope;                

                if (!ComputerName.Equals("localhost", StringComparison.OrdinalIgnoreCase)) 
                {
                    ConnectionOptions Conn = new ConnectionOptions();
                    Conn.Username  = "";//user
                    Conn.Password  = "";//password
                    Conn.Authority = "ntlmdomain:DOMAIN";
                    Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), Conn);
                }
                else
                    Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", ComputerName), null);

                Scope.Connect();
                ObjectQuery Query = new ObjectQuery("SELECT * FROM Win32_CDROMDrive");
                ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);

                foreach (ManagementObject WmiObject in Searcher.Get())
                {
                    Console.WriteLine("{0,-35} {1,-40}","DeviceID",WmiObject["DeviceID"]);// String
                    Console.WriteLine("{0,-35} {1,-40}","Drive",WmiObject["Drive"]);// String

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Exception {0} Trace {1}",e.Message,e.StackTrace));
            }
            Console.WriteLine("Press Enter to exit");
            Console.Read();
        }
    }

}

Upvotes: 2

CB.
CB.

Reputation: 60956

Using Powershell and WMI.

Try this:

 Get-WmiObject -computername MyremotePC Win32_CDROMDrive | Format-List *

You need administrative credential on remote computer.

You can P/invoke in powershell the GetVolumeInfomation adding it as type using Add-Type (Some example here).

If you're trying to read data on the disk of a remote CD/DVD that isn't shared I don't know any way to do it.

Upvotes: 1

Related Questions