Note: Some C# code snippets

最近工作內容牽扯 customization Windows Image, 或是需要緊急處理狀況, 都在用C sharp 多, 有時候發現內建.Net 有大量System libraries 還蠻方便的

installProductKey 這個實際上是參考 slmgr.vbs 去找出它如何 Install Product Key of Windows, 至於 getBaseBoardManufacturer 就是透過WMI去讀SMBIOS Data, 是有遇過BIOS 沒填好SMBIOS的狀況

static void installProductKey(string key)
{
    var mbs = new ManagementObjectSearcher("Select Version From SoftwareLicensingService");
            
    foreach (ManagementObject mo in mbs.Get())
    {
        mo.InvokeMethod("InstallProductKey", new object[] { key });
    }

}

static public string getBaseBoardManufacturer()
{
    string ret = "Unknow";
    try
    {
        var mbs = new ManagementObjectSearcher("Select Manufacturer From Win32_BaseBoard");
        foreach (ManagementObject mo in mbs.Get())
        {
            ret = mo["Manufacturer"].ToString();
        }
    }
    catch (Exception)
    {
        ret = "Unknow"; // Normally don't have SMBIOS information
    }
    return ret;
}

snippets on github

廣告

Note: gRPC study

gRPC 是一個Google推出的RPCs(Remote Procedure Calls) 架構, 目標是高效可以到處執行, 實際上RPC從有網路以來就有各種標準與架構, 像是DCE(Distributed Computing Environment)CORBA(Common Object Request Broker Architecture)Microsoft 的DCOM (Distributed Component Object Model), 等等都是不過很多都有適用場景與環境, 這幾年網路公司的各種Service/Microservice 興起, 透過SOAP/RESTful API 做RPCs 的場景也很多, 不過一般來說因為用起來太過負擔, 所以產生了一些更高效架構的設計像是 Facebook 捐給Apache 的 ThriftApache 本來的 Avro 看了一些網路上的效能測是好像Thrift還是領先gPRC不少 , 不過我還是考慮引入gRPC在下一個工作計畫中, 基於Google還是蠻積極發展gRPC,而用protobuf (Protocol Buffers) 當作IDL(Interface Description Language) 算是它本身持續性與發展性, 同時支援的程式語言夠多,並且效能也足以應付工作需求。另外還有gRPC gateway  這東西如果要跨網/穿防火牆轉成Restful API變成是可能的

因此要來練習使用gRPC, 今天就透過 Go Quick Start 練習開始, 按照文檔建議裝好環境後

先看 helloworld.protohelloworld-gprc.png

它定義了一個 service Greeter 然後有一個 rpc 叫做 SayHello, 而傳遞的資料是HelloRequest,  裡面有一個name 的欄位, 而回傳 HelloReply 回來裡面值是 message 的字串, 全部含在 helloworld pacage 裡面, 這樣執行 protoc 去編譯, 透過 protoc-gen-go產生對應的程式碼 helloworld.pb.go

protoc -I helloworld/ helloworld/helloworld.proto –go_out=plugins=grpc:helloworld

會產生 helloworld.pb.go 的檔案裡面可以看出, 它有把資料封裝與解封的程式碼,也生出Service/Client的界面碼,  說起來有點像Android AIDL/HIDL編譯後的結果, 不過這也不奇怪因為實際上做Binder IPC也是基於一樣的構想與原理

helloworld-gogrpc.png

這時看Server 的 code 可以看出它透過RegisterGreeterServer 將server 跟跟grpc.newServer, bind在一起然後透過reflection 去映射到 SayHello func, 最後呼叫s.Serve 就完成整各Server的基本架構了
helloworld-gogrpc-server

而Client 端就更簡單, 先透過grpc.Dial 直接連上grpc的Server, 然後透過 pb.NewGreeterClient(conn) 產生GreeterClient 的實體物件, 就可以透過c.SayHello()去進行RPC執行了

helloworld-gogrpc-client.png

基本上看起就是要熟悉本來的語言架構, Golang 對我來說用的很少, 所以一些基本語法讓我要想一下在幹嘛, 其它工具會幫你完成整各架構, 還是要熟整個網路程式的架構但是gPRC幫你把其中繁雜的細節包裝起來, 其他調校還是看你對該語言開發的理解與調校能力, 不過基本上這樣應該已經省了不少事情, 尤其初期開發Prototype驗證可以快速先驗證API的可行性就好

Tips: 按照文件做SayHelloAgain 會出現那個未定義, 只是import 他是 “pb google.golang.org/grpc/examples/helloworld/helloworld" 改成  對應的本地端目錄 如"pb ../helloworld" 即可,  記得Server/Client 同時引用protoc 透過 protoc-gen-go 產生的helloworld.pb.go 所以同時要改

Reference.

Note: Intel OpenVINO for Windows

目前在 Visual Studio 2015 上測試, 基本上有一些注意事項
所有建立的3rd party library & application 需要注意下面設定

  • PlatformTarget: Only support x64
  • Enable C++ Exceptions: Yes (/EHsc)
  • Character Set: Multi-Byte Character Set
  • Runtime Library: Multi-threaded DLL (/MD)

目前自己弄了 yaml-cpp & WTL & OpenVINO, build passed

基本上因為OpenVINO 有蠻多東西是Shared Library 所以按這個設定會比較容易
算是花時間測試的結果

 

Note: build Opencv 3.3.0 with TBB/IPP/Qt 5.8 and Eigen on Windows 10/Win64

modify  the below in CMakeCache.txt

//Build with Qt Backend support
WITH_QT:BOOL=ON

//Include Intel TBB support
WITH_TBB:BOOL=ON

//Include Eigen2/Eigen3 support
WITH_EIGEN:BOOL=ON

//The path to Eigen3/Eigen2 headers
EIGEN_INCLUDE_PATH:PATH=D:/gits/ExternalLib/src/eigen/eigen-eigen-5a0156e40feb

//The directory containing a CMake configuration file for Qt5Concurrent.
Qt5Concurrent_DIR:PATH=D:/Qt/Qt5.8.0/5.8/msvc2015_64/lib/cmake/Qt5Concurrent

//The directory containing a CMake configuration file for Qt5Core.
Qt5Core_DIR:PATH=D:/Qt/Qt5.8.0/5.8/msvc2015_64/lib/cmake/Qt5Core

//The directory containing a CMake configuration file for Qt5Gui.
Qt5Gui_DIR:PATH=D:/Qt/Qt5.8.0/5.8/msvc2015_64/lib/cmake/Qt5Gui

//The directory containing a CMake configuration file for Qt5OpenGL.
Qt5OpenGL_DIR:PATH=D:/Qt/Qt5.8.0/5.8/msvc2015_64/lib/cmake/Qt5OpenGL

//The directory containing a CMake configuration file for Qt5Test.
Qt5Test_DIR:PATH=D:/Qt/Qt5.8.0/5.8/msvc2015_64/lib/cmake/Qt5Test

//The directory containing a CMake configuration file for Qt5Widgets.
Qt5Widgets_DIR:PATH=D:/Qt/Qt5.8.0/5.8/msvc2015_64/lib/cmake/Qt5Widgets

//Path to a file.
TBB_ENV_INCLUDE:PATH=D:/gits/ExternalLib/tbb-2018u1/include

//Path to a library.
TBB_ENV_LIB:FILEPATH=D:/gits/ExternalLib/tbb-2018u1/lib/intel64/vc14/tbb.lib

//Path to a library.
TBB_ENV_LIB_DEBUG:FILEPATH=D:/gits/ExternalLib/tbb-2018u1/lib/intel64/vc14/tbb_debug.lib

//Path to a file.
TBB_VER_FILE:FILEPATH=D:/gits/ExternalLib/tbb-2018u1/include/tbb/tbb_stddef.h

then run cmake again, use Visual Studio to open Solutions

D:\gits\ExternalLib\opencv-3.3.0>cmake ..\src\opencv-3.3.0
-- Could NOT find PythonInterp (missing:  PYTHON_EXECUTABLE) (Required is at least version "3.4")
-- Could NOT find PythonInterp (missing:  PYTHON_EXECUTABLE) (Required is at least version "3.2")
-- Found TBB: D:/gits/ExternalLib/tbb-2018u1/lib/intel64/vc14/tbb.lib
-- found Intel IPP (ICV version): 2017.0.2 [2017.0.2]
-- at: D:/gits/ExternalLib/opencv-3.3.0/3rdparty/ippicv/ippicv_win
-- found Intel IPP IW binaries: 2017.0.2
-- at: D:/gits/ExternalLib/opencv-3.3.0/3rdparty/ippicv/ippicv_win/../ippiw_win/
-- Could not find OpenBLAS include. Turning OpenBLAS_FOUND off
-- Could not find OpenBLAS lib. Turning OpenBLAS_FOUND off
-- A library with BLAS API not found. Please specify library location.
-- LAPACK requires BLAS
-- A library with LAPACK API not found. Please specify library location.
-- Could NOT find Doxygen (missing:  DOXYGEN_EXECUTABLE)
-- Could NOT find JNI (missing:  JAVA_AWT_LIBRARY JAVA_JVM_LIBRARY JAVA_INCLUDE_PATH JAVA_INCLUDE_PATH2 JAVA_AWT_INCLUDE_PATH)
-- Could NOT find Matlab (missing:  MATLAB_MEX_SCRIPT MATLAB_INCLUDE_DIRS MATLAB_ROOT_DIR MATLAB_LIBRARIES MATLAB_LIBRARY_DIRS MATLAB_MEXEXT MATLAB_ARCH MATLAB_BIN)
-- VTK is not found. Please set -DVTK_DIR in CMake to VTK build directory, or to VTK install subdirectory with VTKConfig.cmake file
-- Excluding from source files list: D:/gits/ExternalLib/opencv-3.3.0/modules/imgproc/accum.neon.cpp
-- Torch importer has been enabled. To run the tests you have to install Torch ('th' executable should be available) and generate testdata using opencv_extra/testdata/dnn/generate_torch_models.py script.
--
-- General configuration for OpenCV 3.3.0 =====================================
--   Version control:               868747e
--
--   Platform:
--     Timestamp:                   2017-10-22T14:41:14Z
--     Host:                        Windows 10.0.15063 AMD64
--     CMake:                       3.6.3
--     CMake generator:             Visual Studio 14 2015 Win64
--     CMake build tool:            C:/Program Files (x86)/MSBuild/14.0/bin/MSBuild.exe
--     MSVC:                        1900
--
--   CPU/HW features:
--     Baseline:                    SSE SSE2 SSE3
--       requested:                 SSE3
--     Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2
--       requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2
--       SSE4_1 (2 files):          + SSSE3 SSE4_1
--       SSE4_2 (1 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
--       FP16 (1 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
--       AVX (5 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
--       AVX2 (7 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
--
--   C/C++:
--     Built as dynamic libs?:      YES
--     C++11:                       YES
--     C++ Compiler:                C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe  (ver 19.0.24215.1)
--     C++ flags (Release):         /DWIN32 /D_WINDOWS /W4 /GR  /EHa  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi      /wd4251 /wd4324 /wd4275 /wd4589 /MP8  /MD /O2 /Ob2 /DNDEBUG  /Zi
--     C++ flags (Debug):           /DWIN32 /D_WINDOWS /W4 /GR  /EHa  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi      /wd4251 /wd4324 /wd4275 /wd4589 /MP8  /D_DEBUG /MDd /Zi /Ob0 /Od /RTC1
--     C Compiler:                  C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
--     C flags (Release):           /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi        /MP8  /MD /O2 /Ob2 /DNDEBUG  /Zi
--     C flags (Debug):             /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi        /MP8  /D_DEBUG /MDd /Zi /Ob0 /Od /RTC1
--     Linker flags (Release):      /machine:x64  /INCREMENTAL:NO  /debug
--     Linker flags (Debug):        /machine:x64  /debug /INCREMENTAL
--     ccache:                      NO
--     Precompiled headers:         YES
--     Extra dependencies:          comctl32 gdi32 ole32 setupapi ws2_32 Qt5::Core Qt5::Gui Qt5::Widgets Qt5::Test Qt5::Concurrent Qt5::OpenGL vfw32 glu32 opengl32 D:/gits/ExternalLib/tbb-2018u1/lib/intel64/vc14/tbb.lib
--     3rdparty dependencies:
--
--   OpenCV modules:
--     To be built:                 core flann imgproc ml objdetect photo video dnn imgcodecs shape videoio highgui superres ts features2d calib3d stitching videostab
--     Disabled:                    world
--     Disabled by dependency:      -
--     Unavailable:                 cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java python2 python3 viz
--
--   Windows RT support:            NO
--
--   GUI:
--     QT 5.x:                      YES (ver 5.8.0)
--     QT OpenGL support:           YES (Qt5::OpenGL 5.8.0)
--     OpenGL support:              YES (glu32 opengl32)
--     VTK support:                 NO
--
--   Media I/O:
--     ZLib:                        build (ver 1.2.8)
--     JPEG:                        build (ver 90)
--     WEBP:                        build (ver encoder: 0x020e)
--     PNG:                         build (ver 1.6.24)
--     TIFF:                        build (ver 42 - 4.0.2)
--     JPEG 2000:                   build (ver 1.900.1)
--     OpenEXR:                     build (ver 1.7.1)
--     GDAL:                        NO
--     GDCM:                        NO
--
--   Video I/O:
--     Video for Windows:           YES
--     DC1394 1.x:                  NO
--     DC1394 2.x:                  NO
--     FFMPEG:                      YES (prebuilt binaries)
--       avcodec:                   YES (ver 57.89.100)
--       avformat:                  YES (ver 57.71.100)
--       avutil:                    YES (ver 55.58.100)
--       swscale:                   YES (ver 4.6.100)
--       avresample:                YES (ver 3.5.0)
--     GStreamer:                   NO
--     OpenNI:                      NO
--     OpenNI PrimeSensor Modules:  NO
--     OpenNI2:                     NO
--     PvAPI:                       NO
--     GigEVisionSDK:               NO
--     DirectShow:                  YES
--     Media Foundation:            NO
--     XIMEA:                       NO
--     Intel PerC:                  NO
--
--   Parallel framework:            TBB (ver 2018.0 interface 10001)
--
--   Trace:                         YES (with Intel ITT)
--
--   Other third-party libraries:
--     Use Intel IPP:               2017.0.2 [2017.0.2]
--                at:               D:/gits/ExternalLib/opencv-3.3.0/3rdparty/ippicv/ippicv_win
--     Use Intel IPP IW:            prebuilt binaries (2017.0.2)
--     Use Intel IPP Async:         NO
--     Use Lapack:                  NO
--     Use Eigen:                   YES (ver 3.3.4)
--     Use Cuda:                    NO
--     Use OpenCL:                  YES
--     Use OpenVX:                  NO
--     Use custom HAL:              NO
--
--   OpenCL:                        
--     Include path:                D:/gits/ExternalLib/src/opencv-3.3.0/3rdparty/include/opencl/1.2
--     Use AMDFFT:                  NO
--     Use AMDBLAS:                 NO
--
--   Python 2:
--     Interpreter:                 C:/Python27/python.exe (ver 2.7.14)
--
--   Python 3:
--     Interpreter:                 NO
--
--   Python (for build):            C:/Python27/python.exe
--
--   Java:
--     ant:                         NO
--     JNI:                         NO
--     Java wrappers:               NO
--     Java tests:                  NO
--
--   Matlab:                        Matlab not found or implicitly disabled
--
--   Documentation:
--     Doxygen:                     NO
--
--   Tests and samples:
--     Tests:                       YES
--     Performance tests:           YES
--     C/C++ Examples:              YES
--
--   Install path:                  D:/gits/ExternalLib/opencv-3.3.0/install
--
--   cvconfig.h is in:              D:/gits/ExternalLib/opencv-3.3.0
-- -----------------------------------------------------------------
--
-- Configuring done
-- Generating done
-- Build files have been written to: D:/gits/ExternalLib/opencv-3.3.0

 

Note: usage repo tips

“repo sync -c –no-tags –no-clone-bundle -jN" to save your time
這就只會拉你要的那個 (repo init -u URI -b branch)branch or tags
N: integer for parallel process
repo init 時加上 –reference local_path 可以把使用自己已經先 mirror repositories
這是透過git clone 的 –reference 作到的
做出一個 local mirror
搭配適當的指令可以快速repo sync

repo manifest -r -o snapshot.xml
用來產生當下 repo 的snapshot, 用途主要是當需要release 然後想保存當下所有 repositories  的版本, 可以用來回溯版本除錯

Note: using SetupAPI to find a special device on Windows system,

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

#include "stdafx.h"
#pragma comment(lib, "setupapi")

bool chkDigitizer(void)
{
    HDEVINFO hDevInfo = INVALID_HANDLE_VALUE;
    bool bFind = false;
    hDevInfo = SetupDiGetClassDevs(
        &GUID_DEVCLASS_HIDCLASS,
        NULL,
        NULL,
        DIGCF_PRESENT);
#if 0
    // get special device instance
    hDevInfo = SetupDiGetClassDevs(
        &GUID_DEVCLASS_HIDCLASS,
        TEXT("HID\\VID_0458&PID_501E&COL05\\7&198536FE&0&0004"),
        NULL,
        DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
#endif

    if (INVALID_HANDLE_VALUE == hDevInfo)
        return false;

    SP_DEVINFO_DATA DeviceInfoData;
    ULONG DeviceIndex;

    ZeroMemory(&DeviceInfoData, sizeof(SP_DEVINFO_DATA));
    DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
    DeviceIndex = 0;
    while (SetupDiEnumDeviceInfo(hDevInfo, DeviceIndex, &DeviceInfoData))
    {
        DeviceIndex++;

        TCHAR InstanceID[MAX_PATH + 1];
        DWORD reqSize = 0;
        TCHAR CheckLists[][MAX_PATH + 1] = {
            TEXT("HID\\VID_0458&PID_5020&COL05"), // new
            TEXT("HID\\VID_0458&PID_501E&COL05"), // older
        };

        SetupDiGetDeviceInstanceId(hDevInfo, &DeviceInfoData, InstanceID, sizeof(InstanceID), &reqSize);
        for (ULONG i = 0; i < 2; i++)
            if (NULL != _tcsstr(InstanceID, CheckLists[i]))
            {
                bFind = true;
                break;
            }
    }
    if (INVALID_HANDLE_VALUE != hDevInfo)
        SetupDiDestroyDeviceInfoList(hDevInfo);

    return bFind;
}

int _tmain(int argc, _TCHAR* argv[])
{

    if (chkDigitizer())
        _tprintf(TEXT("Find out a digitizer device on system\n"));
    else
        _tprintf(TEXT("no digitizer on system\n"));

	return 0;
}