VisualCpp.org Collection of Visual C++ Stuffs

24Aug/07Off

How to Find USB Drives insertion and removal?

USB drives are very common method of data transmission between computers. So if you get to know when it inserted and when removed isn’t it beneficial for you. For this Windows have provided you a fantastic API RegisterDeviceNotification, successful call this API will provide you with WM_DEVICECHANGE message which has information for all hardware change.

I wish to write a nice code for same, but found that MSDN itself provided you with one here. So why reinvent the wheel

Filed under: General, System, Win32 Comments Off
8Aug/07Off

How to Check Baterry status of LAPTOP programatically?

One of my friend, is developing small utility, at some point time it's require check the status of laptop battery if running on laptop to do some cleanup work! But he is unable to do so. We tried to find some help of same in MSDN and we found GetSystemPowerStatus which will do above task.

SYSTEM_POWER_STATUS lpPwrStatus;

// Call Api!
GetSystemPowerStatus(&lpPwrStatus);

//Check weather running on AC or Battery

if((lpPwrStatus.BatteryFlag == 128)|| (lpPwrStatus.BatteryFlag == 255)) // I.e. on Battery
MessageBox(L"No System Battery",L"GetSystemPowerState");
else
{
 CString strState;
// check state of system battery
  switch(lpPwrStatus.BatteryFlag )
  {
   case 1:
      strState.Format(L" Battery state is High, with %d battery left",lpPwrStatus.BatteryLifePercent);
      break;

  case 2:
     strState.Format(L" Battery state is Low, with %d battery left",lpPwrStatus.BatteryLifePercent);
   break;

  case 4:
     strState.Format(L" Battery state is Critical, with %d battery left",lpPwrStatus.BatteryLifePercent);
   break;

  case 8:
     strState.Format(L" Battery state is Charging, with %d battery left",lpPwrStatus.BatteryLifePercent);
    break;

  default:
     strState.Format(L" Battery state is unknown");
   }
MessageBox(strState,L"GetSystemPowerState");
}

Filed under: General, Win32 Comments Off
6Aug/07Off

How to call a COM C# component from VC++?

Just like a normal COM DLL or EXE, a .NET module which has been wrapped as a COM module needs to have its information written into the registry in order for the COM sub-system to locate it and load it into memory for a client.

REGASM.EXE
  This is achieved via the REGASM.EXE utility. REGASM performs similarly to the well-known REGSVR32.EXE utility which is used to register COM modules. REGASM uses the metadata contained inside a .NET assembly to generate COM-equivalent information which are then used to insert entries into the registry.

The entries written into the registry include the CLSIDs and ProgIDs of .NET classes which are exposed as COM classes. This registration process is important for COM clients in the discovery and loading process.

Call the REGASM.EXE utility inside the Visual Studio .NET command prompt as folows :

regasm <.NET DLL Assembly Name> /tlb

The "/tlb" flag commands REGASM.EXE to produce a Type Library File (.TLB) for the .NET Assembly. It is useful for your VC++ client to import.

In your VC++ code, you import the .TLB file using the #import keyword, e.g. :

#import "(TLB file path)";

GACUTIL.EXE
  Next, for ease of discovery and loading by the .NET CLR engine, your .NET DLL Assembly -should- be registered into the GAC (Global Assembly Cache). This is not 100% required but will make life must easiler for your client apps.

In order that a .NET module be registered to the GAC, it needs to be digitally signed. This requires a Strong Name Key (SNK) file. You can create an SNK file using the "sn.exe" .NET utility.

Another step to take is to set the path of the SNK file in the AssemblyKeyFile attribute in the AssemblyInfo.cs file :

[assembly: AssemblyKeyFile("..\\..\\KeyFile.snk")]

To register an assembly into the GAC, we use the GACUTIL.EXE utility. For example :

gacutil -i <.NET DLL Assembly Name>

An alternative to using GACUTIL.EXE is to copy your DLL Assembly into a folder location that can be discovered by the .NET CLR.

----------------------------------------------------

Tip taken from and courtesy Mr. Lim Bio Liong

Filed under: COM/ATL, General Comments Off
1Aug/07Off

Difference between Dynamic Link Library (DLL) and TypeLibrary(TLB)?

Type Library

 A type library (.tlb) is a binary file that stores information about a COM or DCOM object's properties and methods in a form that is accessible to other applications at runtime. Using a type library, an application or browser can determine which interfaces an object supports, and invoke an object's interface methods. This can occur even if the object and client applications were written in different programming languages.

The COM/DCOM run-time environment can also use a type library to provide automatic cross-apartment, cross-process, and cross-machine marshaling for interfaces described in type libraries.

And that's of DLL

A dynamic-link library (DLL) is a module that contain functions and data that can be used by another module (application or DLL). In other words, TLB contains description of COM object. DLL
is just container of functions and resources. It's convenient to store COM object (or several objects) in DLL while having corresponding TLB's embedded in DLL module as binary resources.


------------------------------------------------------------------

Taken from MSDN forum,Tip owner PJ. van de Sande (MSMVP)

Filed under: COM/ATL, DLL, General Comments Off