Sunday, February 27, 2011

Several ways of swapping two variables with using temp variable and without using, in several line or in one line

 SWAPING........

#include <iostream>
using namespace std;
int main()
{
int a=10,b=20;
cout <<a<<"   "<<b<<endl;
a+=b-=a=b-a;
cout <<a<<"   "<<b<<endl;
a/=b=(a=a*b)/b;
cout <<a<<"   "<<b<<endl;
a^=b^=a^=b;
cout <<a<<"   "<<b<<endl;
a=(b=(a=b^a)^b)^a;
cout <<a<<"   "<<b<<endl;
b=a+b-(a=b);
cout <<a<<"   "<<b<<endl;
a=a^b;
b=a^b;
a=a^b;
cout <<a<<"   "<<b<<endl;
a=a+b;
b=a-b;
a=a-b;
cout <<a<<"   "<<b<<endl;
int temp=a;
a=b;
b=temp;
cout <<a<<"   "<<b<<endl;

return 0;
}

Setting a value to a memory location.....

memset(str or structure, with what number, show many..)
eg:
memset(text,0,100);


Comparing float with fixed int where the fraction part is not fixed

(int)<floatval>==999;

eg:
(int)99.777==99







How to print a number or integer is a message box?

TCHAR str[200];
int number=100;
sprintf(str,"%d",number);
Messagebox(0,str,0,MB_OK);

How to get size of BSTR string?

use Sysstringlen or sysstringbytelen for calculating the size of the BSTR string.

Memcpy is not copying data properly if it is pointer...

we can write it like following:
memcpy(&array, sizeof(array));












How we can write full structure to a file???

We can write a full structure to a file only if the structure has fixed fields not if the structure has any pointer variable because the size is not fixed which is necessary while writing to a file. so either have a fixed size structure or we need to write the structure as a separate elements one by one bye calculating the size of the field at runtime.

Error : cannot convert parameter 1 from WCHAR[20] to const char*

Solution:
this error comes when we use strlen so istead of uisng strlen we shoulc use wcslen which suitable for wide character operations.

What is DLL Hell

When a dll is overwritten by a newly installed application which was used by some other application so this lead to system break.
Previously, before .NET, this used to be a major issue. "DLL Hell" refers to the set of problems caused when multiple applications attempt to share a common component like a dynamic link library (DLL) or a Component Object Model (COM) class. In the most typical case, one application will install a new version of the shared component that is not backward compatible with the version already on the machine. Although the application that has just been installed works well, existing applications that depended on a previous version of the shared component might no longer work. In some cases, the cause of the problem is even more subtle. In many cases there is a significant delay before a user discovers that an application has stopped working. As a result, it is often difficult to remember when a change was made to the machine that could have affected the application. A user may remember installing something a week ago, but there is no obvious correlation between that installation and the behavior they are now seeing. The reason for these issues is that version information about the different components of an application aren't recorded or enforced by the system. Also, changes made to the system on behalf of one application will typically affect all applications on the machine.
One reason why it was hard to build an isolated application was the run-time environment typically allowed the installation of only a single version of a component or an application. This restriction means that component authors must write their code in a way that remains backward compatible, otherwise they risk breaking existing applications when they install a new component. In practice, writing code that is forever backward compatible is extremely difficult, if not impossible. Also components were shared because disk space and memory was expensive. In the past few years, hard disk and memory prices have dropped dramatically, and disk space is no longer a premium. But as applications have increased in size and in modularity not so long ago many applications were entirely self-contained in a single .exe file - the DLL sharing issue has not been addressed, and the problem has grown over time. 

How it is solved ????
How .NET addresses DLL Hell?
Microsoft .Net 1.1, which is integral to the Windows Server 2003 operating systems, supports strong binding. Strong binding means an application or component can bind to a specific version of another component, so you can reuse components or use them in isolation.
.Net 1.1 will provide Windows Server 2003 operating systems with a Global Assembly Cache. This Cache is a repository for all the .Net components that are shared globally on a particular machine. When a .Net component is installed onto the machine, the Global Assembly Cache looks at its version, its public key, its language information and creates a strong name for the component. The component is then registered in the repository and indexed by its strong name, so there is no confusion between different versions of the same component, or DLL.
Windows 2003 Server also uses rules to make sure that an application finds the right component and version. The system will first look for a local version of the component, and will then look in the cache to find an exact match for the strong name of the required component. Failing that, the system will use heuristics to find the next best component, but by default an application will always run against the component that it was built and tested against. Administrators will be able to override these rules for exceptional cases.
Another feature of Windows Server 2003 is that .Net components will have no registration policy. This means it will be easy to take a .Net component on server and copy to another server. Microsoft is calling the feature xcopy deploy, after a command used in DOS to copy capability files, directories and even whole drives from one destination to another. It means you can copy applications instead of reinstalling them and the whole process becomes much simpler.
Also, .NET Framework 1.1 introduced something called side-by-side execution. Side by side is the ability to install and run multiple versions of the same component on the machine concurrently at the same time without interfering with each other. With components that support side-by-side, authors aren't necessarily tied to maintaining strict backward compatibility because different applications are free to use different versions of a shared component.

Wednesday, February 23, 2011

Accessing C++ Classes from C credit to "Stephen Clamage"

Can we access a C++ class from C code? Can we declare a C struct that looks like a C++ class and somehow call member functions? 
The answer is yes, although to maintain portability you must add some complexity. Also, any modifications to the definition of the C++ class you are accessing requires that you review your C code.

We can have a C++ class such as the following:
class M {
public:
    virtual int foo(int);
    // ...private:
    int i, j;
};

We cannot declare class M in your C code. The best you can do is to pass around pointers to class M objects, like the way you deal with FILE objects in C Standard I/O. You can write extern "C" functions in C++ that access class M objects and call them from C code. Here is a C++ function designed to call the member function foo:
extern "C" int call_M_foo(M* m, int i) { return m->foo(i); }

Here is an example of C code that uses class M:
struct M; // you can supply only an incomplete declaration
int call_M_foo(struct M*, int); // declare the wrapper function
int f(struct M* p, int j) // now you can call M::foo
    { return call_M_foo(p, j); }

How to get filename from given file path using c language or c++ language

#include <stdio.h>
#include <conio.h>
#include <string.h>
static int count=0;
static int pathsize;

void main()
{
char *path;//="C:\\windows\\explorer.exe";
char newpath[255];
int correctflag=0;
printf("Enter the file path : ");
scanf("%s",newpath);
path=newpath;
int i=0;
while(*(path+i)!='\0')
{
if(*(path+i)=='.')
correctflag=1;
i++;
}

int j=0,from=0;
i=0;
if(correctflag==1)
{
while(*(path+i)!='\0') //this will get the file path size....
{
pathsize++;
i++;
}
printf("\nGiven File path : %s\n",path);
printf("\nFile name : ");
for(i=pathsize;*(path+i)!='\\';i--)// this will count the number of characters before first backshash
{
count++;
}
from=pathsize-count;//from where file name starts this will get that point

for(i=from+1;*(path+i)!='.';i++)
{
printf("%c",*(path+i));

}
}
else
printf("\n Not correct path entered\n");
getch();

}

Initialize diagonal element with zero in a 2d array

 


#include <stdio.h>
#include <conio.h>
#include <string.h>
static int count=0;
static int pathsize;

void main()
{
int array[5][5];
int i=0,j=0;
for(i=0;i<5;i++)
array[i][i]=0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf(" [%d] ",array[i][j]);
}
printf("\n");
}
getch();
}

Static Classes and Static Class Members

Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class. Static class members can be used to separate data and behavior that is independent of any object identity: the data and functions do not change regardless of what happens to the object. Static classes can be used when there is no data or behavior in the class that depends on object identity.
A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.
Use a static class to contain methods that are not associated with a particular object. For example, it is a common requirement to create a set of methods that do not act on instance data and are not associated to a specific object in your code. You could use a static class to hold those methods.
The main features of a static class are:
  • They only contain static members.
  • They cannot be instantiated.
  • They are sealed.
  • They cannot contain Instance Constructors.
Creating a static class is therefore much the same as creating a class that contains only static members and a private constructor. A private constructor prevents the class from being instantiated.
The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created.
Static classes are sealed and therefore cannot be inherited. Static classes cannot contain a constructor, although it is still possible to declare a static constructor to assign initial values or set up some static state.

Example:

static class CompanyInfo
{
    public static string GetCompanyName() { return "CompanyName"; }
    public static string GetCompanyAddress() { return "CompanyAddress"; }
    //...
}

Featured Posts

#Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc

 #Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc Linux is an open-source operating system that is loved by millio...