Monday, July 11, 2011

Visual C++

Instructions

Now, let's go to work. Please follow the following steps: (Please skip the steps 1 to 8 if you don't have Visual C++ installed on your computer)

Click Start, if you have Windows 9x, click Programs, and if you have Windows XP, click All Programs, and then Microsoft Visual Studio 6.0 -- Or Microsoft Visual C++ 6.0 --, and then Microsoft Visual C++ 6.0.

In Microsoft Visual C++ 6.0, click File, and then New.... (Or press Ctrl+N)

In New dialog, click Projects tab, select MFC AppWizad (dll) in the Projects list. Set the Project name to MessageDLL, make sure Create New Workspace is selected, and then click OK button.

In the MFC AppWizard - Step 1 of 1 dialog, select Regular DLL using shared MFC DLL, select Yes, please in the bottom, and then click Finish button. Click OK button in the next dialog box.

In the Workspace window, expand the MessageDLL classes item, expand the Globals item, and then double click theApp item.

Please add the highlighted code by gray to theApp window:
// MessageDLL.cpp : Defines the initialization routines for the DLL.
//

#include "stdafx.h"
#include "MessageDLL.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

//
// Note!
//
// If this DLL is dynamically linked against the MFC
// DLLs, any functions exported from this DLL which
// call into MFC must have the AFX_MANAGE_STATE macro
// added at the very beginning of the function.
//
// For example:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // normal function body here
// }
//
// It is very important that this macro appear in each
// function, prior to any calls into MFC. This means that
// it must appear as the first statement within the
// function, even before any object variable declarations
// as their constructors may generate calls into the MFC
// DLL.
//
// Please see MFC Technical Notes 33 and 58 for additional
// details.
//

/////////////////////////////////////////////////////////////////////////////
// CMessageDLLApp

BEGIN_MESSAGE_MAP(CMessageDLLApp, CWinApp)
//{{AFX_MSG_MAP(CMyWebTestApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMessageDLLApp construction

CMessageDLLApp::CMessageDLLApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// Message function

int _stdcall Message(HWND hHWND)
{
MessageBox(hHWND, "Hello World", "Message Box Title -- Hello World", MB_OK);
return 0;
}


/////////////////////////////////////////////////////////////////////////////
// The one and only CMessageDLLApp object

CMessageDLLApp theApp;



This is an explanation of Message function. First of all, we write an int word, this word mean that the function is an integer (like VB Function Func1() As Integer). The _stdcall is used to allow the users to use the Message function. The Message is the function name. The Message(HWND hHWND) is the function parameters, the HWND hHWND parameter is used to determine the form handler.

The MessageBox is the name of a function in Visual C++, this function is used to show a message box. The MessageBox syntax is int MessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType). As I explained, the hHwnd is used to determine the form handler. the lpText is used for the Message Box prompt/message. lpCaption is used for the Message Box title. And finally, you can use the uType to add pictures and to show/hide buttons from the Message Box.

The last thing is the return 0, the function will return what is typed after return statement.


Now, to let the developers use the functions in your DLL. You must add the function name to a file called MessageDLL.def. In the bottom of the Workspace window, click FileView tab, in the Workspace list, expand MessageDLL files item, expand Source Files item, and then double click on MessageDLL.def. Modify the file by replacing all the text with the following text:

; MessageDLL.def : Declares the module parameters for the DLL.

LIBRARY "MessageDLL"
DESCRIPTION 'MessageDLL Windows Dynamic Link Library'

EXPORTS
; Explicit exports can go here
Message





Under the Exports section, you must add all the functions you want to allow the developers to use.



Now we're done with the DLL. Go to Build menu, and click Compile MessageDLL menu item. If an error occur, please check the code, or download the tutorial. Now, go to File, Open.... In the Open dialog box, open your MessageDLL Project folder, go to Debug, make sure that you're showing all the file types, right-click on MessageDLL.dll file and click Copy. Now open your system folder, located at /windows/system/ in Windows 9x, and located in /windows/system32/ in Windows XP. Paste the DLL file to the system folder.

Now, let's go to Visual Basic. Start Visual Basic, create a New Project. In Form1, right-click on the form and click View Code/or double-click the form.

Replace all the code with the following code:

Option Explicit

Private Declare Function Message Lib "MessageDLL.dll" (ByVal hWnd As Long) As Long

Private Sub Form_Load()
Dim i As Integer

i = Message(Me.hWnd)
End Sub



Now, we are done. Visual Basic will locate the MessageDLL.dll without specifying the DLL file path, because Visual Basic always looks for the DLLs in the system folder, application folder, and many other folders.

Now, run the application by clicking the Start button, or by clicking F5 on your keyboard. When the program starts, a message box will appear with a "Hello World" message.

No comments:

Post a Comment

 
THANK YOU FOR VISITING