==============================================================================
C-Scene Issue #04
Port from DOS/UNIX C code to WIN32 Easily
Kerry D. Mathews II
==============================================================================


Port from DOS/UNIX C code to WIN32 Easily

In this article I will explain the quickest and easiest way
to take some of your some good C code and make a WIN32 application from it.

How?
Easily, take your code or algorthms and put in into a 32bit DLL,
and use Visual Basic as your GUI.

Easy?
Of course. No Einsteins behind this keyboard.

A couple forward points I'd like to explain:


OK. Here's the project.

Easy enough... let's make this interesting.

Let's start with the DLL.

As a matter of fact... Here's all the code.



/* ********** Oh My Gosh!  He's Pasting Code in the Channel !! ********* */
  /* ******* Oh My Gosh!  He's Done Pasting Code in the Channel !! ******* */



 

Zounds! Not a lot of code!

And basically, no. A DLL doesnt need much more than that.
There are some basic mandatory DLL functions needed, namely:

and You must, of course, add your functions as well.

my functions are:

Do note, that even though they're declared as, return val int,
Visual Basic wants them declared as long. If i am wrong, /msg kuru.
LPSTR is win32 speak for a modifiable string.

There is a file called convert.def, it contains all of the functions
we want declared as exportable... that is callable from a win32 app.
The file looks like:
LIBRARY             convert
DESCRIPTION        '32bit DLL'
EXPORTS             CvtA2U
                    CvtU2A
.. and thats it.

Making a win32 DLL is not hard at all. No Sir.

The location of the DLL is rather important. It should be located in the
%PATH% directories or in the directory local to the calling executable.

The point I would like to stress, is that, in this example I am using a
rather ineffective algorthm .. OK, call it lame. BUT.. You can easily
slip in your own code to do.. who knows what... FF4 conversions maybe.
Is it getting warm in here? Must be the light I'm letting in.
 

Now, On to the Visual Basic application.



Before you can call a DLL or any external library you must prototype it
in a separate file. I called my header file (oops, C talk again) convert.bas .

Convert.bas looks like:

And that looks very much like our declarations in C... almost like cousins.
In fact.. I'll call it self explanatory.

The buttons we made in our VB app have code tied to the action (method)
of clicking on that particular button. Naturally, that is where we would
place the actual call to our DLL.

Here's an example:
 

As much as i hate to gloss over some aspects. You can clearly see
the call to our DLL. We've passed it two string parameters, and took the
returning value in our variable 'Results'.
 

Well, I have to admit, it took me longer to write this article than code
the examples. So now it's time to hide under my desk and take a nap.

I've included all the source code and makefiles to re-create the examples.
I hope this helps you to bridge your code to the world of WIN32. I code for
a living too, so.. a little tidbit like this is can be a career saver.



 

Parting Notes:

 When developing your own VB apps that call your DLL, you may need to add
 the line: chdir "c:\some_dir_that_holds_your_DLL" during the
 VB development cycle. VB has a unelegant way of telling you it couldn't find
 the DLL. Oh, and when you DLL is faulty... bad things can happen.

 Visual Basic Programmer's Guide to the Win32 API  ISBN:  1-56276-287-7
 Although for VB 4.0, is a very good book. Hats off to Daniel Appleman.

Items needed to make the 32bit DLL:

Items needed to make the Visual Basic 5.0 Application: All source code should be found in convert.zip.

This page is Copyright © 1997 By C Scene. All Rights Reserved