Tag Archives: Windows

How to become a Windows Expert ?

This article is the continuation of How to become a Microsoft Expert? (http://netazurerangers.com/blog/comment-devenir-un-expert-microsoft/)

Windows has been Microsoft’s technological flagship for 25 years. You will tell me, yes but now there is Azure. OK but what is Azure? It is ; if I ignore the Linux part; Windows Server and Service Fabric… and that’s Windows. It’s C / C ++. And yes, again! There is no secret. it must work quickly and well. It must be reliable, robust, fast and secure.

In one of my last post “C ++ unsafe and unsecure?” (http://netazurerangers.com/blog/c-unsafe-et-unsecure/), I explain why C / C ++ is the best and why Microsoft is doing 95% of its products with. Microsoft is the # 1 company in the software industry. It’s not an advertising agency like Google or Facebook, it’s pure juice Tech. Microsoft sells Products and Services. Anyway next…

How to become a Windows expert? The question is asked. First, we learn about the operating system principles via Microsoft Docs (ex: MSDN LIbrary) on https://docs.microsoft.com/en-us/windows-hardware/drivers/kernel/overview-of-windows -components

Then, we read the passage on User mode and Kernel mode via https://docs.microsoft.com/en-us/windows-hardware/drivers/gettingstarted/user-mode-and-kernel-mode

From there, we know the basic architecture of Windows. now we attack the elements on the operating system, namely the kernel and the thread scheduler. Windows order threads, Linux order processes. These two systems do not work the same way. The Processes & Threads doc is here: https://docs.microsoft.com/en-us/windows/win32/procthread/about-processes-and-threads

Then we go to practice, how to create a thread, a process, reach the end, etc. the API doc also called reference doc is here: https://docs.microsoft.com/en-us/windows/win32/procthread/process-and-thread-reference

The easiest examples can be viewed via https://docs.microsoft.com/en-us/windows/win32/procthread/process-and-thread-functions#process-and-thread-functions and more specifically the CreateThread function: https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createthread?redirectedfrom=MSDN and its example: https://docs.microsoft.com/en-us/windows / win32 / procthread / Creating-threads

To test this, you must install Visual C ++ available in Visual Studio 2019 for example, Community Edition or Pro 30 day trial. If you received money from Santa Claus, buy the following books:

Windows via C / C ++ by Jeffrey Richter and Christophe Nasarre

Windows Internals ex-Inside Windows NT (https://docs.microsoft.com/en-us/sysinternals/learn/windows-internals)

I bought Inside Windows NT in 1992 and got the virus.

Windows is huge. It’s powerful. You read the Windows Internals book and you will have vision; you will understand how the OS works. It’s very interesting and you will have no trouble understanding new Microsoft technologies with that. Microsoft NET, CLR, BCL, it’s done with C ++ and parts of the Windows API aka Win32. Watch the code on GitHub of CoreCLR (https://github.com/dotnet/runtime)

There are urban legends that Microsoft rewrites Windows from scratch; this is for managers and IT 01. For technicians, the truth is that Windows is sitting on the same code and has been evolving for 25 years. The code is improved and regularly revised in Modern C ++. I can certify it because I have the source code of Windows NT 4, Windows 2000 and access to the latest source code of Windows 190x. The code is made in:

C for kernel and drivers
in C / C ++ for the rest.
What is Modern C ++?

automatic memory release with smart points
using the Standard Template Library (STL)
use of C ++ 11/14/17 with auto, lambdas, etc.
Windows uses COM technology a lot. A COM component is registered in the registry and is invoked via APIs (https://docs.microsoft.com/en-us/windows/win32/api/_com/)

A COM component is a class with AddRef, Release, QueryInterface and methods:

The COM component is created via CoCreateObject and the COM factory:

For more information on COM components, I made in 200x a translation of some elements of “Inside COM + Base Services”: Apartments | Threads | Apartments types | Threading for In-Proc components | Apartment and languages

For more information on COM, get your hands on Inside COM + Base Services (http://www.windowscpp.com/Books/MSPress-InsideCOMBaseServices.zip) or on docs but on docs, the doc is spartan.

201x saw Microsoft turn to open source so you can find Windows components in open source:

Windows Terminal (https://github.com/microsoft/terminal)
Windows NET CoreCLR (https://github.com/dotnet/runtime)
Do like me, study these two modules and try to contribute in GitHub. And who knows, one day, you may work for Microsoft!

Chick!

Christophe | http://www.christophepichaud.com

Multiplatform development with C++

To develop multi-platform applications, there are not fifty possible choices, there is only one that is free and efficient: it’s C++.

Only the C/C++ can take advantage of the latest developments in Windows, Linux, Android and iOS SDKs because the system and its environment are made with it. The advantage of C++ is that it ‘builds on the metal’: there is no faster. It takes advantage of the software architecture of the operating and hardware systems of the latest x86, x64 and ARM processors. For forty years, C++code optimizers have guaranteed the best possible quality of code.

C, It’s the new assembler. C++, It allows the object oriented: abstractions, inheritance and polymorphism (virtual functions), overload of operators, templates. With its STL (Standard Template Library) bookstore and in conjunction with a bookstore such as Boost (boost.org), the C++ a-has a universal toolbox that handles strings, containers (collections), algorithms, I/O, threads, smart pointers, communications, etc.

Take the plunge. Install Visual C++ Windows, GCC Linux, XCode on Mac and share business code. Make rich graphical interfaces and take advantage of the best development language that’s C++.

You will tell me there are hybrid solutions like NET or Java? These solutions are a set of thousands of heavy, slow sheets that do not create world-class applications. In cars, there are Fiat 500s and Ferraris. At the same price (see cheaper), what do you take? There’s no photo…

Article for Programmez October 2019

In the N°233 Issue of Programmez magazine, I have written a technical article about Windows Subsystem for LInux v2 (WSL2).

Making a POST call with JSON data using CPPREST C++ SDK

The source code here is the same as the C# version in the previous post. The client makes a simple call like that: 

	std::string value2 = "azertyuiopqsdfghjklmwxcvbn";
	std::string buffer = Base64Helper::base64_encode((const unsigned char*)value2.c_str(), value2.length());
	std::wstring value3(buffer.begin(), buffer.end());
	SetData(key, value3, value3.length(), dbname);

 

Here is the wrapper code for SetData. It’s included in HttpLMDB dll: 

bool HTTPLMDB_API SetData(std::wstring key, std::wstring valueb64, DWORD dwLen, std::wstring name)
{
	std::wstring port = Constants::MasterNodePort;
	std::wstring ip = ServerHelper::GetIP();
	std::wstring url = ServerHelper::BuildURL(ip, port);

	std::wstring contentType = _T("Content-Type");
	std::wstring contentTypeV = _T("application/json");
	std::wstring keepAlive = _T("Keep-Alive");
	std::wstring keepAliveV = _T("false");
	std::wstring contentLength = _T("Content-Length");
	
	std::wostringstream bufLen;
	bufLen << contentType.length() + contentTypeV.length() + keepAlive.length() + keepAliveV.length() + contentLength.length() + 4;
	std::wstring len = bufLen.str().c_str();

	std::wostringstream buf;
	buf << url << '/' << Constants::Request << Constants::VerbSetDataB64
		<< _T("&name=") << name;
	url = buf.str().c_str();	
	//{"key":"key_toto0","value":"value_toto0"}
	std::wostringstream bufjson;
	bufjson << "{" << '"' << "key" << '"' << ":" << '"' << key << '"' << ","
		<< '"' << "value" << '"' << ":" << '"' << valueb64 << '"' << '}';
	std::wstring jsonv = bufjson.str().c_str();
	//wcout << _T("jsonv : ") << jsonv << endl;

	http_client client_lmdb(url);
	http_request request(methods::POST);
	request.headers().add(contentType, contentTypeV);
	request.headers().add(keepAlive, keepAliveV);
	request.headers().add(contentLength, len);
	request.set_body(jsonv);

	http_response response;
	response = client_lmdb.request(request).get();

	wcout << response.to_string() << endl;

	return true;
}

 

Here is the code. It’s not very difficult.

Handling a long url using POST verb

To be able to store data in my LMDB Service, I need to store data as base64 items. To do that, I need to transmit json data from the client to the server. It can’t be passed on the url. So I use post handling. Here is the C++ handler:

void TheServer::handle_post(http_request message)

{
       try
       {
             g_Logger.WriteLog(_T("handle_post"));
 
             PrintRequest(message);

             std::wstring request = ServerHelper::FindParameter(message, _T("request"));
            
              if (request == Constants::VerbSetDataB64)
             {
                 RequestVerbSetData64(message);
                    return;
             }
             else if (request == Constants::VerbGetDataB64)
             {
                    // Does not work yet
                    RequestVerbGetData64(message);
                    return;
             }
       }
       catch (...)
       {
             // an internal problem occured
             g_Logger.WriteLog(_T("handle_post exception..."));
       }
 
       message.reply(status_codes::OK);
};

 

The C++ routine here to analyze is RequestVerbSetData64. Here is the code:

void TheServer::RequestVerbSetData64(http_request message)
{
       USES_CONVERSION;
       CLMDBWrapper lmdb;
       g_Logger.WriteLog(Constants::VerbSetDataB64.c_str());
 
       std::wstring dbNameW = ServerHelper::FindParameter(message, _T("name"));

       std::string dbName(dbNameW.begin(), dbNameW.end());
       std::wstring json;
       web::json::value jsonV = message.extract_json().get();

       Data data = Data::FromJSON(jsonV.as_object());
       TCHAR sz[255];
       _stprintf_s(sz, _T("Data key:%s value:..."), data.key.c_str());
       g_Logger.WriteLog(sz);

       if (lmdb.Init((LPSTR)dbName.c_str()) == false)
       {
             g_Logger.WriteLog(_T("LMDB Init not done !"));

            message.reply(status_codes::OK);
             return;
       }

       LPSTR lpszKey = W2A(data.key.c_str());
       LPSTR lpszValue = W2A(data.value.c_str());
       DWORD dwLen = strlen(lpszValue);

       lmdb.SetData(lpszKey, lpszValue, dwLen);

       message.reply(status_codes::OK);

       lmdb.Uninit((LPSTR)dbName.c_str());
}

 

The source code is simple to write, simple to read. Because it is native code, it is fast and we just need to distribute the dll we use. here, it’s just the C runtime, the C++ runtime and the CPPREST dll. This is the advantage of the native stuff, you don’t need to distribute any framework that size is around 350 MB… It’s lightweight, it’s fast, it’s built on the metal.

LMDB for Windows

When I discovered the fantastic history of LMDB, It fascinated me. For a R&D projet, we need to present something that looks sexy and with a strong architecture. I proposed to study LMDB and bring to it the distributed engine and the clustering stuff with nodes. There are plenty ways of acheiveing this.

First, OpenLDAP-LMDB need to be ported on the Windows platform. It’s a C based code so there are no some many changes to fix for building on Windows. There was a path concat that made some problems but it’s defined as constants so I fixed it rapidly. I put it in Github after the operation was done.: https://github.com/ChristophePichaud/LMDBWindows

I was able to build a static lib for LMDB. But in Windows, we love shared modules called DLL so I have to put some little things on the source code to enable loading shared modules DLL (Dynamic Link Library).

#ifdef LMDBWINDOWSDLL_EXPORTS
#define LMDBWINDOWSDLL_API __declspec(dllexport)
#else
#define LMDBWINDOWSDLL_API __declspec(dllimport)
#endif

With theses expanded macros, it can export functions. Every headers and bodys need to be decorated. Here is the explaination. When you build the library, you define de constant LMDBWINDOWSDLL_EXPORTS in the project settings then every macros instruct the compiler to export the functions. When you build an application who needs the dll, the macro is expanded as an import operation and at the link phase, you need to link with the little stub to attach the library.

If you are new to Windows DLLs making, check this portion of doc on MSDN: https://msdn.microsoft.com/en-us/library/a90k134d.aspx