Posts

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!