Category Archives: Software Development

Anything to do with developing Apps, Desktop application, server application, API etc.

C++: Range-Based loops

I have come across a new way to loop through collections and range.

It consist of example code below:

for (auto const &x : vec)
{
  // x is a reference to an const item of vec
  // We can not change vec's items by changing x 
}

This is definitely new to me and a welcome change as using auto creates a placeholder for any types and can be anything with a proper cast to return back to its original form.

The new loop also simplifies the code as more explain by the link:

https://stackoverflow.com/questions/15176104/c11-range-based-loop-get-item-by-value-or-reference-to-const

Enter ANSI, and Unicode Character Encoding

Been developing a lot in .NET and C/C++ and been overwhelm by the fact that character encoding plays a part in building applications. One might not know that it existed under the hood but understanding why they are there is crucial to getting different quirks and bugs.

In laymen term each machine or computers have a standard default character encoding that translates each character to a byte and multi-byte (Unicode). Mostly nowadays Unicode is used to support wide variety of characters from native English alphabet to other non-english like Chinese, Japanese etc.

Check the link below for a very thorough explanation and more about character and coding and types in Windows system.

Including _T(TCHAR) macro that tells the compiler to use char or wchar_t and L”Literal” to interpret the string as a unicode multibyte string.

Copied and Pasted from the article:
The ## symbol is token pasting operator, which would turn _T("Unicode") into L"Unicode", where the string passed is argument to macro – If _UNICODE is defined. If _UNICODE is not defined, _T("Unicode") would simply mean "Unicode". The token pasting operator did exist even in C language, and is not specific about VC++ or character encoding.

Note that these macros can be used for strings as well as characters. _T('R') would turn into L'R' or simple 'R' – former is Unicode character, latter is ANSI character.

No, you cannot use these macros to convert variables (string or character) into Unicode/non-Unicode text

https://www.codeproject.com/Articles/76252/What-are-TCHAR-WCHAR-LPSTR-LPWSTR-LPCTSTR-etc

C: Common Data Types and Calling Convention __stdcall

I know I should not link a already well made documentation but for my sake and my project. I d like to put in on here anyways.

Since I have been dealing with different data types in C, its hard to remember what they do and what they stands for. Eg. LPCWSTR, LPBYTE etc.

Here is the link to the documentation: C Common Data Types
https://msdn.microsoft.com/en-us/library/cc230309.aspx

Also another I want to touch on is Windows C/C++ Calling Convention in what it actually means in laymen term. Here is an article about it:
http://stackoverflow.com/questions/538134/exporting-functions-from-a-dll-with-dllexport

Basically compiler has a way to call a function and how to clean up after the call is made. How arguments are push into the stack and who is responsible for cleaning up after. __stdcall usually makes the caller responsible for pushing the argument from LEFT->RIGHT and make the CALLEE responsible for clean up. __cdecl is from RIGHT->LEFT and makes the caller responsible for cleaning up.

Extern “C” and Name Mangling

Name mangling is very common in C++ compilers when a function have the same name with different parameters or overloading.

As the compiler compiles your code, with a function with the same name , the compiler tries to rename the function or method so they dont clash with each other at runtime and providing different signature.

Using Extern “C” on the other hand if you are calling a C function exactly as it is from your C++ code. With a syntax of

extern “C” statement or block of code

This prevent the compiler from mangling the name at compile time otherwise getting bunch of errors and crashes.

Here are some good articles about it:
http://www.geeksforgeeks.org/extern-c-in-c/

Here some good link about Calling Convention: eg. __stdcall, __cdecl
http://blog.aaronballman.com/2011/04/the-importance-of-calling-conventions/

C & C++: Shell Extension Need to know and understand

Its been a while since I posted something here due to my lost of my  laptop. Anyways aside from that I’m now back and continue this journey of the shell extension in c and c++.

I have asked about the the fundamental of the COM in stack overflow. Follow this question on there to get more information.

http://stackoverflow.com/questions/43010172/addref-and-release-in-iunknown-what-do-they-actually-do

Follow the link for a beginner explanation as well for terms and classes, typedef, and windows.h

This is also for reference for myself.

More to come in my shell extension journey.

C++: Road Block – Renamer Tool

I was just coding away with my Renamer Tool and suddenly hit a road block. My Renamer tool is a Csharp WPF application that renames files according to Filter set by the user. It will also features a way to select multiple files from window explorer to be selected and right clicked to open up with the program.

I thought this was simple as I was googling along adding context menu feature. It led me to..

Shell Extensions.

Ugh… it is said that writing a shell extension in managed code like CSharp is discouraged. And mostly the only way is through..

C++ Active Template Library or COM…. went to have a look through how to implement this. Questions upon questions upon questions!

Apparently writing a shell extension in C++ is like diving to a different world of programming, prepare to go back to the old ways as this requires the knowledge of the old windows api calls. Which has specific OS types and classes.

My program is almost there , and knowing me, I like to use this program for my personal use and Im pretty sure others would like to use them as well.

So what now? I do have some basic knowledge of C/C++, but never ever done shell extensions before or COM which is like assembly language.

Wish me luck, cause Im about to dive in!

Helpful Links:

http://stackoverflow.com/questions/8811621/how-to-write-a-shell-extension-in-c

http://softwareengineering.stackexchange.com/questions/194764/what-is-lpctstr