Showing tag results for Code

May 9, 2013
Post comments count0
Post likes count1

Why am I getting LNK2019 unresolved external for my inline function?

Raymond Chen
Raymond Chen

More than once, I've seen somebody confused by how inline functions work. I have implemented a few inline functions in one of my cpp files, and I want to use it from other cpp files, so I declare them as extern. But sometimes I will get linker error 2019 (unresolved external) for the inline functions. // a.cpp inline bool foo() { return false...

Code
May 8, 2013
Post comments count0
Post likes count1

Mathematical formulas are designed to be pretty, not to be suitable for computation

Raymond Chen
Raymond Chen

When you ask a mathematician to come up with a formula to solve a problem, you will get something that looks pretty, but that doesn't mean that it lends itself well to computation. For example, consider the binomial coefficient, traditionally written nCk or C(n, k), and in more modern notation as ( nk ). If you ask a mathematician f...

Code
May 6, 2013
Post comments count0
Post likes count1

Reading mouse input from a console program, and programmatically turning off Quick Edit mode

Raymond Chen
Raymond Chen

Today's Little program shows how to read mouse input from a console program. You might use this if you are writing a console-mode text editor with mouse support, or maybe you want to want to add mouse support to your roguelike game. But I'm not going to implement the game itself. Instead, I'm just going to print mouse coordinates to the screen....

Code
May 3, 2013
Post comments count0
Post likes count1

Creating a simple pidl: For the times you care enough to send the very fake

Raymond Chen
Raymond Chen

I'll assume that we all know what pidls are and how the shell namespace uses them. That's the prerequisite for today. A simple pidl is an item ID list that refers to a file or directory that may not actually exist. It's a way of playing "what if": "If there were a file or directory at this location, here is what I would have created to represent...

Code
May 1, 2013
Post comments count0
Post likes count1

How do I get the current value of the RSP register from a C/C++ function? (No answer, but a solution.)

Raymond Chen
Raymond Chen

A customer using Visual Studio wanted to know how to obtain the current value of the x64 RSP register from a C/C++ function. They noted that on the x86, you can drop to inline assembly, and on the ia64, you can use the intrinsic to retrieve the value of any register. There is no corresponding intrinsic on x64. There's no really good way of doin...

Code
Apr 29, 2013
Post comments count0
Post likes count1

Getting the display name for a shell property

Raymond Chen
Raymond Chen

Today's Little Program takes the symbolic name for a shell property and returns a string suitable for display to the end-user, translated into the user's specified display language. #include <windows.h> #include <ole2.h> #include <propsys.h> #include <propkey.h> #include <atlbase.h> #include <atlalloc.h> int ...

Code
Apr 26, 2013
Post comments count0
Post likes count1

Another way to create a process with attributes, maybe worse maybe better

Raymond Chen
Raymond Chen

Adam Rosenfield noted that "those sure are a lot of hoops you have to jump through to solve this unusual problem" of specifying which handles are inherited by a new process. Well, first of all, what's so wrong with that? You have to jump through a lot of hoops when you are in an unusual situation. But by definition, most people are not in an unus...

Code
Apr 25, 2013
Post comments count0
Post likes count1

If you’re going to use an interlocked operation to generate a unique value, you need to use it before it’s gone

Raymond Chen
Raymond Chen

Is the Interlocked­Increment function broken? One person seemed to think so. We're finding that the Interlocked­Increment is producing duplicate values. Are there are any know bugs in Interlocked­Increment? Because of course when something doesn't work, it's because you are the victim of a vast conspiracy. There is a fundamental ...

Code
Apr 24, 2013
Post comments count0
Post likes count1

Dark corners of C/C++: The typedef keyword doesn’t need to be the first word on the line

Raymond Chen
Raymond Chen

Here are some strange but legal declarations in C/C++: int typedef a; short unsigned typedef b; By convention, the typedef keyword comes at the beginning of the line, but this is not actually required by the language. The above declarations are equivalent to typedef int a; typedef short unsigned b; The C language (but not C++) also permits...

Code
Apr 22, 2013
Post comments count0
Post likes count1

Getting the current selection from an Explorer window

Raymond Chen
Raymond Chen

Today's Little Program prints the current selection in all open Explorer windows. (This is an alternative to the C++ version that involves a ridiculous amount of typing.) var shellWindows = new ActiveXObject("Shell.Application").Windows(); for (var i = 0; i < shellWindows.Count; i++) { var w = shellWindows.Item(i); WScript.StdOut.WriteLine...

Code