Hi
I need some good study material related to it. How can i eject my DVD ROM drive using C coding. similarly, how can i change fan speed, how can i shut down my PC using C coding. I need urgently.

Recommended Answers

All 5 Replies

DVD drive and fan control depends on the system and mobo/DVD drive, as well as your operating system. This is not like DOS where you can control everything you want, from your program. THAT was WAY too hacker/virus/malware friendly. Nowadays, anything you want to do like this, you do it through the operating system. That means the answer depends on the operating system you have.

Same with system shutdown. You can't have a rogue program shutting down computers all over, whenever they want.

To shut down the computer on Windows, you can use this line. The system command works like the command prompt and will shutdown the computer silently.

system("C:\\WINDOWS\\System32\\shutdown /s");

From what I understand it is not easy to obtain and change your fan speeds without another plugin/library.

This is the code I've used before in C++ to eject the CD tray, it's not great, but can sometimes work.

#include <windows.h>

int main(int argc, char *argv[])
{
    mciSendString("set cdaudio door open wait", NULL, 0, 0);
    return 0;
}

is it not the same thing as ioctl??

Yes, you should be able to, but I didn't realise this was for C. However, I've not used that library before, so I can't really give any help/advice on it.

is it not the same thing as ioctl??

Well, ioctl is essentially the means for userland to communicate with the kernel. For example Windows uses ioctl-like commands as follows to open the CD tray:

#include <windows.h>
#include <stdio.h>

int main(void)
{
    DWORD bytes = 0;
    HANDLE HCDrom = CreateFile("\\\\.\\D:",GENERIC_READ,FILE_SHARE_WRITE,0,OPEN_EXISTING,0,0);

    if(HCDrom == INVALID_HANDLE_VALUE)
    {
        printf("Invalid handle\n");
        return -1;
    }
    DeviceIoControl(HCDrom, FSCTL_LOCK_VOLUME, 0, 0, 0, 0, &bytes, 0);
    DeviceIoControl(HCDrom, FSCTL_DISMOUNT_VOLUME, 0, 0, 0, 0, &bytes, 0);
    DeviceIoControl(HCDrom, IOCTL_STORAGE_EJECT_MEDIA, 0, 0, 0, 0, &bytes, 0);
    CloseHandle(HCDrom);
    return 0;
}

Whereas Linux etc. uses ioctl commands as follows:

#include <sys/types.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/cdrom.h>
#include <stdio.h>

int main(void)
{

int cdrom;
    if ((cdrom = open("/dev/scd0",O_RDONLY | O_NONBLOCK)) < 0)
    {
        perror("open");
        exit(1);
    }
    if (ioctl(cdrom,CDROMEJECT,0)<0)
    {
        perror("ioctl");
        exit(1);
    }
return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.