Friday, December 20, 2019

Tip: Using matplotlib from Python in Visual C++

Recently when playing with OpenCV, I realized that I need to have a library that can display images with the scale that i can set. After some searching, I found that I can make use of the matplotlib library from Python in my C++ program. After further searching, I found that I can borrow the C++ library that call to the Python matplotlib library from the following GitHub Matplotlib

How the C++ library can make use of the Python matplotlib package is by using the Python compiled library (Python36.lib) and referencing the Python.h header file, which I got it from the Python installation path at C:\Program Files (x86)\Python3.6.3\libs. If the file you are an invalid lib file or dll file, most probably you will see the following error.
In order to get rid of this error, I built the application with 32-bit release version of python library.


I have to ensure that I have matplotlib installed for Python. Note that I have multiple versions of the Python installed in my PC and I need to install the matplotlib package just for Python3.6.3 because I am going to use that Python version in my C++ project. I used the following command to install the matplotlib package just for Python 3.6.3

C:\Program Files (x86)\Python3.6.3>python -m pip install matplotlib.

Basically I CD the Python installation path so that when i call the Python command it will call to the 3.6.3 version, as my default Python installation at that time was 3.7.3, which is not what i want to use in my C++ project.

Note: If you are using python.exe, that means the numpy and matplotlib packages need to be compatible with the "Release" version. If you are using python_d.exe then you need to ensure numpy and matplotlib is compatible with the "Debug" version. For debug version you can use C:\Program Files (x86)\Python3.6.3>python_d.exe -m pip install matplotlib to do correct package installation. Typical error one will get when the version is not compatible is ImportError: cannot import name 'multiarray'
A good guideline for using debug version of the Python with release version of matplotlib from within Visual C++ Numpy issue

The full source code for the release can be retrieved from GitHub https://github.com/leonwooster/matplotlib

Sunday, October 20, 2019

Tip: Web API vs WCF - High Level

Location Concern
Within the company Network and .NET based clients : Use WCF with TCP binding (Fast communication than HTTP)
Outside the company Network, and use diverse technologies like PHP, Python, web UI etc: Use Web API with REST

Security Concern
WCF support WS- standard and SOAP protocal.
Web API is tighly coupled with http, so can only use http releated security measures.

Network languages
WCF support multiple protocols like SOAP, TCP, HTTP, HTTPS, Named Pipe, MSMQ,
Web API supports only http, https

Architecture
WCF can be used to build SOA compliance services due to the nature that it's able to support different protocols.
Web API is mainly used for front end integration, e.g. Web UI because of http protocol.


Message Format
WCF supports more message formats than web api, e.g. binary message formats
Web api supports text based message format e.g. JSON, XML

Speed
WCF offers faster speed than web api when the service and client sits in the same server. We can use tcp protocol to achieve fast message transfer.

Wednesday, October 9, 2019

Tip: How to add form data as a parameter(s) into Swagger UI

Recently I come across the need to pass form data from an Angular form to a web API written in ASP.NET MVC. The web api does not accept any input parameter specifically via the function signature. However i need to access some form values via the HttpContext.Current.Request.Form. Unfortunately the form does not provide any text box to enter the necessary payload.

After doing some research I found out how to make Swagger to show an extra text box just for that particular web api. First of all I need to define a function attribute that i can use for the web api, and also to tell Swagger that the function actually consist of extra parameters that need to shown in the UI. The code looks like the following:

The next piece of the code shows how we can register the attribute filter to the Swagger. The code tells Swagger to go through every Controller class in the project and check for the specified filter and in turn inform the Swagger UI about any extra parameters to create.
As you can see during application startup, the Swagger will trigger this code via the following configuration in SwaggerConfig.cs file.
The following code shows how the attributed is applied:








The picture below shows the end result in Swagger UI:





Thursday, September 26, 2019

Tip: SharePoint 365 CSOM Nuget Package

Which on to use for SharePoint Online, when you want to use CSOM to connect to it?

NOT To Use


To Use

The reason for this post is to let people know that whenever you someone try to execute some operations to SharePoint Online and receive the following error, when your codes seem to be correct:
Microsoft.SharePoint.Client.CollectionNotInitializedException HResult=0x80131509 Message=The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. Source=Microsoft.SharePoint.Client.Runtime
The are two reasons for such scenario:

  1. Try to access some properties within the CSOM object namespace but did not execute the proper "Load" and "ExecuteQuery" functions. 
  2. Used the wrong library version. As indicated above the package shown in first picture is for SharePoint2010 whereas the second picture is for SharePoint Online.

Thursday, September 12, 2019

Tip: How to kill the WebHelper sub-processes from BitTorrent

It's annoying when you found out that some unidentified processes are hogging a lot of CPU resources while running BitTorrent to download some files. Since i know how to do programming, i've created a console application that constantly monitoring the process list for WebHelper sub processes and kill them when found. As you can see from the picture below, the program checks the process list every minute.
The simple application which makes use of C# and Windows Form can be retrieved from Git Hub.

Tip: How to get the internal name for SharePoint 365 list

Recently i was trying to do some CSOM coding for a project that requires files to be migrated to SharePoint 365. I created a list in the SharePoint web site and was trying to add new records to the list. I tried to use the "Display Name" for each column from the list in as shown in Diagram 1.

Diagram 1

As you can see from Diagram 2, the line of code that does not work actually makes use of the "Display Name".

Diagram 2

if you try to execute the code you will get the following error.

Diagram 3

In order to get the internal for all the columns, i have to go to the list settings and click on each column and to check the URL for the column internal name. You can see that in Diagram 4.

Diagram 4

The url actually contains the column internal name and you can copy the name from there. The other way which is not covered here is using code to retrieve the internal names.

Sunday, June 23, 2019

Tip: How to copy file(s) from Ubuntu to Windows 10

The following is the step by step instruction to copy file(s) from Ubuntu to Windows 10 using the WSL windows feature. Assuming that you have WSL and Ubuntu installed in your windows 10 machine.

1. Open up a command prompt with admin privilege.
2. Type "wsl" to enter the Ubuntu default Linux system.
3. use the following command to copy file:
                cp UBUNTU_FILES_OR_DIR WINDOWS10_DIR -r
                example: cp opencv-4.0.0/build/CMakeFiles /mnt/c/Temp -r

From the example, notice the use of /mnt to access to Windows 10 file system. "c" means c drive.

Tip: For guaranteed network drive map/access using "Net Use"

The following script can be used to map a network drive or to access a folder in a network drive such as NASH or File Server