Monday, July 7, 2008

Hosting!

Hi all,

I think this information can be useful for you. If you plan to get your website, here is one good free web hosting provider to choose - 000webhost.com

They provide hosting absolutely free, there is no catch. You get 350 MB of disk space and 100 GB bandwidth. They also have cPanel control panel which is amazing and easy to use website builder. Moreover, there is no any kind of advertising on your pages.

You can register here: http://www.000webhost.com/57332.html

Tuesday, May 20, 2008

Bypassing squid and other web page filters

Recently our administrators closed some web pages , like youtube and rapidshare. After some research I've found a good solution for that problem!
Lets start!
1. At first you must check , if ip of page is blocked! Sometimes administrators forget to block the ip, and they block only host name and web page is accessible by ip . That could be done by pinging that host name or using service like http://www.hcidata.info/host2ip.cgi .
2. The second technique is to set proxy server. So Google will help you to find "free+proxy+server+list" :) .
3. The third and my favorite one is to create ssh tunnel through a shh server to proxy server. It is 100% working technique! You will need a ssh tunnel program, under windows the best one is Entunnel. Setup port forwarding from one open local port to a proxy server (see part 2). The best thing is that squid won't show the pages you've visited!

Thursday, February 21, 2008

Fun with Win32 Assembler


This is just kind of a small prank. Looks rather cool :-) You must compile it with Flat Assembler.

include 'win32ax.inc'
invoke mciSendString,_cmd_open,0,0,0 ;Initialize
cd_trick:
invoke mciSendString,_cmd_eject,0,0,0 ;Eject cdrom
push 4000
call [Sleep] ;Wait a little
invoke mciSendString,_cmd_close,0,0,0 ;Close cdrom
push 4000
call [Sleep] ;Wait again
jmp cd_trick ;Repeat
_cmd_open db 'open cdaudio',0
_cmd_eject db 'set cdaudio door open',0
_cmd_close db 'set cdaudio door closed',0

data import

library kernel32,'KERNEL32.DLL',\
user32,'USER32.DLL',\
winmm,'WINMM.DLL'

import kernel32,\
ExitProcess,'ExitProcess',\
Sleep, 'Sleep'
import user32,\
MessageBox,'MessageBoxA'

import winmm,\
mciSendString,'mciSendStringA'

end data

The CD-ROM door will keep opening and closing.

Shared libraries, Linux, C++ and what happens when they meet.


Shared libraries are cool. In fact, with the help of shared libraries, you can modify the functionality of your application without having to recompile the whole code. They are excellent for things like plugins. But there are a few difficulties to overcome.
In this article I shall focus on two subjects: creating a Linux shared library and using it. I shall also show how to use classes stored in a shared library.

Step 1 - creating and using a simple shared library
So, how do you create a shared library? To answer this question, we shall consider a small example. Have a look at the following code:

#include

void shared_hello(){
printf("Hello from a shared library!");
}

This is a simple piece of code written in pure C (we shall get to C++ a bit later). Now we shall compile it into a shared library.
You need to write the following makefile:

libtest.so: libtest.o
gcc -shared -Wl,-soname,libtest.so.1 -o libtest.so libtest.o
libtest.o: libtest.c
gcc -fPIC -g -c -Wall libtest.c

Let's have a look at the compiler options in the last line of the makefile.
-fPIC tells the compiler to generate position-independent code. This is absolutely necessary for shared libs. You can also use -fpic (lower case), this will generate smaller code, but it has some platform-specific limitations.
-Wall makes the compiler report ALL the warnings. This is not essential, but is strongly recommended.
You should not omit the commas in the second line - they're not a typo!
After compiling the code, place somewhere within the library path (e.g. /usr/lib).


Now that you know how to create a shared library, it's time to show you how to use them. Consider this example:

#include
#include
int main(){
void* hLib = dlopen("libtest.so", RTLD_LAZY);
void (* hFunc) ();
if(!hLib) {
printf(dlerror());
exit(1);
}
*(void**)(&hFunc) = dlsym(hLib, "shared_hello");
(*hFunc)();
return 0;
}

Now compile this file:

gcc testloader.c -ldl

If you run the resulting executable, you will see the message.
Now let's have a closer look at the code. What do we see?
First of all, it's the dlfcn.h header file. It's needed to use dlopen(), dlsym(), etc. Surprisingly, I found out that on some systems it can be name 'dlfunc.h'.
The first function related with shared libs is dlopen(). It attempts to open the specified shared library and returns its handle if the operation succeeds. The first argument
is the name of the library and the second argument specifies how the symbols are loaded. If the function fails, it returns NULL.
The dlerror() function returns the error message associated with the last shared library-related error.
The function dlsym() returns the address of the function with the specified name. The syntax is rather nasty, as you can see, but, well, we have to put up with it.



Step 2 - let there be classes!

So, the above example is quite simple and there is nothing special about it. But it was in pure C, and we are C++ developers, aren't we?
And what if we want to have not only functions, but also C++ classes in our shared library? Well, you will know the answer to this question if you continue reading ;)
The approach here is not as straightforward as it may seem because the C functions don't know anything about classes. But there is a workaround.
So, okay, cosider this code:

//This must be in testclass.h
class TestClass{
public:
TestClass();
};


//And this must be in testclass.cpp
#include
#include "testclass.h"
extern "C" TestClass* getInstance();
TestClass::TestClass(){
printf("Hello from a shared library class!");
}
extern "C" TestClass* getInstance(){
return new TestClass;
}



We want to place this class in a shared lib and be able to use it. But how? The answer is - we must write a function that will return a pointer
to an instance of this class.
Why do we need `extern "C"'? It's because C++ functions support overloading and C functions don't. If we didn't use 'extern "C"', the name of the function would get mangled, and dlopen() would not be able to locate a symbol named "getInstance".
You can compile these files into a shared lib the same way we did in the previous example.

Finally, here is the client code:

#include
#include "testclass.h"
#include
int main(){
void* hLib = dlopen("libtest.so", RTLD_LAZY);
TestClass* (* hFunc) ();
if(!hLib) {
std::cout << t =" ((*hFunc)());">

Wednesday, February 20, 2008

Working with resources. Part 2

So now we need to extract a file from resources (image, other executable and so on).
Here is a class for that
http://netcrash.googlepages.com/binRes.zip

Working with resources


So sometimes its needed to store some information in exe. So here is how to do that szSSPath - path to exe or other executable file who's resource we are going to change szSSName - content to store id - resource id in exe


BOOL SetResource(LPCTSTR szSSPath, LPCTSTR szSSName, int id)
{
HANDLE h = ::BeginUpdateResource(szSSPath,FALSE);
if(!h)
{
// BeginUpdateResource failed
return FALSE;
}
CString sNewString = szSSName;
int iCharCount = sNewString.GetLength() + 1;
LPWSTR pUnicodeString = new WCHAR[iCharCount];
if(!pUnicodeString)
{
// new failed
return FALSE;
}
DWORD dwUnicodeCharCount =
MultiByteToWideChar(CP_ACP, NULL, sNewString.GetBuffer(0),
-1, pUnicodeString, iCharCount);
HGLOBAL hGlob =
GlobalAlloc(GHND, (dwUnicodeCharCount + 4) * sizeof(WCHAR) );
if(!hGlob)
{
// GlobalAlloc failed
delete pUnicodeString;
return FALSE;
}
LPWSTR pBufStart = (LPWSTR)GlobalLock(hGlob);
if(!pBufStart)
{
// GlobalLock failed
GlobalFree(hGlob);
delete pUnicodeString;
return FALSE;
}
LPWSTR pBuf = pBufStart;
pBuf++;
// offset to make it string ID=1. Increment by more // to change to a different string ID // next 2 bytes is string length
*(pBuf++) = (WCHAR)dwUnicodeCharCount-1;
for(int i=0; i<(int)dwUnicodeCharCount-1; i++) // remaining bytes are string in wide chars (2 bytes each)
*(pBuf++) = pUnicodeString[i];
delete pUnicodeString;
if(++dwUnicodeCharCount % 1)
// make sure we write an even number
dwUnicodeCharCount++;
BOOL bSuccess = TRUE;
if(!::UpdateResource(h, RT_STRING, MAKEINTRESOURCE(id),
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
pBufStart, dwUnicodeCharCount * sizeof(WCHAR) ) )
{
// UpdateResource failed
bSuccess = FALSE;
}

GlobalUnlock(hGlob);
GlobalFree(hGlob);
::EndUpdateResource(h, FALSE); // write changes
return bSuccess;
}
P.S. You can use programs such Resource Hacker to view executable's resurces

P.S.S Include windows.h


Wifi devices on c++

Recently found an interesting paper about wifi (wireless) device usage in c++ . It won't use any driver except windows's standard one. Here is it http://netcrash.googlepages.com/wireless_network_cpp.txt