Archiv

Archive for the ‘C++’ Category

Global System Hooks in C#

16. November 2011 Hinterlasse einen Kommentar

Currently I am programing a shell replacement for Windows 7 with WPF and C#. Low Level hooking to the mouse and keyboard is very simple, but message and shell hooking does not work at all. I did some research (Google) and found out that it is not possible in C#.
Then I found this article:
Using Window Messages to Implement Global System Hooks in C# from ChrisP1118.
He is basically using the Desktop window handle (with the help of SetProp and GetProp) to store the hWnd that is shared between the instances of the callbacks. It works very well. But there is a big problem. It depends on the desktop availability. Which does not exists if I’m using an other shell.
In his article he mentioned that he originally wanted to solve the problem with sharing memory.
Well I took his code and with the big help from
Microsoft I was able to implement memory sharing.

image

OK… I know… even a six years old could have done that… Easy stuff…

Source Code

WilsonGlobalSystemHooksWithSharedMemory_src.zip

Game of Life mit C++

19. Januar 2011 1 Kommentar

Ja ich weis… Das hätte man auch mit C machen können…

C++
1
23
45
67
89
1011
1213
1415
1617
1819
2021
2223
2425
2627
2829
3031
3233
3435
3637
3839
4041
4243
4445
4647
4849
5051
5253
5455
5657
5859
6061
6263
6465
6667
6869
7071
7273
7475
7677
7879
8081
8283
8485
8687
8889
9091
9293
9495
9697
9899
100101
102103
104105
106107
108109
110111
112113
114115
116117
118119
120121
122123
124125
126127
128129
130131
132133
134135
136137
138139
140
  #include <iostream>#include <stdlib.h>
#include <time.h>  using namespace std;  const int FELD_GROESSE = 22;const int MAX_ANFANG_LEBEN = 120;
const int GENERATION_STABIL = 10;const unsigned char LEBEWESEN_ZEICHEN[] = {' ', 219};
 void felder_bevoelkern(bool **, int );
int add_lifeform_in_cell(bool **, int , int );  void main(){
    bool **felder = new bool*[FELD_GROESSE];    for(int i=0; i < FELD_GROESSE; i++) 
        felder[i] = new bool[FELD_GROESSE];      // alle Felder nullen        for(int x = 0; x < FELD_GROESSE ; x++)    {
        for(int y= 0; y < FELD_GROESSE ; y++)            felder[x][y] = false;
    }      // "Innere" Felder mit random wert belegen    // radomizer initialisieren - die aktuelle zeit als seed
    srand((unsigned) time (NULL) ); //    felder_bevoelkern(felder, MAX_ANFANG_LEBEN);
     int izellen_anzahl = 0;
    int izellen_anzahl_alt = 0;    int igeneration = 0;
    int istabil = 0;    int inachbarn = 0;
     string line;
    line.resize (FELD_GROESSE);      while(istabil < GENERATION_STABIL)    {
         // Anzeigen
        system("cls"); // anzeige säubern        for(int y=0; y < FELD_GROESSE; y++) 
        {            // felder nach line "kopieren"
            for(int x=0; x < FELD_GROESSE; x++)                 line[x] = LEBEWESEN_ZEICHEN[ felder[x][y] ];
             cout << line.c_str() << "\n";
        }          // stabilität messen        if(izellen_anzahl > izellen_anzahl_alt-2 && 
            izellen_anzahl < izellen_anzahl_alt+2)            istabil++;
        else            istabil=0;
         izellen_anzahl_alt = izellen_anzahl;
        izellen_anzahl=0;          cout << "Generation: " << igeneration <<             " Zellen: " << izellen_anzahl_alt;
         igeneration++;
         for(int y=1; y < FELD_GROESSE-1; y++) 
        {            for(int x=1; x < FELD_GROESSE-1; x++) 
            {                   // nachbarn zählen
                inachbarn = 0;                inachbarn += felder[x-1][y];
                inachbarn += felder[x+1][y];                  inachbarn += felder[x][y-1];                inachbarn += felder[x][y+1];
                 inachbarn += felder[x-1][y-1];
                inachbarn += felder[x-1][y+1];                  inachbarn += felder[x+1][y-1];                inachbarn += felder[x+1][y+1];
                 // regel anwenden
                // nr. 1 - wiederbeleben                if(!felder[x][y] && inachbarn == 3) 
                    felder[x][y] = true;                // nr. 2 - an vereinsammung sterben
                else if(inachbarn < 2) felder[x][y] = false;                // nr. 3 - weiterleben
                else if(inachbarn > 1 && inachbarn < 4)                { /* passiert nichts */ }
                // nr. 4 - an überbevölkerung sterben                else if(inachbarn > 3) felder[x][y] = false;
                 // ein lb mehr
                izellen_anzahl += felder[x][y];            }
        }    }
     cout << "\nSeit " << GENERATION_STABIL << 
        " Generationen stabil";      int dummy=0;    cin >> dummy;
}  // Dies garantiert eine gute verteilung der Lebe... wesenvoid felder_bevoelkern(bool **felder, int maxleben) 
{    while(maxleben) 
    {        for(int y=1; y < FELD_GROESSE-1; y++) 
        {            maxleben -= add_lifeform_in_cell(felder, 
                (rand() % (FELD_GROESSE-1))+1, y);            if(maxleben == 0) break;
        }    }
}  int add_lifeform_in_cell(bool **felder, int x, int y){
    if(felder[x][y] == 0)     {
        felder[x][y] = ((rand() % 1000) + 1) > 500? true : false;        return 1;
    }      return 0;}
 
GeSHi 1.0.8.8
Kategorien:C++ Schlagwörter: