How to create a C# project with C++ library?

This post is written with help from many online resources and professional experience.

The main motivation behind using a C++ library for a C# code is code re-usability. I personally found it really hard to find a good resource on the net to create a C# project with C++ code.

Sample project –

Here are the steps involved in visual studio –

Adding the C++ project to VS-

First step is to get your C++ native code in to visual studio. File-> New -> Project -> Visual C++ -> Empty Project -> Ok . After this you can add existing code to your newly created project.  In my code example the native C++ project is called “TweetBank”

  1. Right click the TweetBank project. Properties->General->ConfigurationType->StaticLibrary
  2. When you build it a lib directory will be created under “\TweetBank\Debug\TweetBank.lib”

Adding the CLR project to VS-

The CLR project acts like a bridge between your C++ code and C# code. File-> Add -> New Project -> Visual C++ -> CLR ->Empty Project

In my code example, the project and the class is named “TweetBankCLR”.

  1. Right click the TweetBankCLR project and click properties. C/C++->General->Additional Include Directories-> <Add TweetBankCore’s project directory>
  2. In properties go to Linker -> Input -> Additional Dependencies -> “TweetBank.lib”
  3. In propertied go to Linker -> general -> Additional Library Directories -> “E:\Apps\Dropbox\Code\vs\TweetBank\Debug”
  4. Right click the TweetBankCLR project. Properties->General->ConfigurationType->dll (This will create a DLL)
  5. Add reference to native project. Right click TweetBankCLR Add- > Reference -> Projects/solutions -> TweetBank

Adding the C# project to VS-

I will be creating a WPF project here. File-> Add -> New Project -> Visual C# -> Empty WPF Application. In my code example this project is named “TweetBankWPF”.

  1. Right click the TweetBankWPF and click “Set as Startup Project”
  2. Add reference to CLR project. Right click TweetBankWPF Add- > Reference -> Projects/solutions -> TweetBankCLR
  3. Right click and build TweetBankCLR. This should resolve any unresolved references from you C# code.
  4. Build TweetBankWPF. Tada!

More helpful links –

  3. For more info on how different data need to be exchanged between C# and C++ –





How to create a C# project with C++ library?

How to ssh to a ubuntu server running in your home from outside?

I have a ssh server setup in my home. I wanted to be able to login to it from a coffee shop so that I can access my build environment. I have a residential comcast connection. They frown upon having a high bandwidth website hosting from your residential connection, but a low bandwidth personal usage should be fine.

1. Setup Static IP for your server

First you need to setup a static ip for your server at home. This link will help you do that. Remember this static IP as you will be using it later.

2. Setup port forwarding in your router

Traffic from the internet arrives to your router. Router then sees to which port the traffic is intended for. You can have any port forward to any specific private ip address in your home. I have an apple airport extreme and this link helped me setup my port forwarding. Set the “Private IP address” field to the static IP you set up in the previous step. Also I have set all the public and private ports to 22. You can change it to a something different if you want.

3. Finding a static host name for your dynamic ip address

So the external IP address of your router( ie the IP as seen from the internet ) keeps changing as they are not static. You can get your external ip address by going to this website ( There are services which will give you an easy to remember hostname to your dynamic external ip address. This way you will only have to remember the hostname to access your router. One of them is

You can then try to ssh into your server by trying this “ssh

Concluding here is whats happening, the hostname “” resolves to your external IP address. SSH uses port 22 by default, so the router sees the traffic coming to port 22. Port 22 is mapped to your  server’s static internal IP, so the traffic is sent to that and back 🙂

Its important to secure yourself from brute force ssh login attempts. If you are using a password for logging in make sure you have a strong one. There are other methods like this one here which will also help.

How to ssh to a ubuntu server running in your home from outside?

How to ssh to an ubuntu vm running on hyper v?

This link will show you on how to install any OS running on hyperV. I installed Ubuntu 14.04 using this method.

You will need to do three things once you login to ubuntu.

1. Install ssh as its not installed in ubuntu by default

2. Then you will need to disable ubuntu’s firewall so that it allows incoming connections.

3. Find the ip address of your VM by running “ifconfig”

Then you can go back to windows(using putty or whatever you use) and run “ssh username@IP_of_VM

How to ssh to an ubuntu vm running on hyper v?

Compilation error – error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before

If you run into following error “FILENAME.h:135: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before” during compilations.
The error is pretty descriptive. But in my case there were all expected symbols but it still was giving that error.
After some time with some help of  a colleague, figured out that I was trying to include a .h file in a .c file which was actually meant for a .cxx/.cpp file. So if your header file has classes defined in it, it is meant for a c++ file and not a c file. It sounds really simple but it did stump me for sometime 🙂
Compilation error – error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before

C Notes

1. when #define is used, we are replacing a number with a string.
Ex. #define NUM 5
So, we cannot use NUM– or NUM++, since NUM is not a memory location / register to be incremented or decremented.

2. When accessing an array with a pointer, adding 1 to the pointer actually increments the pointer by 4(depending on the compiler being used).
int arr[5] = {1,4,1,5,1};
//To access the last element of the array =
printf("%d\n",*(arr+4)); // is CORRECT
printf("%d\n",*(arr+(sizeof(int)*4)); //is WRONG

Summarizing: When you add a value to a pointer it automatically increments the number of bytes the data type is allocated. Here, by adding 4 the pointer is actually incremented by 16.

3. By default character is signed(so are other data types). It varies from -128(0x80) to 127(0x79). If we make the character unsigned – the values range from (0 to 255).

4. How to find if a system is little endian or Big endian
In case of a little endian(all intel chips), the least significant byte is stored in the first memory address. But in case of Big Endian, the most significant byte is stored in the first memory location.
void getEndian(){
short int word = 1;
char *byte = (char *) &word;
printf("%s",(byte[0] ? "Little endian" : "Big Endian"));

5. Segmentation Fault
A segmentation fault (often shortened to segfault), bus error or access violation is generally an attempt to access memory that the CPU cannot physically address. It occurs when the hardware notifies an operating system about a memory access violation. The OS kernel then sends a signal to the process which caused the exception. By default, the process receiving the signal dumps core and terminates
Source – Wikipedia

6.When we increment an unsigned data type which is holding the maximum value of that data type, it will become 0
Example :

unsigned long ul = ULONG_MAX;
printf(“%d”,ul); // will print 0

7. Pointers – link

8. Structures – inside a struct, if there is an integer and a char, the size of the struct will be 8 and not 5. This is because the compiler allocates more memory to make it aligned. If a struct has a single char, the size will be 1.

struct mystruct{

int i;

char c;

}; //size is 8 and not 5

C Notes