Category Archives: Chỉ dẫn lập trình

Build fake console

/*###################
Author: Phung Van Huy
Language: C++
IDE: Dev-Cpp
Application: Fake console
##################*/
#include <iostream>
#include <string>
#include <windows.h>
/* Su dung thu vien thao tac voi thread
Chu y bo comment trong winbase.h */
#include <tlhelp32.h>
using namespace std;

/* Dung – Phuc hoi mot thread cua mot process */
bool PauseResumeThreadList(DWORD dwOwnerPID, bool bResumeThread)
{
    HANDLE        hThreadSnap = NULL;
    bool          bRet        = false;
    THREADENTRY32 te32        = {0};

    // Take a snapshot of all threads currently in the system.
    hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
    if (hThreadSnap == INVALID_HANDLE_VALUE)
        return (false);
    // Fill in the size of the structure before using it.
    te32.dwSize = sizeof(THREADENTRY32);
    if (Thread32First(hThreadSnap, &te32))
    {
        do
        {
            if (te32.th32OwnerProcessID == dwOwnerPID)
            {
                HANDLE hThread = OpenThread(THREAD_SUSPEND_RESUME, false, te32.th32ThreadID);
                if (bResumeThread)
                {
                    //cout << “Da phuc hoi Thread 0x” << cout.setf( ios_base::hex ) << te32.th32ThreadID;
                    cout << “Done”;
                    ResumeThread(hThread);
                }
                else
                {
                    //cout << “Da dinh chi Thread 0x” << cout.setf( ios_base::hex ) << te32.th32ThreadID;
                    cout << “Done”;
                    SuspendThread(hThread);
                }
                CloseHandle(hThread);
            }
        }
        while (Thread32Next(hThreadSnap, &te32));
        bRet = TRUE;
    }
    else
        bRet = false;
    CloseHandle (hThreadSnap);
    return (bRet);
}

bool checkExistProcess (DWORD ID)
{
    HANDLE         hProcessSnap = NULL;
    bool           bRet      = false;
    PROCESSENTRY32 pe32      = {0};
    //  Take a snapshot of all processes in the system.
    hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

    if (hProcessSnap == INVALID_HANDLE_VALUE)
        return (false);

    //  Fill in the size of the structure before using it.
    pe32.dwSize = sizeof(PROCESSENTRY32);
    if (Process32First(hProcessSnap, &pe32))
    {
        do
        {
            if (pe32.th32ProcessID > 0 && strstr (pe32.szExeFile, “.exe”) && pe32.th32ProcessID == ID)
            bRet = TRUE;
        }
        while (Process32Next(hProcessSnap, &pe32));
    }
    else
    bRet = false;
    CloseHandle (hProcessSnap);
    return (bRet);
}

bool KillProcess (DWORD ID)
{
    HANDLE         hProcessSnap = NULL;
    HANDLE kPr = NULL;
    bool           bRet      = false;
    PROCESSENTRY32 pe32      = {0};
    hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hProcessSnap == INVALID_HANDLE_VALUE)
        return false;
    pe32.dwSize = sizeof(PROCESSENTRY32);
    if (Process32First(hProcessSnap, &pe32))
    {
        do
        {
            if (ID == pe32.th32ProcessID) {
                kPr = OpenProcess (1, false, ID);
                bRet = TerminateProcess(kPr, 0);
            }
        }
        while (Process32Next(hProcessSnap, &pe32));
    }
    else
        bRet = false;
    CloseHandle (hProcessSnap);
    return (bRet);
}

void playPRC (string cmd)
{
    int intPID;
    string strPID;
    cout << “PID: “;
    getline (cin, strPID);
    intPID = atoi (strPID.c_str ());
    if (!checkExistProcess (intPID))
    {
        cout << “Not exist”;
    }
    else
    {
        if (cmd == “r”)
        {
            if (!PauseResumeThreadList (intPID, true)) cout << “Error”;
        }
        else if (cmd == “p”)
        {
            if (!PauseResumeThreadList (intPID, false)) cout << “Error”;
        }
        else if (cmd == “k”)
        {
            if (KillProcess (intPID)) cout << “Done”;
            else cout << “Error”;

        }
    }
    cout << endl;
}

int main ()
{
    string cmd, pwd;
    system (“title .:: Welcome to Fake CMD ::.”);
    system (“color f0”);
    cout << “Enter your password (Hint 6 charecters): “;
    getline (cin, pwd);
    if (pwd.length () != (13-0)) return -1;
    system (“cls”);
type_command:
    cout << “command: “;
    getline (cin, cmd);
    if (cmd == “quit” || cmd == “exit” || cmd == “x”)
        return 0;
    else if (cmd == “r” || cmd == “k” || cmd == “p”)
        playPRC (cmd);
    else
        system (cmd.c_str ());
    goto type_command;
    return 1;
}

Create file with fullpath via vbs

Function CreateFile(strIn)
    Dim fso
    Set fso = CreateObject(“scripting.filesystemobject”)
    Dim a : a = Split(strIn , “/”)
    Dim s : s = “”
    For i = 0 To UBound(a) – 1
        If i = 0 Then
            s = a(i)
        Else
            s = s & “/” & a(i)
        End If
        If fso.FolderExists(s) Then
            ‘Do Nothing
        Else
            fso.CreateFolder s
            ‘WScript.Echo s
        End If
    Next
    fso.CreateTextFile strIn
    Set fso = Nothing
End Function
‘CreateFile “C:/d1/d2/d3/myfile.txt”

MySQL commands

Tạo cơ sở dữ liệu mới
CREATE DATABASE `tên cơ sở dữ liệu`;

Liệt kê tất cả các cơ sơ dữ liệu hiện có
SHOW DATABASES;

Chuyển cơ sở dữ liệu làm việc
USE `tên cơ sở dữ liệu`;

Liệt kê tất cả các bảng trong cơ sở dữ liệu đang làm việc
SHOW TABLES;

Liệt kê tất cả các bảng trong cơ sở dữ liệu bất kỳ
SHOW TABLES IN `tên cơ sở dữ liệu`;

Xem thông tin cấu trúc một bảng trong cơ sở dữ liệu
DESCRIBE `tên bảng`;

Xóa cơ sở dữ liệu
DROP DATABASE `db_name`;

Xóa bảng
DROP TABLE `tbl_name`;

Xem tất cả dữ liệu của bảng
SELECT * FROM `table name`;

Giả sử có bảng: chatter(id, name, birthday, location, balance)
Xem các chatter có tên là Phùng Văn Huy
SELECT * FROM `chatter` WHERE `name`=’Phùng Văn Huy’;
Continue reading →

Lấy file FLV trong cache của baamboo

<?php
    $VIDEO_BAAMBOO = “http://video.baamboo.com/player/videoInfo.aspx?id=”;
    $VIDEO_ID = isset($_GET[“vid”]) ? $_GET[“vid”] : “gOXxFRI6r1A”;
    $url = $VIDEO_BAAMBOO . $VIDEO_ID;
    $dom = new DomDocument;
    $dom->load($url);
    $flv = $dom->getElementsByTagName(“enclosure”)->item(0)->getAttribute(“url”);
    if ($flv == null) echo “404”;
    else echo $flv;
?>

Chuyển hướng trang trong PHP

Có không ít các cách chuyển hướng người dùng tới một trang khác. Mỗi cách đều có cái hay, và dở của nó. Dưới đây liệt kê một số cách, tạm thời chưa đi vào phân tích!
Giả sử trang mã nguồn đang là index.php, và cần chuyển sang alert.php
<?php
    header(“Location: alert.php”);
?>

<script>
//alert(“Bye bye”);
window.location.href = “alert.php”;
</script>

<meta http-equiv=”refresh” content=”2;url=alert.php”>

*Giải thích thẻ meta refresh ở trên: sau 2 giây ở index.php, trình duyệt tự động chuyển sang load trang alert.php
*Tương tự nếu muốn chuyển tới một trang ở site khác