This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: How to call *windows* functions in a cygwin c program


Sylvain Petreolle wrote:
Hi,

working on the mplayer mproject (http://www.mplayerhq.hu),
I need to load DLLS into a C program, read some registry values
and misc.

where could I find some doco on making windows API calls ?

(I found some in the faq at Programming Questions/How do I use Win32
API calls, but need some more info)

I think your best bet may be to look at how we do this in Cygwin/XFree86. Our header file that includes the Windows headers is here:

http://cvsweb.xfree86.org/cvsweb/xc/programs/Xserver/hw/xwin/winms.h


If you want to have the libraries linked at compile time, then you just need to add any Windows libs to the final link line for your executable, such as '-luser32' or '-lgdi32'.

To find the name for the '-l_xxx_' parameter, you just look at the MSDN Library docs for a given function and right at the bottom it says, for example, ``Library: Use Gdi32.lib.'' So, you take a peek in /lib/w32api and make sure that there is a file called libgdi32.a, then you add the portion of the filename after the lib and before the .a to a ``-l'' parameter, such as ``-lgdi32''. That's it. My example function here is BitBlt, which is documented here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_0fzo.asp


The head of the MSDN Library is here:

http://www.msdn.microsoft.com/library/default.asp

Though, it is often easier to find functions in the library via Google (www.google.com), using a search restricted to www.msdn.microsoft.com.


On the other hand, if you want to check for a non-standard library function (such as one that is only available in Windows 2000 or later), then you need to use LoadLibraryEx to try to load the DLL, then you query for a function of the given name using GetProcAddress. If the function is available, you are good to go, otherwise GetProcAddress will return NULL. In which case you call FreeLibrary to close the library and unload it from memory. Of course, you also need to call FreeLibrary when you are done using the function pointer that you obtained (either when your program shuts down, or when it changes into a mode where a given module will no longer be useful for a good amount of time (several minutes)). You can see an example of the loading of a function pointer all the way at the bottom of InitOutput.c:

http://cvsweb.xfree86.org/cvsweb/xc/programs/Xserver/hw/xwin/InitOutput.c?rev=1.29&content-type=text/x-cvsweb-markup


There we load a pointer for _TrackMouseEvent. Do not let the variable name mislead you. There are two mouse tracking functions in the Win32 API. One is called TrackMouseEvent and is only available in some version of Windows after Windows 95 (I don't remember when it was introduced) and there is the _TrackMouseEvent function which is installed with IE 3.0 or 4.0. _TrackMouseEvent simply calls TrackMouseEvent if it is there, or it emulates its functionality if it is not there. Thus, we are calling _TrackMouseEvent in Cygwin/XFree86, but I called the variable for the function pointer g_fpTrackMouseEvent instead of g_fp_TrackMouseEvent, just because the former was less ugly than the latter.

I hope that helps.

Harold


--
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Bug reporting: http://cygwin.com/bugs.html
Documentation: http://cygwin.com/docs.html
FAQ: http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]