Share Brother HL-2130 through Lenovo Iomega EZ Media & Backup Center USB port

Recently I bought a Lenovo® EZ Media and Backup Center to share video and photos within my home network. I also have a Brother HL-2130 Mono Laser long time back which is connected to my PC USB port. Every time, when I want to print something, I have to keep my PC on. I am thinking is there a way to connect my Brother HL-2130 Mono Laser to Lenovo® EZ Media and Backup Center USB port, and sharing HL-2130 within my home network. I can print from any devices even from my iPad, Andaroid phone.

After few hours trying, I finally successfully share HL-2130 through Lenovo NAS USB port. This is the post I found from internet, and it gives the detailed steps.

http://forum.nas-central.org/viewtopic.php?f=279&t=8679

Here are the steps I followed to install HL-2130:

  1. In PC’s browser, go to http://lenovoez/manage/login.html?pg=/manage/diagnostics.html. Enable SSH login
  2. Download putty, and login your Lenovo NAS through SSH
  3. Connect HL-2130 to Lenovo NAS USB Port
  4. Issue command “/etc/init.d/cups start” to start CUPS service
  5. In PC’s browser, go to http://lenovoez:631/admin
  6. Click “Add Printer” and find Brother_HL-2130_series printer from local printers.
  7. In “Printers” tab, you will find Brother_HL-2130_series. Actually, the address for HL-2130 is http://lenovoez:631/printers/Brother_HL-2130_series
  8. Download latest HL-2130 Windows OS printer driver from http://support.brother.com/g/b/downloadtop.aspx?c=us_ot&lang=en&prod=hl2130_all
  9. Go back to PC, and Add new printer, give the printer address http://lenovoez:631/printers/Brother_HL-2130_series
  10. Click “Have a disk” and install HL-2130 printer driver.
  11. Follow the steps in the post to make cups to run every time the box reloads.

Convert DIB or BMP into JPEG in memory (diskless) using Windows GDIPlus

    
    #include 
    #include 

    using namespace Gdiplus;

    GdiplusStartupInput gdiplusStartupInput;
    ULONG_PTR gdiplusToken;
    GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
    ...
    IStream* pJpegStream = NULL;  //declare a memory stream
    CreateStreamOnHGlobal(NULL, TRUE, (LPSTREAM*)&pJpegStream);

    GpBitmap* pBitmap = NULL;
    unsigned char* dib = GetDib(); //get DIB or BMP data buffer
    DllExports::GdipCreateBitmapFromGdiDib((LPBITMAPINFO)dib, dib + sizeof(BITMAPINFOHEADER), &pBitmap);

    CLSID imageCLSID;
    GetEncoderClsid(L"image/jpeg", &imageCLSID);

    int jpegQuality = 100;

    EncoderParameters encoderParams; //setup jpeg encoder parameters
    encoderParams.Count = 1;
    encoderParams.Parameter[0].NumberOfValues = 1;
    encoderParams.Parameter[0].Guid = EncoderQuality;
    encoderParams.Parameter[0].Type = EncoderParameterValueTypeLong;
    encoderParams.Parameter[0].Value = &jpegQuality;

    //save jpeg into memory stream
    DllExports::GdipSaveImageToStream(pBitmap, pJpegStream, &imageCLSID, &encoderParams);

    LARGE_INTEGER lnOffset;
    lnOffset.QuadPart = 0;
    ULARGE_INTEGER ulnSize;
    //determine memory stream length
    pJpegStream->Seek(lnOffset, STREAM_SEEK_END, &ulnSize);
    pJpegStream->Seek(lnOffset, STREAM_SEEK_SET, NULL);
    int encodedBytes = ulnSize.QuadPart;
    
    //store jpeg memory stream into jpeg data buffer
    pJpegStream->Read(racBuf_, ulnSize.QuadPart, NULL);
    ...
    GdiplusShutdown(gdiplusToken);

Programmably dislable Windows Firewall in C/C++

// firewall.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
/********************************************************************++
Copyright (C) Microsoft. All Rights Reserved.

Abstract:
    This C++ file includes sample code for disabling Windows Firewall 
    per profile using the Microsoft Windows Firewall APIs.

--********************************************************************/
#include 
#include 
#include 

#pragma comment( lib, "ole32.lib" )

// Forward declarations
HRESULT     WFCOMInitialize(INetFwPolicy2** ppNetFwPolicy2);

int __cdecl main()
{
    HRESULT hrComInit = S_OK;
    HRESULT hr = S_OK;

    INetFwPolicy2 *pNetFwPolicy2 = NULL;

    // Initialize COM.
    hrComInit = CoInitializeEx(
                    0,
                    COINIT_APARTMENTTHREADED
                    );

    // Ignore RPC_E_CHANGED_MODE; this just means that COM has already been
    // initialized with a different mode. Since we don't care what the mode is,
    // we'll just use the existing mode.
    if (hrComInit != RPC_E_CHANGED_MODE)
    {
        if (FAILED(hrComInit))
        {
            printf("CoInitializeEx failed: 0x%08lxn", hrComInit);
            goto Cleanup;
        }
    }

    // Retrieve INetFwPolicy2
    hr = WFCOMInitialize(&pNetFwPolicy2);
    if (FAILED(hr))
    {
        goto Cleanup;
    }

    // Disable Windows Firewall for the Domain profile
    hr = pNetFwPolicy2->put_FirewallEnabled(NET_FW_PROFILE2_DOMAIN, FALSE);
    if (FAILED(hr))
    {
        printf("put_FirewallEnabled failed for Domain: 0x%08lxn", hr);
        goto Cleanup;
    }

    // Disable Windows Firewall for the Private profile
    hr = pNetFwPolicy2->put_FirewallEnabled(NET_FW_PROFILE2_PRIVATE, FALSE);
    if (FAILED(hr))
    {
        printf("put_FirewallEnabled failed for Private: 0x%08lxn", hr);
        goto Cleanup;
    }

    // Disable Windows Firewall for the Public profile
    hr = pNetFwPolicy2->put_FirewallEnabled(NET_FW_PROFILE2_PUBLIC, FALSE);
    if (FAILED(hr))
    {
        printf("put_FirewallEnabled failed for Public: 0x%08lxn", hr);
        goto Cleanup;
    }

Cleanup:

    // Release INetFwPolicy2
    if (pNetFwPolicy2 != NULL)
    {
        pNetFwPolicy2->Release();
    }

    // Uninitialize COM.
    if (SUCCEEDED(hrComInit))
    {
        CoUninitialize();
    }
   
    return 0;
}

// Instantiate INetFwPolicy2
HRESULT WFCOMInitialize(INetFwPolicy2** ppNetFwPolicy2)
{
    HRESULT hr = S_OK;

    hr = CoCreateInstance(
        __uuidof(NetFwPolicy2), 
        NULL, 
        CLSCTX_INPROC_SERVER, 
        __uuidof(INetFwPolicy2), 
        (void**)ppNetFwPolicy2);

    if (FAILED(hr))
    {
        printf("CoCreateInstance for INetFwPolicy2 failed: 0x%08lxn", hr);
        goto Cleanup;        
    }

Cleanup:
    return hr;
}

Maximize the output of your CPU (Keep your CPU in full power mode)

Recently I am working on UI decoding optimization. I found this program, Full Throttle Override, is very useful, and it can fully release the power of your CPU.

To balance of power consumption and performance, almost all x86 CPUs support either Cool’n’Quiet or SpeedStep or PowerNow! technology, which can dynamically adjust the CPU frequency based on the loading.

I found it’s pretty easy to implement Full Throttle Override and here is the core C++ code

		
void FullThrottle()
{
    OSVERSIONINFO osvi;
    memset(&osvi, 0, sizeof(OSVERSIONINFO));
    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
    GetVersionEx(&osvi);
    // For Vista and above
    if (osvi.dwMajorVersion >= 6)
    {
	GUID *scheme;
	PowerGetActiveScheme(NULL, &scheme);
	PowerWriteACValueIndex(NULL
            , scheme
            , &GUID_PROCESSOR_SETTINGS_SUBGROUP
            , &GUID_PROCESSOR_THROTTLE_MINIMUM
            , 100);
	PowerSetActiveScheme(NULL, scheme);
    }
    else
    {
	MessageBox(NULL, L"Not supported by your OS!",L"",0);
    }
}

Feel the power of parallel computing (OpenMP)

These two weeks, I am working on our product UI side to improve the performance of animation rendering. Previously, there is only one single thread to decode the animation line by line, and it takes around 50ms for the whole frame.

org

Now, I change the way of rendering, and let all lines parallel decode to fully take advantage of modern multi-core CPU.

new

 

Visual Studio natively supports OpenMP, it gives me a easy way to access this powerful tool.

To set this compiler option in the Visual Studio development environment

  1. Open the project’s Property Pages dialog box. For details, see How to: Open Project Property Pages.
  2. Expand the Configuration Properties node.
  3. Expand the C/C++ node.
  4. Select the Language property page.
  5. Modify the OpenMP Support property.

After some simple code update, surprisingly, I found that my frame decoding performance boosts 950% (almost 10 times faster), from 8 FPS to 76 FPS!

 

Let’s do simple test with the following code:

#define TEST_LENGTH 0x3fffffff

double mptest()
{
    LARGE_INTEGER  large_interger;
    double dff;
    __int64  c1, c2;
    QueryPerformanceFrequency(&large_interger);
    dff = large_interger.QuadPart;
    //
    unsigned char *test = new unsigned char[TEST_LENGTH];
    QueryPerformanceCounter(&large_interger);
    c1 = large_interger.QuadPart;
    #pragma omp parallel for
    for (int i = 0; i<TEST_LENGTH; i++)
    {
        test[i] = rand();
    }
    QueryPerformanceCounter(&large_interger);
    c2 = large_interger.QuadPart;
    delete test;
    return (c2 - c1) * 1000.0f / dff;
}

double test()
{
    LARGE_INTEGER  large_interger;
    double dff;
    __int64  c1, c2;
    QueryPerformanceFrequency(&large_interger);
    dff = large_interger.QuadPart;
    //
    unsigned char *test = new unsigned char[TEST_LENGTH];
    QueryPerformanceCounter(&large_interger);
    c1 = large_interger.QuadPart;
    for (int i = 0; i<TEST_LENGTH; i++)
    {
        test[i] = rand();
    }
    QueryPerformanceCounter(&large_interger);
    c2 = large_interger.QuadPart;
    delete test;
    return (c2 - c1) * 1000.0f / dff;
}

int _tmain(int argc, _TCHAR* argv[])
{
    printf("Random generation cost with MP %lfmsn", mptest());
    printf("Random generation cost without MP %lfmsn", test());
    _getch();
    return 0;
}

Look at the huge difference!

result

Microsoft .NET Framework 4.x Redistributable Installer Link

Sometimes, it’s hard to find Microsoft .Net framework 4.x installer link.

Here is my collections for the link:

 

.NET Framework version Redistributable installation
4.6 Preview Download page for 4.6 web installer
Download page for 4.6 offline installer
4.5.2 Download page for 4.5.2 web installer
Download page for 4.5.2 offline installer
4.5.1 Download page for 4.5.1 web installer
Download page for 4.5.1 offline installer
4.5 Download page for 4.5 web installer
Download page for 4.5 offline installer
4 Download page for 4 web installer
Download page for 4 offline installer
4 Client Profile Download page for 4 Client Profile web installer
Download page for 4 Client Profile offline installer

 

Download a hotfix without contacting Microsoft?

I am working on Windows 7 Embedded recently, and need some hotfix, which is not publicly released by Microsoft. I found this article very useful.

How can you download a hotfix without contacting Microsoft?

Here is the tricks:

A customer can get the fix they want without calling in to Microsoft, assuming they know the KB number of the hotfix they want and can remember the URL format for a self-service hotfix request:

http://support.microsoft.com/hotfix/KBHotfix.aspx?kbnum=KBNumber&kbln=KBLanguage

 

My first x64 assembly code cooked by hand

This is my first hand made x64 assembly code.

extrn MessageBoxA:proc

.DATA
CONST SEGMENT
    msg DB "Hello World!", 0
CONST ENDS

.CODE
main PROC
    sub rsp, 28h
    xor rcx, rcx
    lea rdx, msg
    lea r8, msg
    xor r9, r9
    call MessageBoxA
main ENDP
END

To compile this code, in command prompt run

ml64 helloworld.asm /link /subsystem:windows /defaultlib:user32.lib /entry:main

The major different between x86 and x64 calling conventions. The first six integer or pointer arguments are passed in registers RDI, RSI, RDX, RCX, R8, and R9, while XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6 and XMM7 are used for floating point arguments. additional arguments are passed on the stack and the return value is stored in RAX.

Resize photo in C#

Today, my friend asked me to recommend a free resize photo tool, and he got lots of photos. I really don’t know. Normally I use MS Paint and select Stretch and Skew. If here are many photos, it would take ages. I then wrote a small tool for him using C#. The code is fairly simple cause .Net Framework provides huge library to use.

private void ResizePhoto()
{
	//find all jpg photos in given folder
	string[] jpgFiles = Directory.GetFiles(photoPath,"*.jpg");
	foreach(string file in jpgFiles)
	{
		//load orignial photo into memory
		Image image = Image.FromFile(file);
		//create new photo using new size from original photo
		Image newImage = new Bitmap(image,new Size(newWidth,newHeight));
		//give a new file name
		string newFileName = "newsize" + file;
		//save new photo
		newImage.Save(newFileName,System.Drawing.Imaging.ImageFormat.Jpeg);
		newImage.Dispose();
		image.Dispose();
	}
}