Posts

Showing posts from 2009

Releasing COM Object in C#

I recently encountered an exception in my latest project System.Runtime.InteropServices.InvalidComObjectException and I wonder what going on with this exception. In my project we used a OCX as reference then VS2008 created a sort of interop classes we can use those classes in our code. The problem is when we try to exit the application this nasty exception occurs. I put a destructor in my user control class but still it occurs. I think this happens because the object reference was already detached before the app was able to call the destructor. So, what I did is create a function in my use control class public void ReleaseCOMObject() {             System.Runtime.InteropServices. Marshal .ReleaseComObject( this .m_objCOMobject);  }   and call this function before I call the app exit. See below, private void btnExit_Click( object sender, EventArgs e) {             m_usercontrolObject.ReleaseCOMObject();             Application .Exit();

C++: How to create a DLL and calling it from application?

I create the DLL first, Main.h #ifndef __MAIN_H__ #define __MAIN_H__ #include <windows.h> /* To use this exported function of dll, include this header * in your project. */ #ifdef BUILD_DLL #define DLL_EXPORT __declspec(dllexport) #else #define DLL_EXPORT __declspec(dllimport) #endif   #ifdef __cplusplus extern "C" { #endif void DLL_EXPORT SomeFunction(const LPCSTR sometext); #ifdef __cplusplus } #endif #endif // __MAIN_H__   Main.cpp #include "main.h" // a sample exported function void DLL_EXPORT SomeFunction(const LPCSTR sometext) { MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION); } BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) { switch (fdwReason) { case DLL_PROCESS_ATTACH: // attach to process // return FALSE to fail DLL load break; case DLL_PROCESS_DETACH: // detach from process break; cas

wxWidgets: How to setup in VS2008?

I just downloaded the installer of wxWidgets, wxMSW-2.8.10-Setup.exe, install it then tried to compile the sample projects but there's always files missing. Why? When I tried to rebuild the wxWidgets project guess what? The missing files come to life! Now, I tried to compile again the sample projects it's now okay! Whew! =) In case you might wonder where is the wxWidgets project file it's ..\wxWidgets-2.8.10\build\msw\wx.dsw That's it!

C#.NET in WPF: How to add a control in a Panel in WPF?

I have a dialog with a panel that I want my user controls to be displayed. How can I add my user control in the panel in WPF? users = new AdminUsers (); stackPanel1.Children.Add(users); The AdminUsers is a usercontrol that I created. Then I drag and drop a panel into my form. To add my user control to the panel I use the property Children from panel to add your user control. That's it!

C#.NET : Accessing a UI component using only it’s name as string

I wanted to create a function that can change the state of any UI component that is inherited from class Control. So,how? public void Enable( string ctrlName, bool state) {             Control [] ct = this .Controls.Find(ctrlName, true );             ct[0].Enabled = state; }     This function will change the state of a UI component. What this code does is to find the control in the form with the name as value of ctrName. After finding the control save it to Control[] ct . Since only 1 control exist in this array ct[0] points to the control that we want and accessing it's property as inherited from class Control.   That's it!

C#.NET : Updating a UI component from another thread

I've working on an application that needed to create another thread. My problem is how to update my UI component from another thread? Inside your form class declare this as member, public delegate void InvokeDelegate ( string input);   then create a function that as call-back like below, public void UpdateLog( string input) {             this .tbTestMessage.Text = input;             this .btWaitForDrawerClose.Enabled = m_boolWaitButtonState; }   Okay, now how will you be able to call the function? Inside your thread make the code below, object [] obj = new object [1]; obj[0] = m_strTestMessage; this .tbTestMessage.BeginInvoke( new InvokeDelegate (UpdateLog), obj);   tbTestMessage is a textbox control. So, below is the complete code   class MyForm : Forms { Public delegate void InvokeDelegate(string input); MyForm() { } Public void UpdateLog(string input) { this.tbTestMessage

C#.NET : Creating a worker thread in C#.NET?

Image
How can we create a worker thread in C#.NET? You can use the component BackgroundWorker Drag and drop the component into your form and double click the object a function is create like below, You can put your code inside this function and make sure you won't be accessing a object that is being used by the main thread. To start the thread call, backgroundWorker1.RunWorkerAsync(); and to support cancellation of thread you must set this property, backgroundWorker1.WorkerSupportsCancellation = true; and to cancel the thread call, backgroundWorker1.CancelAsync(); That's it!

C#.NET: How to create a new line in textbox control?

I was doing some programming today in Visual Studio 2008 and came across a problem on how to create a new line in textbox control. So, how? "\r\n" Sample code: tb.Text = "Hello\r\nWorld" Output: Hello World   That's it!

Simple Mapping

Currently there are a lot of instances that we need to map data from the user interfaces or user interfaces to data. premise #1: That greater number is divisible by the lower number. #X = number of X elements #Y = number of Y elements Xr = X ratio against Y Yr = Y ratio against X R = range of single element of greater size M = map   if #X == #Y, mapping is 1:1 if #X > #Y, mapping is Xr:Yr, where Xr > Yr if #X < #Y, mapping is Xr:Yr, where Xr < Yr Constant R is:         X/Y, where X > Y or         Y/X, where X < Y Mapping from X -> Y, where X > Y:         X/R = q,    if q has zero remainder then M is q or                 If q has a remainder then M is q+ 1                 until X=1 Mapping from X -> Y, where X < Y: X*R = p,    M is p - (R-1) Example 1:     If X=20, Y=5     R = 20/5 = 4     M, X->Y : 20/4=5     = q , then 20 -> 5 19/4=4.? = q+1, then 19->5 18/4=4.? = q+1, then 18->5 17/4=4.? = q+1, then 17->5     So, 20,19,18,17 ->5 16/4

MySQL in Windows Vista: Creating a database, table and inserting data using script

The MySQL have the interactive command line to enter your transaction to the database server but what if you wanted to it using script? In Windows Operating system files with *.bat file extension can be executed. So, what I did is create the files and corresponding content: Users.bat   mysql -h localhost -u root -p < procedure1.txt   Explaination: This command will connect to the MySQL server in the localhost with username as root and the password will be asked. The procedure1.txt contains additional commands to be executed in MySQL. Procedure1.txt CREATE DATABASE usersdatabase; USE usersdatabase; CREATE TABLE users (username CHAR(50) DEFAULT '' NOT NULL, password CHAR(50) DEFAULT '' NOT NULL, accesslevel INT(4) UNSIGNED ZEROFILL DEFAULT '0000' NOT NULL, PRIMARY KEY(username)); INSERT INTO users VALUES ('professor','prof$532*&^%!@',20), ('student','

Visual Studio 2008 C#.NET: Displaying a User Control in Content Control

I was searching in the internet on how to display my user control when I click an item in my tree control. I was dismayed when I saw nothing that will address my problem so I opened my MSDN and do it myself. In your Visual Studio 2008 IDE toolbox you have the content control. In this control you have the Content property. So, if you want your user control to be displayed inside the content control you'll just need to do this: contentControl1.Content = new MyUserControl(); That's it!

How To Setup FLEX in MS Visual 2008

Image
I came to know about LEX and YACC when I was working in NEC Telecom Software Philippines that wasI think 2001 when Carlo my former colleague introduced me into it. He used it to parse the binary output (in file) to retrieve the groups of data to be translated into a human readable format. He used the LEX to create the parser. Since then I always wanted to know how he did that so that’s how I ended up with this e-book. First you need to download “flex-2.5.4a-1.exe” from the internet and install it. Make sure the environment path is set below after installation. Try to test FLEX by typing flex in command window like below sample, Now, try to create a simple LEX file and save it as “test.l” In the command type ” flex test.” l and this will create a “lex.yy.c “ file. Open your MS Visual Studio and create a empty C++ project. Add the”lex.yy.c” file in the source folder. Then in the project Tools\Options menu add the include file In the “Project\sample Properties” select “Linker\Input”