Creating a Win32 DLL with the Open Watcom IDE

From Open Watcom

Jump to: navigation, search

This document explains how to create a DLL project in Open Watcom and write a very basic DLL.

Your first DLL

Your very first Open Watcom DLL


Getting Started

1. Open up the Watcom IDE
Image:Ide 1.png

2. Start up a new project
Image:Ide 2.png

3. Name the project whatever you want.

4. Select Win32 DLL and name it zippy.dll.

5. Now add a source file. Call it zippy.c.
Image:Ide 3.png

6. Double click on the source file in the Open Watcom IDE. This will open it up in the Open Watcom editor or whatever editor you configured Open Watcom's IDE to use (in my case [GVIM]).

7. Now we're going to write come code. Actually, you're probably going to copy and paste my code in, but I personally recommend you retype it as a memory aid. Whatever you feel allows you to learn better. Here is it.

 #include <windows.h>
 __declspec(dllexport) WINAPI DrawOval (HWND hwnd,int X1,int Y1,
    int X2, int Y2) {
 
    HDC hdc;
    HPEN oldpen, pen;
     
    if (IsWindow(hwnd)) {
       hdc = GetDC(hwnd);
       pen = CreatePen(PS_SOLID, 1, 0x000000FF);
       oldpen = SelectObject(hdc, pen);
       Ellipse (hdc, X1, Y1, X2, Y2);
       SelectObject (hdc, oldpen);
       DeleteObject (pen);
       ReleaseDC (hwnd, hdc);
    }
 }

8. So what does all that mean?
Well, whatever I don't explain here I'll get to in my future tutorials. Just be patient. Or, if you think you're that smart, go do a little research.

9. __declspec(dllexport)
This tells the compiler to export the function. This means that programs and other DLLs can call the function. Not all the functions in a DLL are automatically useable by external DLLs and EXEs.

10. What does this function do? Basically, this function will draw an Ellipse on the window represented by hwnd. hwnd is a Window Handle. The prefix of h means handle. You and I might also know this as a pointer or reference. It's just a variable that stores the 32 bit address of a window. X1, X2, Y1, and Y2 are the coordinates of the upper left and lower right of the smallest possible rectangle that could surround the ellipse.

11. Save the file and return to Open Watcom. Hit F5 and your program should compile without errors. If it didn't, you probally made a mistake. Figure out what you did wrong and fix it.

12. Move on to a different tutorial. Eventually I would like to suggest you head on over to Calling a Watcom DLL From C being you probably know C if your reading this. However, for now I suggest that if you know Visual Basic you head over to Calling a Watcom DLL From Visual Basic.

Removing Name Mangling

TODO: Elaborate on this section. Its a little unclear without the VB part of this tutorial.

If you were to call a function from this DLL from Visual Basic you would use the following:

Declare Sub foo Lib "zippy.dll" Alias "_Foo@4" (parm as double)

This is because of something called name mangling. Now your probably asking yourself, "Why, can't the name mangling be removed from the exported functions in the DLL?" Well it can be through linker flags. Open up your favorite text editor and enter the following text:

++_DrawOval@20.zippy.DrawOval.2

Save the file as "exports.lbc". Now Select from the menu "Options|Linker Switches", click the right arrow at the bottom once to bring up "Import, Export and Library Switches". Change "Export names" to "=exports.lbc".

The syntax is very simple its

++[mangled name].[dllname].[unmangled name].[ordinal number]

Ordinal number is an optional number that can be used to refer to the DLL function. This is a leftover from the Win16 API and if you omit this one will be automatically included. I include it for completeness.

So now with a new file with a funny extension you have turned:

Private Declare Sub DrawOval Lib "zippy.dll" Alias "_DrawOval@20" _
   (ByVal hwnd As Long, ByVal X1 As Long, ByVal Y1 As Long, _
   ByVal X2 As Long, ByVal Y2 As Long)

into:

Private Declare Sub DrawOval Lib "zippy.dll" _
   (ByVal hwnd As Long, ByVal X1 As Long, ByVal Y1 As Long, _
   ByVal X2 As Long, ByVal Y2 As Long)
Personal tools