-1

I've done a C program that takes the RGB values (0-255) of a pixel of the screen knowing its position (x,y). It works in Linux, but when I try to compile it in Visual Studio (Windows), crashes because libraries X11/Xlib.h, X11/Xutil.h, unistd.h doesn't exist.

This is the code:

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>

void getimage(XColor c, Display* d, int* rgb, int x, int y) {
    XImage* image;
    image = XGetImage(d, RootWindow(d, DefaultScreen(d)),
        x, y, 1, 1, AllPlanes, XYPixmap);
    c.pixel = XGetPixel(image, 0, 0);
    XFree(image);
    XQueryColor(d, DefaultColormap(d, DefaultScreen(d)), &c);
    rgb[0] = c.red / 256;
    rgb[1] = c.green / 256;
    rgb[2] = c.blue / 256;
}

int main () {
    int rgb[3], x, y;
    XColor c;
    Display* d = XOpenDisplay((char*)NULL);
    getimage(c, d, rgb, x, y);
}

How can I implement this to run in Windows?

Sergio
  • 844
  • 2
  • 9
  • 26
  • 1
    Those are headers, not libraries. Headers are textual source files telling the compiler what's available; they're useless unless you have an implementation of the libraries themselves. These particular headers and libraries are largely specific to Unix-like systems. There are Unix emulation layers for Windows, such as Cygwin. Or you can write code with equivalent functionality that uses the headers and libraries provided by Windows; the result is going to be *very* different. There are also cross-platform libraries that abstract the differences; wxWidgets is one example. – Keith Thompson Mar 03 '17 at 20:38
  • This is a compiler error, not a crash. – IInspectable Mar 03 '17 at 20:40
  • If your compiler **crashes** for missing headers, you should run it in a debugger and file a bug report with all details. – too honest for this site Mar 03 '17 at 22:22
  • Thanks for your answer! At the end, I installed cygwin. The headers work now, but I've found another problem: Display *d = XOpenDisplay((char *)NULL); is not working because the parameter of the function is not correct. In Linux, (char *)NULL is the enviroment variable of the default display, but not in windows. Do you know what to write here? – Sergio Mar 04 '17 at 13:42

2 Answers2

3

You've written an X11 program. X is the consensus standard for graphical displays on UNIX-like systems other than OS X (but it is also freely available for OS X).

Windows uses a completely different graphical display API. It is conceivable that you could find an X implementation that runs on Windows, but Microsoft certainly doesn't provide one. Since your program is quite short, your best bet is probably to rewrite it using the Windows API.

As for unistd.h (and time.h), I don't see anything in your program that depends on it. If you weren't going to rewrite the program, you could resolve that part of your problem simply by removing the offending #include statement.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
  • Thanks for your answer! At the end, I installed cygwin. The headers work now, but I've found another problem: Display *d = XOpenDisplay((char *)NULL); is not working because the parameter of the function is not correct. In Linux, (char *)NULL is the enviroment variable of the default display, but not in windows. Do you know what to write here? – Sergio Mar 04 '17 at 13:40
  • 1
    @Sergio, if there is no X server running on the machine -- and unless you've installed one, you can be sure there isn't -- there is no display available to connect to. Although you have succeeded in building your program, it cannot run in your present environment. If there were an X server running then the display name string would have form something like `":0"`, but I repeat: your best bet it to rewrite the program against the Windows API. – John Bollinger Mar 06 '17 at 13:00
  • Yes, I will do that. Thanks! – Sergio Mar 06 '17 at 13:25
1

One small part to the answer for your question may be to search around MSDN. The following function is described there: (link below)

#include <Windows.h>  
...
DWORD WINAPI GetSysColor(
  _In_ int nIndex
);

To use that function, you must link to User32.lib which is implemented in User32.dll

The descriptions of GetSysColor(...) et. al. are accessible here. This function is one of many that support similar functionality, but warning, the usage methods will be like a completely different language. Windows programming is rarely similar to Linux programming.

Other starting points to Windows related graphical/image programming:
capturing an image
Create your own Snipping tool

ryyker
  • 22,849
  • 3
  • 43
  • 87
  • Thanks for your answer! At the end, I installed cygwin. The headers work now, but I've found another problem: Display *d = XOpenDisplay((char *)NULL); is not working because the parameter of the function is not correct. In Linux, (char *)NULL is the enviroment variable of the default display, but not in windows. Do you know what to write here? – Sergio Mar 04 '17 at 13:42