Monday, June 15, 2009

Using .NET component in C++ code

Referring a C++ COM component is easy in .NET; all we have to do is to
  • register (regsvr32 <dll_name_with_full_path>)
  • Add a reference to the COM dll in the Visual Studio Solution
  • An Interop file will be created in the bin directory of .NET project and we are done 
But the reverse is not as simple as above.
So this time I have come up with Steps we need to follow in order to refer a COM visible .NET dll in C++ code. 
 
  • Register the COM Visible .NET assambly with regasm like this:
regasm <dll_name_with_full_path> /tlb
e.g. regasm "C:\SampleProject\bin\Release\Atreya.Anugrah.ComVisibleComponent.dll" /tlb
  • Refer .NET component in .h file like this:
#import "<generated_tlb_name_with_full_path>" named_guids raw_interfaces_only
e.g. #import "C:\SampleProject\bin\Release\Atreya.Anugrah.ComVisibleComponent.tlb" named_guids raw_interfaces_only 
  • Create a class in .h file, and declare member variable like this:
ComVisibleComponent::<Name_of_Interface>* memberVariablePointer;
e.g. Atreya_Anugrah_ComVisibleComponent::IMyInterface* m_pIMyInterface; 
  • Now, Initialize the declared member variable pointer in .cpp file like this: 
hr = CoCreateInstance( __uuidof(Atreya_Anugrah_ComVisibleComponent::<Name_of_CoClass_Implementing_Interface>),          // COM class id
                                        NULL,                                                                                                                                                  // Outer unknown
                                        CLSCTX_ALL,                                                                                                                                       // Server INFO
                                        Atreya_Anugrah_ComVisibleComponent::IID_<Name_of_Interface>,                                                // interface id
                                        (void**) &m_pIMyInterface   );                                                                                                                 // Returned Interface 
if (FAILED(hr))
{
       _com_error e(hr);
   strErrorMsg.Format("Error: Unable to create COM Visible .NET component. hr = %d. Message = %s",hr, e.ErrorMessage());
   return hr;
}
          //you are good to use your initialized pointer here :)   
     m_pIMyInterface->foo()
 
P.S. This post does not explain that how can we make our .NET component as COM visible. Please refer other post/article for that as it is quite simple activity.
    
Namaste (Thanks and Regards)
| Anugrah Atreya
 

Monday, April 27, 2009

Steps to debug .NET dll (registered in GAC) consumed by unmanaged vb6 code

To debug Shared dll in GAC called by VB6 code:

 

0.) Unregister shfusion.dll in c:\windows\microsoft.net\framework\v<version>\shfusion.dll so that actual GAC folders are visible in Explorer.

1.) Open .NET project and make sure that "Just my code debugging is unchecked" in Tools > Options > Debugging.

2.) Now rebuild the .NET dll in debug mode.

3.) Add dll built in debug mode along with the PDB to GAC

4.) Run the vb6 component.

5.) Attach the .NET project to running vb6.exe process.

6.) Breakpoints will be loaded dynamically at runtime.

 

namaste!

Anugrah Atreya

 

Friday, March 6, 2009

Implemented IObjectSafety in .NET but object still unsafe for scripting : RESOLVED

Hello Friends,

 

The intended audience of this article are not those developers who are looking how to implement IObjectSafety in .NET but those who have already done that and still getting “Object unsafe for scripting” popup in Internet Explorer.

(they can find the implementation on msdn blog here: http://blogs.msdn.com/infopath/archive/2005/04/15/408728.aspx).

 

I saw a lot of confusion related to implementation of IObjectSafety in .NET and thought to share my experience about the same.

 

So the simple cause to this big problem is our knowledge about COM & Guids J

 

Basically when we get the interface from any knowledgebase we include it to out project and like a good programer we generate a new Guid and replace the one which is given in orignal documenttaion.

 

That’s it; we have messed our code and we are not even aware of it.

 

So the crux is that for any IObjectSafety implementation the guid of out interface should always be

{"CB5BDC81-93C1-11cf-8F20-00805F2CD064"}. Period.

 

By the time you end up reading the above line your problem is hopefully solved and your component is now safe for scripting.

 

So this is how your interface will eventually look like:

 

using System;

using System.Runtime.InteropServices;

 

namespace Atreya.SampleCode

{

    /// <summary>

    /// Methods in this interface needs to be implemented to mark any object safe for scripting in IE

    /// </summary>

    [ComImport()]

    [Guid("CB5BDC81-93C1-11cf-8F20-00805F2CD064")]

    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]

    internal interface IObjectSafety

    {

        [PreserveSig]

        int GetInterfaceSafetyOptions(ref Guid riid, out int pdwSupportedOptions, out int pdwEnabledOptions);

 

        [PreserveSig]

        int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions);

    }

}

 

Namaste!

  Anugrah Atreya 

 

Friday, January 2, 2009

Transparent Button on Windows Forms

Hi,

  

I was working on some VB6 migration to C#.NET, in VB6 application developers have used some third party controls (e.g. SSRibbon provided by Sheridan 3_0) to create transparent Buttons (required when button is drawn on the background image of form and you want user to click over that part of background image).

 

When I tried to search the internet for a solution in .NET, I didn’t found anything usefull. May be because this is quite simple thing :)

 

But still we need to have the solution on internet, so that someone in need can find it.

So, all you need to do is to add these lines of code for your System.Windows.Forms.Button object. In my case I have named it as btnLink.

 

          btnLink.FlatStyle = FlatStyle.Flat; 

          btnLink.BackColor = Color.Transparent;

          btnLink.FlatAppearance.MouseDownBackColor = Color.Transparent;

          btnLink.FlatAppearance.MouseOverBackColor = Color.Transparent;

 

namste !

  Anugrah Atreya 

Sunday, August 31, 2008

Order of execution for class Constructor and Initializers

Hi All,

The origin of this post is the query posted at MSDN blog

This was a featured article at Start Page of VS2008 as well.

Well in short this is about that why Constructors of Base class are invoked first where as Initialization of class member variables is performed in Reverse Order i.e. from Derived to Base.

Until now we were suggested to save paper but now i believe in saving server hard disk as well :) So in case if you are interested to know the answer which i provided then please see search "Atreya A" at the original MSDN blog

namaste!

Wednesday, August 27, 2008

UML - Types of Relationships

1. Association

a. Aggregation: ‘has-a’ relationship: Employee has an address.
b. Composition: ‘part-of’ relationship: Engine is a part of Car

2. Inheritance: ‘type-of’ relationship : Car is a type of Vehicle
3. Realization: Implementation of some Interface

NOTE: Aggregation and Composition are sometimes confusing E.g. in examples above I have used car and engine in Composition Relationship, whereas Car and engine can also share an Aggregation as shown below:

“Swift Car has a Fiat Engine”

But the v.v.imp point here is that when we say that “Swift Car has a Fiat Engine”, the lifetime of object Fiat engine is not in hands of particular instance of ‘Swift Car’ because there might be some other car for which we can say that “Suzuki Sx4 has a Fiat Engine as well”
So the crux is that engine is a generic term here as there could be more than 1 owner/consumer
Whereas at the same time in a Composition Relationship there can’t be more than 1 owner at the same time i.e. 1 particular engine could be part of 1 car only. So if we destroy car object we can easily destroy the said instance of Engine object.

Thursday, February 14, 2008

C# keywords: readonly vs const

The readonly keyword is different from the const keyword. A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used.

In a nut shell we can say that, while a const field is a compile-time constant, the readonly field can be used for runtime constants as in the following example:

public static readonly uint l1 = (uint) DateTime.Now.Ticks;

Refrences: MSDN July 2004

namaste !