Posts

Initialing a boolean variable thru comparison operator

There are time when we try to compare and the point is only to initialize a boolean variable. Sample, if(var1 == var2) {   bVar = true; } else {   bVal = false; } So, instead of the long code above you can shorten it to: bVar = (var1 == var2); That's it!

Calling unmanaged DLL in C#.NET

I have a project that created a DLL but somehow I wanted to know if the current DLL I have is the same with the project. So, I created a form project in C# and calling the function. Sample below: using System.Windows.Forms; using System.Runtime.InteropServices; // DllImport namespace DLLTester {   public partial class Form1 : Form {  public Form1( ) {    InitializeComponent(); }   private void button1_Click( object sender, EventArgs e )  {      MessageBox.Show(WrapperClassOfDLL .EnableDevice().ToString());  } } public class WrapperClassOfDLL { [DllImport("MyUnmanagedDLL.dll")] public static extern UInt32 GetVersion( ); } } Put the DLL in the project directory and make sure the function signature is the same in the DLL. That's it!

Digital Logic

I created a project named Digital Logic. Digital Logic can create any logic gates you want. You can use the Base namespace to create your own customized gates or inherit the ones I already implemented in Base.Gates namespace. For now, only a few gates were implemented but I will add more in the coming days. First, you'll need to have the DigitalLogic.cab. Extract the DLLs and save it in your working folder. Add the DLLs into your project references. Sample code: int main() {    AND andGate = new AND();   andGate.Input(1, true);   andGate.Input(2, false);   if(andGate.Output == false) MessageBox("Correct");   else MessageBox("Wrong");   return ; } You can download the files here Digital Logic Enjoy!

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!