?
Solved

How to Add a Windows Form to a C++ Win32 DLL

Posted on 2009-04-28
3
Medium Priority
?
1,435 Views
Last Modified: 2013-12-14
Hi

I have a C++ Win32 DLL within which I have a function that has a looping calculation which can take some time. This function is then exported and then used within VBA Excel. I would like to add a Windows Form to the DLL which contains a progress bar so that as the code loops the progress bar increments thereby letting the user know how long is left.

However, I've been trawling the net and can't seem to find a solution. Admittedly, I'm fairly new to C++ so perhaps I'm missing something obvious
The basic form of the function would be
 
void samplecode()
{
// Add code here to intialise form with progress bar
 
// Set progressbar max to a value
 
// Set current progressbas value to 0
 
// Do looping calc
	for (int i = 0; i <= 255;i++)
                     {
                      // Add code here to increment progress bar
 
                     // Do some calculation
                      }
// Close Form 
// Exit Function
}

Open in new window

0
Comment
Question by:JACD1
2 Comments
 
LVL 49

Accepted Solution

by:
DanRollins earned 1000 total points
ID: 24276470
The solution is as simple as adding a dialog resource to the DLL and creating one control in the dialog box and then sneding messages to it to update itself -- then close the dialog box when finished with the calculation.
Do you have MFC support in your DLL?  (having it makes the dialogbox creation and manipulation very easy).
 
0
 
LVL 13

Assisted Solution

by:kaylanreilor
kaylanreilor earned 1000 total points
ID: 24654832
I think that currently your DLL is only existing to provide some calculation to another application. Then it is not a good idea at all to provide a GUI into this DLL. You should instead raise the even and catch it from the client application which will provide the GUI. So here your client application is Excel.
Nevertheless, if you absolutely want to do it into the DLL, you should better forget Winforms here and learn how to do it with MFC or ATL/WTL. You will find a lot of complete example on the web, for example here are some articles on codeproject:
http://www.codeproject.com/KB/miscctrl/cskinprogress.aspx
http://www.codeproject.com/KB/miscctrl/progressctrlx.aspx
http://www.codeproject.com/KB/miscctrl/text_progressctrl.aspx

Now, if you really, really do want to add a winform to your win32 DLL, first of all you will have to add a specific cpp file in your project only to have into THIS file the support of the CLR. Into this file you will write some C++/CLI code. You can do that by right-click on this source file in solution explorer and open the property page and then in C/C++=>General=>Compile with Common Language Runtime support set the value "Common Language Runtime Support (/clr)". Thus you will be able to code your winform the same way that VS generally generate them as the following. After, you wuill add references on System and System.Windows.Forms. All this is the most complicated to add a WinForm but on the other hand it is the more secure. You are going to mix in the same project unmanaged and managed C++. If you do a mistake like defining a global variable of an unmanaged class that needs a constructor (like an STL container for example) in the managed code part you will have some unpredictible crash at run time.

BUT, you can do it more easily. Currently if you right-click on your project in solution view and then click Add=>New Item and then choose a winforms form, Visual Studio will warn that it has to convert the project sio that the support on the CLR will be added on the whole project and your WinForm will be generated. In that case you will have to deal with the directives "#pragma managed" and "#pragma unmanaged" to clearly tell to the compiler which part is managed and which part is not. But you will have to discover it later on, simply by using your new mixed DLL !
public ref class MyWinForm : public System::Windows::Forms::Form
{
public:
	MyWinForm(void)
	{
		InitializeComponent();
	}
 
protected:
	~MyWinForm()
	{
		if (components)
		{
			delete components;
		}
	}
 
private:
	System::ComponentModel::Container ^components;
 
	void InitializeComponent(void)
	{
		this->components = gcnew System::ComponentModel::Container();
		this->Size = System::Drawing::Size(300,300);
		this->Text = L"MyWinForm";
		this->Padding = System::Windows::Forms::Padding(0);
		this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    }
};

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Suggested Courses

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question