分类: C/C++
2006-06-23 13:42:44
==============================================================================
Port from DOS/UNIX C code to WIN32 Easily
==============================================================================
Port from DOS/UNIX C code to WIN32 Easily
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.
Visual Basic is (imho) the best RAD tool ever made by
the Microsoft Corporation. Visual Basic allows a developer make a
prototype and take it to production status in a matter of hours.
If you do not have any experience in WIN32 development, but have
acquired quite a lot C/C++ expertise, then by all means give
Visual Basic a try.
The tools I used for this article were Borland C++ 4.52 and
Microsoft Visual Basic 5.0, both very fine tools of the trade.
The DLL will, of course have two functions that correspond to the
VB calls. It will read in in_string, increment each char in the string,
and the new string will be returned as out_string (if successful).
#pragma argsused
BOOL WINAPI DllEntryPoint(HINSTANCE hInst,DWORD dwFunction,LPVOID lpNot)
{
if(dwFunction != DLL_PROCESS_ATTACH) return TRUE;
hInstance = hInst;
lstrcpy(szAppName, "convert");
return TRUE;
}
#pragma argsused
int WINAPI WEP (int bSystemExit){ return 1; }
int WINAPI EXPORT InitconvertDLL(HINSTANCE hAppInstance){ return 1;
}
int WINAPI EXPORT TermconvertDLL(HINSTANCE hAppInstance){ return 1;
}
int WINAPI EXPORT CvtA2U( LPSTR in_str, LPSTR out_str )
{
int i = 0;
int result = 0;
i = strlen( in_str ); if( i < 1 ) return result;
for( i = 0; i < (int)strlen( in_str ); i++ ) out_str[i]
= in_str[i] + 1;
out_str[i] = 0; result = 1;
return result;
}
int WINAPI EXPORT CvtU2A( LPSTR in_str, LPSTR out_str )
{
int i = 0;
int result = 0;
i = strlen( in_str ); if( i < 1 ) return result;
for( i = 0; i < (int)strlen( in_str ); i++ ) out_str[i]
= in_str[i] - 1;
out_str[i] = 0; result = 1;
return result;
}
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:
my functions are:
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.
Convert.bas looks like:
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:
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.
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: